FileExistenceResource example


    public function fileExists(string $path, bool|string $trackContents = true): bool
    {
        $exists = file_exists($path);

        if (!$this->trackResources || $this->inVendors($path)) {
            return $exists;
        }

        if (!$exists) {
            $this->addResource(new FileExistenceResource($path));

            return $exists;
        }

        if (is_dir($path)) {
            if ($trackContents) {
                $this->addResource(new DirectoryResource($path, \is_string($trackContents) ? $trackContents : null));
            } else {
                $this->addResource(new GlobResource($path, '/*', false));
            }
        } elseif ($trackContents) {
            


            if ($paths) {
                // the last path must be the bundle views directory                 $twigFilesystemLoaderDefinition->addMethodCall('addPath', [$path, '!'.$namespace]);
            }
        }

        if (file_exists($defaultTwigPath)) {
            $twigFilesystemLoaderDefinition->addMethodCall('addPath', [$defaultTwigPath]);
        }
        $container->addResource(new FileExistenceResource($defaultTwigPath));

        if (!empty($config['globals'])) {
            $def = $container->getDefinition('twig');
            foreach ($config['globals'] as $key => $global) {
                if (isset($global['type']) && 'service' === $global['type']) {
                    $def->addMethodCall('addGlobal', [$keynew Reference($global['id'])]);
                } else {
                    $def->addMethodCall('addGlobal', [$key$global['value']]);
                }
            }
        }

        
/** @var Translator $translator */
        $translator = $this->getTranslator($loader[
            'resource_files' => $resourceFiles,
            'scanned_directories' => [__DIR__, '/tmp/I/sure/hope/this/does/not/exist'],
        ], 'yml');

        $catalogue = $translator->getCatalogue('fr');

        $resources = $catalogue->getResources();

        $this->assertEquals(new DirectoryResource(__DIR__)$resources[1]);
        $this->assertEquals(new FileExistenceResource('/tmp/I/sure/hope/this/does/not/exist')$resources[2]);
    }

    public function testCachedCatalogueIsReDumpedWhenScannedDirectoriesChange()
    {
        /** @var Translator $translator */
        $translator = $this->getTranslator(new YamlFileLoader()[
            'cache_dir' => $this->tmpDir,
            'resource_files' => [
                'fr' => [
                    __DIR__.'/../Fixtures/Resources/translations/messages.fr.yml',
                ],
            ],

        if (!isset($this->collection)) {
            $this->collection = $this->container->get('routing.loader')->load($this->resource, $this->options['resource_type']);
            $this->resolveParameters($this->collection);
            $this->collection->addResource(new ContainerParametersResource($this->collectedParameters));

            try {
                $containerFile = ($this->paramFetcher)('kernel.cache_dir').'/'.($this->paramFetcher)('kernel.container_class').'.php';
                if (file_exists($containerFile)) {
                    $this->collection->addResource(new FileResource($containerFile));
                } else {
                    $this->collection->addResource(new FileExistenceResource($containerFile));
                }
            } catch (ParameterNotFoundException) {
            }
        }

        return $this->collection;
    }

    /** * @return string[] A list of classes to preload on PHP 7.4+ */
    


        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;
        }
        $resource = new GlobResource($prefix$pattern$recursive$forExclusion$excluded);

        yield from $resource;
    }

    private function doImport(mixed $resource, string $type = null, bool $ignoreErrors = false, string $sourceResource = null): mixed
    {
        
class FileExistenceResourceTest extends TestCase
{
    protected FileExistenceResource $resource;
    protected string $file;
    protected int $time;

    protected function setUp(): void
    {
        $this->file = realpath(sys_get_temp_dir()).'/tmp.xml';
        $this->time = time();
        $this->resource = new FileExistenceResource($this->file);
    }

    protected function tearDown(): void
    {
        if (file_exists($this->file)) {
            @unlink($this->file);
        }
    }

    public function testToString()
    {
        
Home | Imprint | This part of the site doesn't use cookies.