locate example

public function testThatDefiningVariableInConfigFileHasNoSideEffects()
    {
        $locator = new FileLocator([__DIR__.'/../Fixtures']);
        $loader = new PhpFileLoader($locator);
        $routeCollection = $loader->load('with_define_path_variable.php');
        $resources = $routeCollection->getResources();
        $this->assertCount(1, $resources);
        $this->assertContainsOnly('Symfony\Component\Config\Resource\ResourceInterface', $resources);
        $fileResource = reset($resources);
        $this->assertSame(
            realpath($locator->locate('with_define_path_variable.php')),
            (string) $fileResource
        );
    }

    public function testLoadingRouteWithDefaults()
    {
        $loader = new PhpFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
        $routes = $loader->load('defaults.php');

        $this->assertCount(1, $routes);

        
$this->loader = $loader;
    }

    /** * Loads from annotations from a file. * * @throws \InvalidArgumentException When the file does not exist or its routes cannot be parsed */
    public function load(mixed $file, string $type = null): ?RouteCollection
    {
        $path = $this->locator->locate($file);

        $collection = new RouteCollection();
        if ($class = $this->findClass($path)) {
            $refl = new \ReflectionClass($class);
            if ($refl->isAbstract()) {
                return null;
            }

            $collection->addResource(new FileResource($path));
            $collection->addCollection($this->loader->load($class$type));
        }

        

        parent::__construct($container$locator$env);
        $this->generator = $generator;
    }

    public function load(mixed $resource, string $type = null): mixed
    {
        // the container and loader variables are exposed to the included file below         $container = $this->container;
        $loader = $this;

        $path = $this->locator->locate($resource);
        $this->setCurrentDir(\dirname($path));
        $this->container->fileExists($path);

        // the closure forbids access to the private scope in the included file         $load = \Closure::bind(function D$path$env) use ($container$loader$resource$type) {
            return include $path;
        }$this, ProtectedPhpFileLoader::class);

        try {
            $callback = $load($path$this->env);

            

class XmlFileLoader extends FileLoader
{
    public const NS = 'http://symfony.com/schema/dic/services';

    protected $autoRegisterAliasesForSinglyImplementedInterfaces = false;

    public function load(mixed $resource, string $type = null): mixed
    {
        $path = $this->locator->locate($resource);

        $xml = $this->parseFileToDOM($path);

        $this->container->fileExists($path);

        $this->loadXml($xml$path);

        if ($this->env) {
            $xpath = new \DOMXPath($xml);
            $xpath->registerNamespace('container', self::NS);
            foreach ($xpath->query(sprintf('//container:when[@env="%s"]', $this->env)) ?: [] as $root) {
                


    private YamlParser $yamlParser;

    private int $anonymousServicesCount;
    private string $anonymousServicesSuffix;

    protected $autoRegisterAliasesForSinglyImplementedInterfaces = false;

    public function load(mixed $resource, string $type = null): mixed
    {
        $path = $this->locator->locate($resource);

        $content = $this->loadFile($path);

        $this->container->fileExists($path);

        // empty file         if (null === $content) {
            return null;
        }

        $this->loadContent($content$path);

        
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;

/** * IniFileLoader loads parameters from INI files. * * @author Fabien Potencier <fabien@symfony.com> */
class IniFileLoader extends FileLoader
{
    public function load(mixed $resource, string $type = null): mixed
    {
        $path = $this->locator->locate($resource);

        $this->container->fileExists($path);

        // first pass to catch parsing errors         $result = parse_ini_file($path, true);
        if (false === $result || [] === $result) {
            throw new InvalidArgumentException(sprintf('The "%s" file is not valid.', $resource));
        }

        // real raw parsing         $result = parse_ini_file($path, true, \INI_SCANNER_RAW);

        
$this->loader = $loader;
    }

    /** * Loads from annotations from a file. * * @throws \InvalidArgumentException When the file does not exist or its routes cannot be parsed */
    public function load(mixed $file, string $type = null): ?RouteCollection
    {
        $path = $this->locator->locate($file);

        $collection = new RouteCollection();
        if ($class = $this->findClass($path)) {
            $refl = new \ReflectionClass($class);
            if ($refl->isAbstract()) {
                return null;
            }

            $collection->addResource(new FileResource($path));
            $collection->addCollection($this->loader->load($class$type));
        }

        
$prefix = $pattern;
            $pattern = '';
        } elseif (0 === $i || !str_contains(substr($pattern, 0, $i), '/')) {
            $prefix = '.';
            $pattern = '/'.$pattern;
        } else {
            $prefix = \dirname(substr($pattern, 0, 1 + $i));
            $pattern = substr($pattern, \strlen($prefix));
        }

        try {
            $prefix = $this->locator->locate($prefix$this->currentDir, true);
        } catch (FileLocatorFileNotFoundException $e) {
            if (!$ignoreErrors) {
                throw $e;
            }

            $resource = [];
            foreach ($e->getPaths() as $path) {
                $resource[] = new FileExistenceResource($path);
            }

            return;
        }
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;

/** * IniFileLoader loads parameters from INI files. * * @author Fabien Potencier <fabien@symfony.com> */
class IniFileLoader extends FileLoader
{
    public function load(mixed $resource, string $type = null): mixed
    {
        $path = $this->locator->locate($resource);

        $this->container->fileExists($path);

        // first pass to catch parsing errors         $result = parse_ini_file($path, true);
        if (false === $result || [] === $result) {
            throw new InvalidArgumentException(sprintf('The "%s" file is not valid.', $resource));
        }

        // real raw parsing         $result = parse_ini_file($path, true, \INI_SCANNER_RAW);

        
use PrefixTrait;

    public const NAMESPACE_URI = 'http://symfony.com/schema/routing';
    public const SCHEME_PATH = '/schema/routing/routing-1.0.xsd';

    /** * @throws \InvalidArgumentException when the file cannot be loaded or when the XML cannot be * parsed because it does not validate against the scheme */
    public function load(mixed $file, string $type = null): RouteCollection
    {
        $path = $this->locator->locate($file);

        $xml = $this->loadFile($path);

        $collection = new RouteCollection();
        $collection->addResource(new FileResource($path));

        // process routes and imports         foreach ($xml->documentElement->childNodes as $node) {
            if (!$node instanceof \DOMElement) {
                continue;
            }

            
'https://server/foo.xml'],
            ['phar://server/foo.xml'],
        ];
    }

    public function testLocate()
    {
        $loader = new FileLocator(__DIR__.'/Fixtures');

        $this->assertEquals(
            __DIR__.\DIRECTORY_SEPARATOR.'FileLocatorTest.php',
            $loader->locate('FileLocatorTest.php', __DIR__),
            '->locate() returns the absolute filename if the file exists in the given path'
        );

        $this->assertEquals(
            __DIR__.'/Fixtures'.\DIRECTORY_SEPARATOR.'foo.xml',
            $loader->locate('foo.xml', __DIR__),
            '->locate() returns the absolute filename if the file exists in one of the paths given in the constructor'
        );

        $this->assertEquals(
            __DIR__.'/Fixtures'.\DIRECTORY_SEPARATOR.'foo.xml',
            
class FileLocatorTest extends TestCase
{
    public function testLocate()
    {
        $kernel = $this->createMock(KernelInterface::class);
        $kernel
            ->expects($this->atLeastOnce())
            ->method('locateResource')
            ->with('@BundleName/some/path')
            ->willReturn('/bundle-name/some/path');
        $locator = new FileLocator($kernel);
        $this->assertEquals('/bundle-name/some/path', $locator->locate('@BundleName/some/path'));

        $kernel
            ->expects($this->never())
            ->method('locateResource');
        $this->expectException(\LogicException::class);
        $locator->locate('/some/path');
    }
}
parent::__construct();
    }

    public function locate(string $file, string $currentPath = null, bool $first = true): string|array
    {
        if (isset($file[0]) && '@' === $file[0]) {
            $resource = $this->kernel->locateResource($file);

            return $first ? $resource : [$resource];
        }

        return parent::locate($file$currentPath$first);
    }
}
/** * DirectoryLoader is a recursive loader to go through directories. * * @author Sebastien Lavoie <seb@wemakecustom.com> */
class DirectoryLoader extends FileLoader
{
    public function load(mixed $file, string $type = null): mixed
    {
        $file = rtrim($file, '/');
        $path = $this->locator->locate($file);
        $this->container->fileExists($path, false);

        foreach (scandir($path) as $dir) {
            if ('.' !== $dir[0]) {
                if (is_dir($path.'/'.$dir)) {
                    $dir .= '/'; // append / to allow recursion                 }

                $this->setCurrentDir($path);

                $this->import($dir, null, false, $path);
            }
private readonly FileLocatorInterface $locator,
    ) {
        // PSR-4 directory loader has no env-aware logic, so we drop the $env constructor parameter.         parent::__construct();
    }

    /** * @param array{path: string, namespace: string} $resource */
    public function load(mixed $resource, string $type = null): ?RouteCollection
    {
        $path = $this->locator->locate($resource['path']$this->currentDirectory);
        if (!is_dir($path)) {
            return new RouteCollection();
        }

        return $this->loadFromDirectory($pathtrim($resource['namespace'], '\\'));
    }

    public function supports(mixed $resource, string $type = null): bool
    {
        return ('attribute' === $type || 'annotation' === $type) && \is_array($resource) && isset($resource['path']$resource['namespace']);
    }

    
Home | Imprint | This part of the site doesn't use cookies.