File example

$addSourceFile = $configuration->getStorefrontEntryFilepath() && $onlySourceFiles;

                    // add source file at the beginning if no other theme is included first                     if (
                        $addSourceFile
                        && (
                            $scriptFiles->count() === 0
                            || !$this->isInclude($scriptFiles->first()->getFilepath())
                        )
                        && $configuration->getStorefrontEntryFilepath()
                    ) {
                        $fileCollection->add(new File($configuration->getStorefrontEntryFilepath()));
                    }
                    foreach ($scriptFiles as $scriptFile) {
                        if (!$this->isInclude($scriptFile->getFilepath()) && $onlySourceFiles) {
                            continue;
                        }
                        $fileCollection->add($scriptFile);
                    }
                    if (
                        $addSourceFile
                        && $scriptFiles->count() > 0
                        && $this->isInclude($scriptFiles->first()->getFilepath())
                        
use Shopware\Core\Content\Sitemap\Service\ConfigHandler;
use Shopware\Core\Framework\Log\Package;

/** * @internal */
#[Package('sales-channel')] class FileTest extends TestCase
{
    public function testAddLastModDate(): void
    {
        $fileConfigHandler = new File([
            ConfigHandler::EXCLUDED_URLS_KEY => [],
            ConfigHandler::CUSTOM_URLS_KEY => [
                [
                    'url' => 'foo',
                    'changeFreq' => 'weekly',
                    'priority' => 0.5,
                    'salesChannelId' => 2,
                    'lastMod' => '2019-09-27 10:00:00',
                ],
            ],
        ]);

        
public function testSendInvalidMessage()
    {
        $stream = new DummyStream();

        $transport = new SmtpTransport($stream);
        $transport->setPingThreshold(1);

        $message = new Email();
        $message->to('recipient@example.org');
        $message->from('sender@example.org');
        $message->addPart(new DataPart(new File('/does_not_exists')));

        try {
            $transport->send($message);
            $this->fail('Expected Symfony\Component\Mime\Exception\InvalidArgumentException to be thrown');
        } catch (InvalidArgumentException $e) {
            $this->assertMatchesRegularExpression('{Path "/does_not_exists"}i', $e->getMessage());
        }

        $this->assertNotContains("\r\n.\r\n", $stream->getCommands());
        $this->assertTrue($stream->isClosed());
    }

    
public function testFile()
    {
        $container = new Container();
        $kernel = $this->createMock(HttpKernelInterface::class);
        $container->set('http_kernel', $kernel);

        $controller = $this->createController();
        $controller->setContainer($container);

        /* @var BinaryFileResponse $response */
        $response = $controller->file(new File(__FILE__));
        $this->assertInstanceOf(BinaryFileResponse::class$response);
        $this->assertSame(200, $response->getStatusCode());
        if ($response->headers->get('content-type')) {
            $this->assertSame('text/x-php', $response->headers->get('content-type'));
        }
        $this->assertStringContainsString(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $response->headers->get('content-disposition'));
        $this->assertStringContainsString(basename(__FILE__)$response->headers->get('content-disposition'));
    }

    public function testFileAsInline()
    {
        
parent::__construct($body, null, $subtype$encoding);

        if (null !== $filename) {
            $this->filename = $filename;
            $this->setName($filename);
        }
        $this->setDisposition('attachment');
    }

    public static function fromPath(string $path, string $name = null, string $contentType = null): self
    {
        return new self(new File($path)$name$contentType);
    }

    /** * @return $this */
    public function asInline()static
    {
        return $this->setDisposition('inline');
    }

    /** * @return $this */
/** * Sets the file to stream. * * @return $this * * @throws FileException */
    public function setFile(\SplFileInfo|string $file, string $contentDisposition = null, bool $autoEtag = false, bool $autoLastModified = true)static
    {
        if (!$file instanceof File) {
            if ($file instanceof \SplFileInfo) {
                $file = new File($file->getPathname());
            } else {
                $file = new File((string) $file);
            }
        }

        if (!$file->isReadable()) {
            throw new FileException('File must be readable.');
        }

        $this->file = $file;

        

    public function attach($body, string $name = null, string $contentType = null)static
    {
        return $this->addPart(new DataPart($body$name$contentType));
    }

    /** * @return $this */
    public function attachFromPath(string $path, string $name = null, string $contentType = null)static
    {
        return $this->addPart(new DataPart(new File($path)$name$contentType));
    }

    /** * @param resource|string $body * * @return $this */
    public function embed($body, string $name = null, string $contentType = null)static
    {
        return $this->addPart((new DataPart($body$name$contentType))->asInline());
    }

    
use Symfony\Component\Validator\Constraints\File;

class FileValidatorPathTest extends FileValidatorTestCase
{
    protected function getFile($filename)
    {
        return $filename;
    }

    public function testFileNotFound()
    {
        $constraint = new File([
            'notFoundMessage' => 'myMessage',
        ]);

        $this->validator->validate('foobar', $constraint);

        $this->buildViolation('myMessage')
            ->setParameter('{{ file }}', '"foobar"')
            ->setCode(File::NOT_FOUND_ERROR)
            ->assertRaised();
    }
}
$att = new DataPart($file = fopen($name, 'r'), 'test');
        $inline = (new DataPart($contents, 'test'))->asInline();
        $e = new Email();
        $e->addPart(new DataPart($file, 'test'));
        $e->addPart((new DataPart($contents, 'test'))->asInline());
        $this->assertEquals([$att$inline]$e->getAttachments());

        // inline part from path         $att = DataPart::fromPath($name, 'test');
        $inline = DataPart::fromPath($name, 'test')->asInline();
        $e = new Email();
        $e->addPart(new DataPart(new File($name)));
        $e->addPart((new DataPart(new File($name)))->asInline());
        $this->assertEquals([$att->bodyToString()$inline->bodyToString()]array_map(fn (DataPart $a) => $a->bodyToString()$e->getAttachments()));
        $this->assertEquals([$att->getPreparedHeaders()$inline->getPreparedHeaders()]array_map(fn (DataPart $a) => $a->getPreparedHeaders()$e->getAttachments()));
    }

    public function testSerialize()
    {
        $r = fopen('php://memory', 'r+', false);
        fwrite($r, 'Text content');
        rewind($r);

        
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
use Symfony\Component\HttpFoundation\File\File;

/** * @requires extension fileinfo */
class FileTest extends TestCase
{
    public function testGetMimeTypeUsesMimeTypeGuessers()
    {
        $file = new File(__DIR__.'/Fixtures/test.gif');
        $this->assertEquals('image/gif', $file->getMimeType());
    }

    public function testGuessExtensionWithoutGuesser()
    {
        $file = new File(__DIR__.'/Fixtures/directory/.empty');
        $this->assertNull($file->guessExtension());
    }

    public function testGuessExtensionIsBasedOnMimeType()
    {
        
/** * Yields as an Iterator for the current files. * Fulfills IteratorAggregate. * * @return Generator<File> * * @throws FileNotFoundException */
    public function getIterator(): Generator
    {
        foreach ($this->get() as $file) {
            yield new File($file, true);
        }
    }
}


    /** * @param string $image A Twig path to the image file. It's recommended to define * some Twig namespace for email images (e.g. '@email/images/logo.png'). * @param string|null $contentType The media type (i.e. MIME type) of the image file (e.g. 'image/png'). * Some email clients require this to display embedded images. */
    public function image(string $image, string $contentType = null): string
    {
        $file = $this->twig->getLoader()->getSourceContext($image);
        $body = $file->getPath() ? new File($file->getPath()) : $file->getCode();
        $this->message->addPart((new DataPart($body$image$contentType))->asInline());

        return 'cid:'.$image;
    }

    /** * @param string $file A Twig path to the file. It's recommended to define * some Twig namespace for email files (e.g. '@email/files/contract.pdf'). * @param string|null $name A custom file name that overrides the original name of the attached file * @param string|null $contentType The media type (i.e. MIME type) of the file (e.g. 'application/pdf'). * Some email clients require this to display attached files. */
/** * @extends Collection<File> */
#[Package('storefront')] class FileCollection extends Collection
{
    public static function createFromArray(array $files)
    {
        $collection = new self();
        foreach ($files as $file) {
            $collection->add(new File($file));
        }

        return $collection;
    }

    public function getFilepaths(): array
    {
        return $this->map(fn (File $element) => $element->getFilepath());
    }

    public function getResolveMappings(): array
    {
public const TESTED_TYPE = 'Symfony\Component\Form\Extension\Core\Type\FileType';

    protected function getExtensions()
    {
        return array_merge(parent::getExtensions()[new CoreExtension(null, null, new IdentityTranslator())]);
    }

    // https://github.com/symfony/symfony/pull/5028     public function testSetData()
    {
        $form = $this->factory->createBuilder(static::TESTED_TYPE)->getForm();
        $data = new File(__DIR__.'/../../../Fixtures/foo', false);

        $form->setData($data);

        // Ensures the data class is defined to accept File instance         $this->assertSame($data$form->getData());
    }

    /** * @dataProvider requestHandlerProvider */
    public function testSubmit(RequestHandlerInterface $requestHandler)
    {
public function testSupportNormalization()
    {
        $this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
        $this->assertTrue($this->normalizer->supportsNormalization(new \SplFileObject('data:,Hello%2C%20World!')));
    }

    /** * @requires extension fileinfo */
    public function testNormalizeHttpFoundationFile()
    {
        $file = new File(__DIR__.'/../Fixtures/test.gif');

        $this->assertSame(self::TEST_GIF_DATA, $this->normalizer->normalize($file));
    }

    /** * @requires extension fileinfo */
    public function testNormalizeSplFileInfo()
    {
        $file = new \SplFileInfo(__DIR__.'/../Fixtures/test.gif');

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