hasExtension example

$product['manyToOne'] = [
            'id' => $extendableId,
        ];
        $productId = $product['id'];
        $this->productRepository->create([$product]$this->context);

        $criteria = (new Criteria([$productId]))->addAssociation('manyToOne');
        /** @var ProductEntity|null $product */
        $product = $this->productRepository->search($criteria$this->context)->first();
        static::assertNotNull($product);

        static::assertTrue($product->hasExtension('manyToOne'));
        /** @var ArrayEntity $extension */
        $extension = $product->getExtension('manyToOne');

        static::assertInstanceOf(ArrayEntity::class$extension);
        static::assertEquals($extendableId$extension->get('id'));

        $criteria = (new Criteria())->addFilter(new EqualsFilter('manyToOne.id', $extendableId));

        $products = $this->productRepository->searchIds($criteria$this->context);
        static::assertTrue($products->has($productId));

        
$this->productRepository->create([$data], Context::createDefaultContext());

        $criteria = new Criteria([$id]);
        $criteria->addAssociation('myTax');

        /** @var ProductEntity|null $product */
        $product = $this->productRepository->search($criteria, Context::createDefaultContext())->first();

        static::assertInstanceOf(ProductEntity::class$product);

        static::assertTrue($product->hasExtension('myTax'));

        /** @var TaxEntity $tax */
        $tax = $product->getExtension('myTax');
        static::assertInstanceOf(TaxEntity::class$tax);

        static::assertSame('my-tax', $tax->getName());

        $this->connection->rollBack();

        $this->connection->executeStatement('ALTER TABLE `product` DROP COLUMN my_tax_id');

        


    public function initialize(): void
    {
        // use private twig instance here, because we use custom template loader         $this->twig = new TwigEnvironment(new ArrayLoader()[
            'cache' => new FilesystemCache($this->cacheDir . '/string-template-renderer'),
        ]);

        $this->disableTestMode();
        foreach ($this->platformTwig->getExtensions() as $extension) {
            if ($this->twig->hasExtension($extension::class)) {
                continue;
            }
            $this->twig->addExtension($extension);
        }
        if ($this->twig->hasExtension(CoreExtension::class) && $this->platformTwig->hasExtension(CoreExtension::class)) {
            /** @var CoreExtension $coreExtensionInternal */
            $coreExtensionInternal = $this->twig->getExtension(CoreExtension::class);
            /** @var CoreExtension $coreExtensionGlobal */
            $coreExtensionGlobal = $this->platformTwig->getExtension(CoreExtension::class);

            $coreExtensionInternal->setTimezone($coreExtensionGlobal->getTimezone());
            
if ($this->hasNode('arguments')) {
            $compiler->raw(', ')->subcompile($this->getNode('arguments'));
        } else {
            $compiler->raw(', []');
        }

        $compiler->raw(', ')
            ->repr($this->getAttribute('type'))
            ->raw(', ')->repr($this->getAttribute('is_defined_test'))
            ->raw(', ')->repr($this->getAttribute('ignore_strict_check'))
            ->raw(', ')->repr($env->hasExtension(SandboxExtension::class))
            ->raw(', ')->repr($this->getNode('node')->getTemplateLine())
            ->raw(')')
        ;
    }
}
$message->setIndexer($indexer->getName());
            self::addSkips($message$context);

            $this->sendOrHandle($message$useQueue);
        }

        $this->working = false;
    }

    public static function addSkips(EntityIndexingMessage $message, Context $context): void
    {
        if (!$context->hasExtension(self::EXTENSION_INDEXER_SKIP)) {
            return;
        }
        /** @var ArrayEntity $skip */
        $skip = $context->getExtension(self::EXTENSION_INDEXER_SKIP);

        $message->addSkip(...$skip->get('skips'));
    }

    /** * @param list<string> $indexer * @param list<string> $skip */

                'productId' => $this->ids->get('p1'),
                'page' => $page,
            ],
            [
                RepositoryFacadeHookFactory::class,
            ]
        );

        $this->getContainer()->get(ScriptExecutor::class)->execute($hook);

        static::assertTrue($page->hasExtension('myProduct'));
        $product = $page->getExtension('myProduct');
        static::assertInstanceOf(ProductEntity::class$product);
        static::assertEquals($this->ids->get('p1')$product->getId());
    }

    public function testSearchWithFilterIntegration(): void
    {
        $this->ids = new IdsCollection();
        $this->createProducts();

        $this->installApp(__DIR__ . '/_fixtures/apps/pageLoadedExample');

        
        if (!str_starts_with($namespace, 'Symfony\\Config\\')) {
            throw new InvalidArgumentException(sprintf('Could not find or generate class "%s".', $namespace));
        }

        // Try to get the extension alias         $alias = Container::underscore(substr($namespace, 15, -6));

        if (str_contains($alias, '\\')) {
            throw new InvalidArgumentException('You can only use "root" ConfigBuilders from "Symfony\\Config\\" namespace. Nested classes like "Symfony\\Config\\Framework\\CacheConfig" cannot be used.');
        }

        if (!$this->container->hasExtension($alias)) {
            $extensions = array_filter(array_map(fn (ExtensionInterface $ext) => $ext->getAlias()$this->container->getExtensions()));
            throw new InvalidArgumentException(sprintf('There is no extension able to load the configuration for "%s". Looked for namespace "%s", found "%s".', $namespace$alias$extensions ? implode('", "', $extensions) : 'none'));
        }

        $extension = $this->container->getExtension($alias);
        if (!$extension instanceof ConfigurationExtensionInterface) {
            throw new \LogicException(sprintf('You cannot use the config builder for "%s" because the extension does not implement "%s".', $namespace, ConfigurationExtensionInterface::class));
        }

        $configuration = $extension->getConfiguration([]$this->container);
        $loader = $this->generator->build($configuration);

        

    public function get(string $property)
    {
        if (FieldVisibility::$isInTwigRenderingContext) {
            $this->checkIfPropertyAccessIsAllowed($property);
        }

        if ($this->has($property)) {
            return $this->$property; /* @phpstan-ignore-line */
        }

        if ($this->hasExtension($property)) {
            return $this->getExtension($property);
        }

        /** @var ArrayStruct<string, mixed>|null $extension */
        $extension = $this->getExtension('foreignKeys');
        if ($extension && $extension instanceof ArrayStruct && $extension->has($property)) {
            return $extension->get($property);
        }

        throw new \InvalidArgumentException(
            sprintf('Property %s do not exist in class %s', $propertystatic::class)
        );
Context::createDefaultContext(),
            [
                'page' => $page,
            ],
            [
                SystemConfigFacadeHookFactory::class,
            ]
        );

        $this->getContainer()->get(ScriptExecutor::class)->execute($hook);

        static::assertTrue($page->hasExtension('systemConfigExtension'));
        $extension = $page->getExtension('systemConfigExtension');
        static::assertInstanceOf(ArrayStruct::class$extension);

        static::assertEquals('system_config', $extension->get('systemConfig'));
        static::assertEquals('app_config', $extension->get('appConfig'));
    }

    private function installApp(string $appDir): ScriptAppInformation
    {
        $this->loadAppsFromDir($appDir);

        
        if (!str_starts_with($namespace, 'Symfony\\Config\\')) {
            throw new InvalidArgumentException(sprintf('Could not find or generate class "%s".', $namespace));
        }

        // Try to get the extension alias         $alias = Container::underscore(substr($namespace, 15, -6));

        if (str_contains($alias, '\\')) {
            throw new InvalidArgumentException('You can only use "root" ConfigBuilders from "Symfony\\Config\\" namespace. Nested classes like "Symfony\\Config\\Framework\\CacheConfig" cannot be used.');
        }

        if (!$this->container->hasExtension($alias)) {
            $extensions = array_filter(array_map(fn (ExtensionInterface $ext) => $ext->getAlias()$this->container->getExtensions()));
            throw new InvalidArgumentException(sprintf('There is no extension able to load the configuration for "%s". Looked for namespace "%s", found "%s".', $namespace$alias$extensions ? implode('", "', $extensions) : 'none'));
        }

        $extension = $this->container->getExtension($alias);
        if (!$extension instanceof ConfigurationExtensionInterface) {
            throw new \LogicException(sprintf('You cannot use the config builder for "%s" because the extension does not implement "%s".', $namespace, ConfigurationExtensionInterface::class));
        }

        $configuration = $extension->getConfiguration([]$this->container);
        $loader = $this->generator->build($configuration);

        
$context->setConsiderInheritance(true);

        $criteria = new Criteria($ids->getList(['v1', 'v2']));
        $criteria->addAssociation('customEntityBlogInheritedProducts');

        $products = $container->get('product.repository')->search($criteria$context);

        static::assertCount(2, $products);
        $v1 = $products->get($ids->get('v1'));

        static::assertInstanceOf(ProductEntity::class$v1);
        static::assertTrue($v1->hasExtension('customEntityBlogInheritedProducts'));
        $inheritedProductsExtension = $v1->getExtension('customEntityBlogInheritedProducts');
        static::assertInstanceOf(EntityCollection::class$inheritedProductsExtension);
        static::assertCount(1, $inheritedProductsExtension);
        $blog = $inheritedProductsExtension->first();
        static::assertInstanceOf(ArrayEntity::class$blog);
        static::assertEquals($blog1['id']$blog->getId());

        $v2 = $products->get($ids->get('v2'));
        static::assertInstanceOf(ProductEntity::class$v2);
        static::assertTrue($v2->hasExtension('customEntityBlogInheritedProducts'));
        $inheritedProductsExtension = $v2->getExtension('customEntityBlogInheritedProducts');
        
private $defaultStrategy = false;
    private $safeVars = [];

    public function __construct()
    {
        $this->safeAnalysis = new SafeAnalysisNodeVisitor();
    }

    public function enterNode(Node $node, Environment $env): Node
    {
        if ($node instanceof ModuleNode) {
            if ($env->hasExtension(EscaperExtension::class) && $defaultStrategy = $env->getExtension(EscaperExtension::class)->getDefaultStrategy($node->getTemplateName())) {
                $this->defaultStrategy = $defaultStrategy;
            }
            $this->safeVars = [];
            $this->blocks = [];
        } elseif ($node instanceof AutoEscapeNode) {
            $this->statusStack[] = $node->getAttribute('value');
        } elseif ($node instanceof BlockNode) {
            $this->statusStack[] = isset($this->blocks[$node->getAttribute('name')]) ? $this->blocks[$node->getAttribute('name')] : $this->needEscaping($env);
        } elseif ($node instanceof ImportNode) {
            $this->safeVars[] = $node->getNode('var')->getAttribute('name');
        }

        
public function manualSetter(EntityRepository $categoryRepository): void
    {
        $this->categoryRepository = $categoryRepository;
    }

    public function uninstall(UninstallContext $uninstallContext): void
    {
    }

    public function update(UpdateContext $updateContext): void
    {
        if ($updateContext->getContext()->hasExtension(self::THROW_ERROR_ON_UPDATE)) {
            throw new \BadMethodCallException('Update throws an error');
        }

        parent::update($updateContext);
    }

    public function deactivate(DeactivateContext $deactivateContext): void
    {
        if ($deactivateContext->getContext()->hasExtension(self::THROW_ERROR_ON_DEACTIVATE)) {
            throw new \BadFunctionCallException('Deactivate throws an error');
        }
        
private function isComplete(LineItem $lineItem): bool
    {
        return $lineItem->getPriceDefinition() !== null
            && $lineItem->getLabel() !== null
            && $lineItem->getDeliveryInformation() !== null
            && $lineItem->getQuantityInformation() !== null;
    }

    private function shouldPriceBeRecalculated(LineItem $lineItem, CartBehavior $behavior): bool
    {
        if ($lineItem->getPriceDefinition() !== null
            && $lineItem->hasExtension(self::CUSTOM_PRICE)
            && $behavior->hasPermission(self::ALLOW_PRODUCT_PRICE_OVERWRITES)) {
            return false;
        }

        if ($lineItem->getPriceDefinition() !== null
            && $behavior->hasPermission(self::SKIP_PRODUCT_RECALCULATION)) {
            return false;
        }

        if ($lineItem->getPriceDefinition() !== null && $lineItem->isModifiedByApp()) {
            return false;
        }
$event = new SalesChannelEntityLoadedEvent(
            $this->createMock(SalesChannelProductDefinition::class),
            [$p1$p2],
            $this->createMock(SalesChannelContext::class)
        );

        $subscriber->salesChannelLoaded($event);

        static::assertEquals(10, $p1->getStock());
        static::assertFalse($p1->getAvailable());
        static::assertEquals(5, $p1->getMinPurchase());
        static::assertTrue($p1->hasExtension('stock_data'));
        static::assertSame($stock1$p1->getExtension('stock_data'));

        static::assertEquals(12, $p2->getStock());
        static::assertTrue($p2->getAvailable());
        static::assertNull($p2->getMinPurchase());
        static::assertTrue($p2->hasExtension('stock_data'));
        static::assertSame($stock2$p2->getExtension('stock_data'));
    }

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