withContext example

$this->getPublicFilesystem()->writeStream(
            $this->urlGenerator->getRelativeMediaUrl($media),
            fopen(__DIR__ . '/../fixtures/shopware-logo.png', 'rb')
        );

        $msg = new GenerateThumbnailsMessage();
        $msg->setMediaIds([$media->getId()]);

        if (Feature::isActive('v6.6.0.0')) {
            $msg->setContext($this->context);
        } else {
            $msg->withContext($this->context);
        }

        $this->handler->__invoke($msg);

        $criteria = new Criteria([$media->getId()]);
        $criteria->addAssociation('thumbnails');

        /** @var MediaEntity $media */
        $media = $this->mediaRepository->search($criteria$this->context)->get($media->getId());
        $mediaThumbnailCollection = $media->getThumbnails();
        static::assertNotNull($mediaThumbnailCollection);
        

        $batchCount = 0;
        $this->io->comment('Generating batch jobs...');
        while (($result = $mediaIterator->fetch()) !== null) {
            $msg = new UpdateThumbnailsMessage();
            $msg->setIsStrict($this->isStrict);
            $msg->setMediaIds($result->getEntities()->getIds());

            if (Feature::isActive('v6.6.0.0')) {
                $msg->setContext($context);
            } else {
                $msg->withContext($context);
            }

            $this->messageBus->dispatch($msg);
            ++$batchCount;
        }
        $this->io->success(sprintf('Generated %d Batch jobs!', $batchCount));
    }
}
$dumper = \in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) ? new CliDumper() : new HtmlDumper();
        }

        if (!$dumper instanceof ServerDumper) {
            $dumper = new ContextualizedDumper($dumper[new SourceContextProvider()]);
        }

        self::$handler = function D$var, string $label = null) use ($cloner$dumper) {
            $var = $cloner->cloneVar($var);

            if (null !== $label) {
                $var = $var->withContext(['label' => $label]);
            }

            $dumper->dump($var);
        };
    }

    private static function getDefaultContextProviders(): array
    {
        $contextProviders = [];

        if (!\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) && class_exists(Request::class)) {
            
$newMedia = $this->getNewMediaEntities();

        $output = new BufferedOutput();

        $affectedMediaIds = [...array_combine($this->initialMediaIds, $this->initialMediaIds), ...$newMedia->getIds()];

        $expectedMessageStrict = new UpdateThumbnailsMessage();

        if (Feature::isActive('v6.6.0.0')) {
            $expectedMessageStrict->setContext($this->context);
        } else {
            $expectedMessageStrict->withContext($this->context);
        }

        $expectedMessageStrict->setIsStrict(true);
        $expectedMessageStrict->setMediaIds($affectedMediaIds);

        $expectedMessageNonStrict = new UpdateThumbnailsMessage();

        if (Feature::isActive('v6.6.0.0')) {
            $expectedMessageNonStrict->setContext($this->context);
        } else {
            $expectedMessageNonStrict->withContext($this->context);
        }
use Symfony\Component\HttpKernel\DataCollector\DumpDataCollector;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\VarDumper;

VarDumper::setHandler(function D$var, string $label = null) {
    $dumper = new DumpDataCollector();
    $cloner = new VarCloner();
    $handler = function D$var, string $label = null) use ($dumper$cloner) {
        $var = $cloner->cloneVar($var);
        if (null !== $label) {
            $var = $var->withContext(['label' => $label]);
        }

        $dumper->dump($var);
    };
    VarDumper::setHandler($handler);
    $handler($var$label);
});

$arrayObject = new \ArrayObject();
dump($arrayObject);
$arrayObject['X'] = 'A';
'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition' => ['privates', 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition', 'getCustomDefinitionService', false],
            'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestServiceSubscriber' => ['services', 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestServiceSubscriber', 'getTestServiceSubscriberService', false],
            'bar' => ['services', 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestServiceSubscriber', 'getTestServiceSubscriberService', false],
            'baz' => ['privates', 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition', 'getCustomDefinitionService', false],
            'late_alias' => ['services', 'late_alias', 'getLateAliasService', false],
        ][
            'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition' => 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition',
            'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestServiceSubscriber' => 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestServiceSubscriber',
            'bar' => 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition',
            'baz' => 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition',
            'late_alias' => 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestDefinition1',
        ]))->withContext('foo_service', $container));
    }

    /** * Gets the public 'late_alias' shared service. * * @return \Symfony\Component\DependencyInjection\Tests\Fixtures\TestDefinition1 */
    protected static function getLateAliasService($container)
    {
        return $container->services['late_alias'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\TestDefinition1();
    }

    
$mediaFile = new MediaFile($tempMeta['uri'], 'image/png', 'png', 0);

        $context = Context::createDefaultContext(new AdminApiSource(Uuid::randomHex()));
        $mediaId = Uuid::randomHex();

        $message = new GenerateThumbnailsMessage();
        $message->setMediaIds([$mediaId]);

        if (Feature::isActive('v6.6.0.0')) {
            $message->setContext($context);
        } else {
            $message->withContext($context);
        }

        $this->mediaRepository
            ->expects(static::once())
            ->method('update')
            ->with(static::callback(static fn (array $payload) => $payload[0]['id'] === $currentMedia->getId() && $payload[0]['fileName'] === 'foo'));

        $this->fileSaver->persistFileToMedia($mediaFile, 'foo', $mediaId$context);

        static::assertArrayHasKey(0, $this->messageBus->getMessages());
        static::assertEquals($message$this->messageBus->getMessages()[0]->getMessage());
    }
// This code is here to lazy load the dump stack. This default             // configuration is overridden in CLI mode on 'console.command' event.             // The dump data collector is used by default, so dump output is sent to             // the WDT. In a CLI context, if dump is used too soon, the data collector             // will buffer it, and release it at the end of the script.             VarDumper::setHandler(function D$var, string $label = null) use ($container) {
                $dumper = $container->get('data_collector.dump');
                $cloner = $container->get('var_dumper.cloner');
                $handler = function D$var, string $label = null) use ($dumper$cloner) {
                    $var = $cloner->cloneVar($var);
                    if (null !== $label) {
                        $var = $var->withContext(['label' => $label]);
                    }

                    $dumper->dump($var);
                };
                VarDumper::setHandler($handler);
                $handler($var$label);
            });
        }
    }

    /** * @return void */

  public function __construct(CacheableDependencyInterface $cacheability, ResourceType $resource_type$id$revision_id, array $fields, LinkCollection $links, LanguageInterface $language = NULL) {
    assert(is_null($revision_id) || $resource_type->isVersionable());
    $this->setCacheability($cacheability);
    $this->resourceType = $resource_type;
    $this->resourceIdentifier = new ResourceIdentifier($resource_type$id);
    $this->versionIdentifier = $revision_id ? 'id:' . $revision_id : NULL;
    $this->fields = $fields;
    $this->links = $links->withContext($this);

    // If the specified language empty it falls back the same way as in the entity system     // @see \Drupal\Core\Entity\EntityBase::language()     $this->language = $language ?: new Language(['id' => LanguageInterface::LANGCODE_NOT_SPECIFIED]);
  }

  /** * Creates a new ResourceObject from an entity. * * @param \Drupal\jsonapi\ResourceType\ResourceType $resource_type * The JSON:API resource type of the resource object. * @param \Drupal\Core\Entity\EntityInterface $entity * The entity to be represented by this resource object. * @param \Drupal\jsonapi\JsonApiResource\LinkCollection $links * (optional) Any links for the resource object, if a `self` link is not * provided, one will be automatically added if the resource is locatable * and is not an internal entity. * * @return static * An instantiated resource object. */
$context
        );

        $this->saveFileToMediaDir($mediaFile$media);

        $message = new GenerateThumbnailsMessage();
        $message->setMediaIds([$mediaId]);

        if (Feature::isActive('v6.6.0.0')) {
            $message->setContext($context);
        } else {
            $message->withContext($context);
        }

        $this->messageBus->dispatch($message);
    }

    public function renameMedia(string $mediaId, string $destination, Context $context): void
    {
        $destination = $this->validateFileName($destination);
        $currentMedia = $this->findMediaById($mediaId$context);
        $fileExtension = $currentMedia->getFileExtension();

        
$this->stopwatch?->start('dump');

        ['name' => $name, 'file' => $file, 'line' => $line, 'file_excerpt' => $fileExcerpt] = $this->sourceContextProvider->getContext();

        if (!$this->dumper || $this->dumper instanceof Connection && !$this->dumper->write($data)) {
            $this->isCollected = false;
        }

        $context = $data->getContext();
        $label = $context['label'] ?? '';
        unset($context['label']);
        $data = $data->withContext($context);

        if ($this->dumper && !$this->dumper instanceof Connection) {
            $this->doDump($this->dumper, $data$name$file$line$label);
        }

        if (!$this->dataCount) {
            $this->data = [];
        }
        $this->data[] = compact('data', 'name', 'file', 'line', 'fileExcerpt', 'label');
        ++$this->dataCount;

        
$dumper = \in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) ? new CliDumper() : new HtmlDumper();
        }

        if (!$dumper instanceof ServerDumper) {
            $dumper = new ContextualizedDumper($dumper[new SourceContextProvider()]);
        }

        self::$handler = function D$var, string $label = null) use ($cloner$dumper) {
            $var = $cloner->cloneVar($var);

            if (null !== $label) {
                $var = $var->withContext(['label' => $label]);
            }

            $dumper->dump($var);
        };
    }

    private static function getDefaultContextProviders(): array
    {
        $contextProviders = [];

        if (!\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) && class_exists(Request::class)) {
            
parent::testThrowsOnCircularReference();
    }

    public function testThrowsInServiceSubscriber()
    {
        $this->expectException(NotFoundExceptionInterface::class);
        $this->expectExceptionMessage('Service "foo" not found: even though it exists in the app\'s container, the container inside "caller" is a smaller service locator that only knows about the "bar" service. Unless you need extra laziness, try using dependency injection instead. Otherwise, you need to declare it using "SomeServiceSubscriber::getSubscribedServices()".');
        $container = new Container();
        $container->set('foo', new \stdClass());
        $subscriber = new SomeServiceSubscriber();
        $subscriber->container = $this->getServiceLocator(['bar' => function D) {}]);
        $subscriber->container = $subscriber->container->withContext('caller', $container);

        $subscriber->getFoo();
    }

    public function testGetThrowsServiceNotFoundException()
    {
        $this->expectException(ServiceNotFoundException::class);
        $this->expectExceptionMessage('Service "foo" not found: even though it exists in the app\'s container, the container inside "foo" is a smaller service locator that is empty... Try using dependency injection instead.');
        $container = new Container();
        $container->set('foo', new \stdClass());

        
use Symfony\Component\VarDumper\Dumper\CliDumper;
use Symfony\Component\VarDumper\Server\Connection;

/** * @author Nicolas Grekas <p@tchwork.com> */
class DumpDataCollectorTest extends TestCase
{
    public function testDump()
    {
        $data = new Data([[123]]);
        $data = $data->withContext(['label' => 'foo']);

        $collector = new DumpDataCollector(null, new FileLinkFormatter([]));

        $this->assertSame('dump', $collector->getName());

        $collector->dump($data);
        $line = __LINE__ - 1;
        $this->assertSame(1, $collector->getDumpsCount());

        $dump = $collector->getDumps('html');
        $this->assertArrayHasKey('data', $dump[0]);
        

  protected function __construct($public_field_name, RelationshipData $data, LinkCollection $links, array $meta, ResourceObject $context) {
    $this->fieldName = $public_field_name;
    $this->data = $data;
    $this->links = $links->withContext($this);
    $this->meta = $meta;
    $this->context = $context;
  }

  /** * Creates a new Relationship from an entity reference field. * * @param \Drupal\jsonapi\JsonApiResource\ResourceObject $context * The context resource object of the relationship to be created. * @param \Drupal\Core\Field\EntityReferenceFieldItemListInterface $field * The entity reference field from which to create the relationship. * @param \Drupal\jsonapi\JsonApiResource\LinkCollection $links * (optional) Any extra links for the Relationship, if a `self` link is not * provided, one will be automatically added if the context resource is * locatable and is not internal. * @param array $meta * (optional) Any relationship metadata. * * @return static * An instantiated relationship object. */
Home | Imprint | This part of the site doesn't use cookies.