<?php

namespace Sitecake\ServiceProviders;

use Sitecake\Container;
use Sitecake\Filesystem\Filesystem;

class FilesystemServiceProvider
{
    public function register(Container $app): void
    {
        $app['filesystem.adapter.config'] = '.';

        $app['filesystem'] = function ($app) {
            $root = $app['filesystem.adapter.config'] ?? '.';
            // The config may be a plain string (root path) or an array whose first element is the root.
            if (is_array($root)) {
                $root = $root[0] ?? '.';
            }
            return new Filesystem($root);
        };
    }
}
