listContents example

$filesystem,
            $filesystem,
            $this->createMock(KernelInterface::class),
            $this->createMock(KernelPluginLoader::class),
            $this->createMock(CacheInvalidator::class),
            $this->createMock(AbstractAppLoader::class),
            new ParameterBag(['shopware.filesystem.asset.type' => 's3'])
        );

        $assetService->copyAssetsFromApp('TestApp', __DIR__ . '/foo');

        static::assertEmpty($filesystem->listContents('bundles')->toArray());
    }

    public function testCopyAssetsWithApp(): void
    {
        $filesystem = new Filesystem(new MemoryFilesystemAdapter());

        $appLoader = $this->createMock(AbstractAppLoader::class);
        $appLoader
            ->method('locatePath')
            ->with(__DIR__ . '/_fixtures/ExampleBundle', 'Resources/public')
            ->willReturn(__DIR__ . '/../_fixtures/ExampleBundle/Resources/public');

        
use FilesystemBehaviour;
    use KernelTestBehaviour;

    public function testWrittenFilesGetDeleted(): void
    {
        $this->getPublicFilesystem()
            ->write('testFile', 'testContent');

        $this->getPublicFilesystem()
            ->write('public/testFile', 'testContent');

        static::assertNotEmpty($this->getPublicFilesystem()->listContents('', true)->toArray());
    }

    /** * @depends testWrittenFilesGetDeleted */
    public function testFileSystemIsEmptyOnNextTest(): void
    {
        static::assertEmpty($this->getPublicFilesystem()->listContents('', true)->toArray());
    }
}
/** @var ImportExportFileEntity $file */
        $file = $logEntity->getFile();
        $target = $file->getPath();

        $dir = \dirname($target);

        $partFilePrefix = $target . self::PART_FILE_SUFFIX;

        $partFiles = [];

        foreach ($this->filesystem->listContents($dir) as $meta) {
            if ($meta->type() !== 'file'
                || $meta->path() === $target
                || !str_starts_with($meta->path()$partFilePrefix)) {
                continue;
            }

            $partFiles[] = $meta->path();
        }

        // sort by offset         natsort($partFiles);

        
gzwrite($this->handle, $sitemapOpenTagEvent->getFullOpenTag());
    }

    private function printFooter(): void
    {
        gzwrite($this->handle, '</urlset>');
    }

    private function cleanUp(): void
    {
        try {
            $files = $this->filesystem->listContents($this->getPath($this->context));
        } catch (\Throwable) {
            // Folder does not exists             return;
        }

        foreach ($files as $file) {
            $this->filesystem->delete($file->path());
        }
    }

    private function setDomainName(?string $domain = null): void
    {
public function __construct(FilesystemInterface $filesystem, PublicUrlGeneratorInterface $publicUrlGenerator)
    {
        $this->filesystem = $filesystem;
        $this->publicUrlGenerator = $publicUrlGenerator;
    }

    /** * {@inheritdoc} */
    public function getSitemaps($shopId)
    {
        $files = $this->filesystem->listContents('shop-' . $shopId);
        $sitemaps = [];

        foreach ($files as $file) {
            if ($file['basename'][0] === '.') {
                continue;
            }

            $path = $this->publicUrlGenerator->generateUrl('sitemap/shop-' . $shopId . '/' . $file['basename']);

            $sitemaps[] = new Sitemap(
                $path,
                
$salesChannel = $this->salesChannelRepository->search($this->storefontSalesChannelCriteria([$this->context->getSalesChannelId()])$this->context->getContext())->first();

        $languageIds = $salesChannel->getDomains()->map(fn (SalesChannelDomainEntity $salesChannelDomain) => $salesChannelDomain->getLanguageId());

        $languageIds = array_unique($languageIds);

        foreach ($languageIds as $languageId) {
            $salesChannelContext = $this->getContainer()->get(SalesChannelContextFactory::class)->create('', $salesChannel->getId()[SalesChannelContextService::LANGUAGE_ID => $languageId]);

            $this->generateSitemap($salesChannelContext, false);

            $files = $this->getFilesystem('shopware.filesystem.sitemap')->listContents('sitemap/salesChannel-' . $salesChannel->getId() . '-' . $salesChannelContext->getLanguageId());

            static::assertCount(1, $files);
        }

        static::assertTrue(true);
    }

    private function createCacheItem($key$value$isHit): CacheItemInterface
    {
        $class = new \ReflectionClass(CacheItem::class);
        $keyProp = $class->getProperty('key');
        
$this->setVisibility($this->filesystemSitemap, $style, 'public');
        $style->comment('Setting visibility to public in asset bucket.');
        $this->setVisibility($this->filesystemAsset, $style, 'public');

        $style->info('Finished setting visibility of objects in all pre-defined buckets.');

        return Command::SUCCESS;
    }

    private function setVisibility(FilesystemOperator $filesystem, ShopwareStyle $style, string $visibility): void
    {
        $files = array_filter($filesystem->listContents('/', true)->toArray()fn (StorageAttributes $object): bool => $object->type() === 'file');
        ProgressBar::setFormatDefinition('custom', '[%bar%] %current%/%max% -- %message%');
        $progressBar = new ProgressBar($style, \count((array) $files));
        $progressBar->setFormat('custom');
        $progressBar->setMessage('');

        foreach ($files as $file) {
            $filesystem->setVisibility($file->path()$visibility);

            $progressBar->advance();
            $progressBar->setMessage($file->path());
        }

        
static::assertFalse($prefix->directoryExists('dir'));

        $prefix->move('bla.txt', 'bla2.txt');
        static::assertFalse($prefix->has('bla.txt'));
        static::assertTrue($prefix->has('bla2.txt'));

        $prefix->delete('bla2.txt');
        static::assertFalse($prefix->has('bla2.txt'));

        $prefix->createDirectory('test');

        $files = $prefix->listContents('', true)->toArray();
        static::assertCount(2, $files);
    }

    public function testWriteStream(): void
    {
        $fs = new Filesystem(new MemoryFilesystemAdapter());
        $prefix = new PrefixFilesystem($fs, 'foo');
        $tmpFile = sys_get_temp_dir() . '/' . uniqid('test', true);
        file_put_contents($tmpFile, 'test');

        $prefix->writeStream('a.txt', fopen($tmpFile, 'rb'));

        
public function __construct(
        private readonly FilesystemOperator $filesystem,
        private readonly Package $package
    ) {
    }

    /** * {@inheritdoc} */
    public function getSitemaps(SalesChannelContext $salesChannelContext): array
    {
        $files = $this->filesystem->listContents('sitemap/salesChannel-' . $salesChannelContext->getSalesChannel()->getId() . '-' . $salesChannelContext->getLanguageId());

        $sitemaps = [];

        foreach ($files as $file) {
            if ($file->isDir()) {
                continue;
            }

            $sitemaps[] = new Sitemap($this->package->getUrl($file->path()), 0, new \DateTime('@' . ($file->lastModified() ?? time())));
        }

        
public function getAdapterType()
    {
        return $this->config['type'];
    }

    /** * {@inheritdoc} */
    public function listFiles($directory = '')
    {
        $files = [];
        foreach ($this->filesystem->listContents($directory, true) as $file) {
            if ($file['type'] === 'dir' || strstr($file['path'], '/.') !== false) {
                continue;
            }

            $files[] = $file['path'];
        }

        return $files;
    }

    /** * {@inheritdoc} */
gzwrite($fileHandler$content);
    }

    /** * Makes sure all files get closed and replaces the old sitemaps with the freshly generated ones */
    private function moveFiles()
    {
        /** @var Sitemap[] $sitemaps */
        foreach ($this->sitemaps as $shopId => $sitemaps) {
            // Delete old sitemaps for this siteId             foreach ($this->filesystem->listContents(sprintf('shop-%d', $shopId)) as $file) {
                $this->filesystem->delete($file['path']);
            }

            // Move new sitemaps into place             foreach ($sitemaps as $sitemap) {
                $sitemapFileName = $this->sitemapNameGenerator->getSitemapFilename($shopId);
                try {
                    $this->filesystem->write($sitemapFileNamefile_get_contents($sitemap->getFilename()));
                } catch (\League\Flysystem\Exception $exception) {
                    $this->logger->error(sprintf('Could not move sitemap to "%s" in the location for sitemaps', $sitemapFileName));
                } finally {
                    
if (!$io->confirm(sprintf('Found %d orphaned thumbnails. Are you sure you want to delete the files? This step is irreversible.', \count($thumbnailFiles)))) {
            return;
        }

        $deletedThumbnails = $this->deleteThumbnails($io$filesystem$thumbnailFiles);

        $io->success(sprintf('Removed %d/%d orphaned thumbnails.', $deletedThumbnails, \count($thumbnailFiles)));
    }

    private function processFilesIn(string $directory, FilesystemInterface $filesystem, ProgressBar $progressBar): void
    {
        $contents = $filesystem->listContents($directory);

        foreach ($contents as $item) {
            if ($item['type'] === 'dir') {
                $this->processFilesIn($item['path']$filesystem$progressBar);
            }

            if ($item['type'] === 'file') {
                if (str_starts_with($item['basename'], '.')) {
                    continue;
                }

                
static::assertTrue($fs->fileExists('foo.txt'));

        static::expectException(UnableToCopyFile::class);
        $fs->copy('blaa', 'blaa.txt');
    }

    public function testListing(): void
    {
        $fs = new Filesystem(new MemoryFilesystemAdapter());
        $fs->write('a/b/c/d/e/f/bla.txt', 'foo');

        $files = $fs->listContents('', true)->toArray();
        static::assertCount(7, $files);

        /** @var DirectoryAttributes[] $firstLevel */
        $firstLevel = $fs->listContents('', false)->toArray();
        static::assertCount(1, $firstLevel);
        static::assertTrue($firstLevel[0]->isDir());
        static::assertSame('a', $firstLevel[0]->path());
    }
}
$toFileSystem->writeStream($path$contents);

        return $toFileSystem->has($path);
    }

    /** * @param string $directory */
    private function migrateFilesIn($directory, MediaServiceInterface $fromFilesystem, MediaServiceInterface $toFilesystem, ProgressBar $progressBar)
    {
        /** @var array $contents */
        $contents = $fromFilesystem->getFilesystem()->listContents($directory);

        foreach ($contents as $item) {
            if ($item['type'] === 'dir') {
                $this->migrateFilesIn($item['path']$fromFilesystem$toFilesystem$progressBar);
                continue;
            }

            if ($item['type'] === 'file') {
                if (strpos($item['basename'], '.') === 0) {
                    continue;
                }

                

        return $this->inner->lastModified($path);
    }

    public function fileSize(string $path): FileAttributes
    {
        return $this->inner->fileSize($path);
    }

    public function listContents(string $path, bool $deep): iterable
    {
        return $this->inner->listContents($path$deep);
    }

    public function move(string $source, string $destination, Config $config): void
    {
        $this->inner->move($source$destination$config);
    }

    public function copy(string $source, string $destination, Config $config): void
    {
        $this->inner->copy($source$destination$config);
    }

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