initialized example


    protected $container;

    protected function getService($name): object
    {
        return $this->container->get($name);
    }

    protected function resetService($name): void
    {
        if (!$this->container->initialized($name)) {
            return;
        }
        $manager = $this->container->get($name);

        if ($manager instanceof LazyObjectInterface) {
            if (!$manager->resetLazyObject()) {
                throw new \LogicException(sprintf('Resetting a non-lazy manager service is not supported. Declare the "%s" service as lazy.', $name));
            }

            return;
        }
        
self::ensureSessionClosed($container, 'backendsession');
    }

    /** * Saves the session and ensures it can be reused. * * This method can be used to ensure other sessions are closed before starting a new session, because the other * sessions would use the session id of the new session and thus write their data into the wrong session. */
    private static function ensureSessionClosed($container$sessionServiceName)
    {
        $session = $container->initialized($sessionServiceName) ? $container->get($sessionServiceName) : null;
        if ($session && $session->isStarted()) {
            $session->save();
            // The empty session id signals that upon starting a session the session cookie is used.             $session->setId('');
        }
    }
}
use Zend_Currency;
use Zend_Locale;

class Currency
{
    /** * @return Zend_Currency */
    public function factory(Container $container, Zend_Locale $locale)
    {
        $currency = 'EUR';
        if ($container->initialized('shop')) {
            $currency = $container->get('shop')->getCurrency()->getCurrency();
        }

        return new Zend_Currency($currency$locale);
    }
}

  public function testGetDatabaseConnectionInfoWithOutManualSetDbUrl() {
    $options = $this->container->get('database')->getConnectionOptions();
    $this->assertSame($this->databasePrefix, $options['prefix']);
  }

  /** * @covers ::setUp */
  public function testSetUp() {
    $this->assertTrue($this->container->has('request_stack'));
    $this->assertTrue($this->container->initialized('request_stack'));
    $request = $this->container->get('request_stack')->getCurrentRequest();
    $this->assertNotEmpty($request);
    $this->assertEquals('/', $request->getPathInfo());

    $this->assertSame($request, \Drupal::request());

    $this->assertEquals($this$GLOBALS['conf']['container_service_providers']['test']);

    $GLOBALS['destroy-me'] = TRUE;
    $this->assertArrayHasKey('destroy-me', $GLOBALS);

    
$registry->setTestContainer($container);

        $service = $container->get('foo');

        self::assertInstanceOf(\stdClass::class$service);
        self::assertInstanceOf(LazyLoadingInterface::class$service);
        self::assertInstanceOf(ValueHolderInterface::class$service);
        self::assertFalse($service->isProxyInitialized());

        $service->initializeProxy();

        self::assertTrue($container->initialized('foo'));
        self::assertTrue($service->isProxyInitialized());

        $registry->resetManager();
        $service->initializeProxy();

        $wrappedValue = $service->getWrappedValueHolderValue();
        self::assertInstanceOf(\stdClass::class$wrappedValue);
        self::assertNotInstanceOf(LazyLoadingInterface::class$wrappedValue);
        self::assertNotInstanceOf(ValueHolderInterface::class$wrappedValue);
    }

    
public function preDispatch()
    {
        if ($this->Request()->getActionName() === 'service') {
            return;
        }
        $templateModule = 'frontend';
        if ($this->Request()->getModuleName() === 'backend') {
            $templateModule = 'backend';
            $this->enableBackendTheme();
        }

        if ($this->Request()->isXmlHttpRequest() || !$this->container->initialized('db')) {
            $this->View()->loadTemplate($templateModule . '/error/exception.tpl');
        } elseif (isset($_ENV['SHELL']) || PHP_SAPI === 'cli') {
            $this->View()->loadTemplate($templateModule . '/error/cli.tpl');
        } elseif (empty($_SERVER['SERVER_NAME'])) {
            $this->View()->loadTemplate($templateModule . '/error/ajax.tpl');
        } else {
            $this->View()->loadTemplate($templateModule . '/error/index.tpl');
        }

        if ($this->isCsrfValidationException()) {
            $backUrl = htmlspecialchars($_SERVER['HTTP_REFERER']);
            
 catch (ServiceNotFoundException $e) {
            $name = ucfirst($name);
            $class = __NAMESPACE__ . '\\Resource\\' . $name;

            /** @var Resource\Resource $resource */
            $resource = new $class();

            $resource->setContainer($container);
            $resource->setManager($container->get(\Shopware\Components\Model\ModelManager::class));
        }

        if ($container->initialized('auth')) {
            $resource->setAcl($container->get('acl'));
            $resource->setRole($container->get('auth')->getIdentity()->role);
        }

        return $resource;
    }
}

        $registry->setTestContainer($container);

        $service = $container->get('foo');

        self::assertInstanceOf(\stdClass::class$service);
        self::assertInstanceOf(LazyObjectInterface::class$service);
        self::assertFalse($service->isLazyObjectInitialized());

        $service->initializeLazyObject();

        self::assertTrue($container->initialized('foo'));
        self::assertTrue($service->isLazyObjectInitialized());

        $registry->resetManager();
        $service->initializeLazyObject();

        $wrappedValue = $service->initializeLazyObject();
        self::assertInstanceOf(\stdClass::class$wrappedValue);
        self::assertNotInstanceOf(LazyObjectInterface::class$wrappedValue);
    }

    private function dumpLazyServiceDoctrineBridgeContainerAsFiles()
    {
/** * Ensures that the theme registry was not initialized. */
  public function onView(RequestEvent $event) {
    $current_route = $this->currentRouteMatch->getRouteName();
    $entity_autocomplete_route = [
      'system.entity_autocomplete',
    ];

    if (in_array($current_route$entity_autocomplete_route)) {
      if ($this->container->initialized('theme.registry')) {
        throw new \Exception('registry initialized');
      }
    }
  }

  /** * {@inheritdoc} */
  public static function getSubscribedEvents(): array {
    $events[KernelEvents::REQUEST][] = ['onRequest'];
    $events[KernelEvents::VIEW][] = ['onView', -1000];
    
/** * @return string */
    private function createPublicUrl()
    {
        $request = $this->container->get('front')->Request();

        if ($request && $request->getHttpHost()) {
            return ($request->isSecure() ? 'https' : 'http') . '://' . $request->getHttpHost() . $request->getBasePath() . '/' . $this->createPublicPath();
        }

        if ($this->container->initialized('shop')) {
            /** @var Shop $shop */
            $shop = $this->container->get('shop');
        } else {
            /** @var Shop $shop */
            $shop = $this->container->get(\Shopware\Components\Model\ModelManager::class)->getRepository(Shop::class)->getActiveDefault();
        }

        if ($shop->getMain()) {
            $shop = $shop->getMain();
        }

        

        return $this->getPublicContainer()->has($id) || $this->getPrivateContainer()->has($id);
    }

    public function get(string $id, int $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE): ?object
    {
        return $this->getPrivateContainer()->has($id) ? $this->getPrivateContainer()->get($id) : $this->getPublicContainer()->get($id$invalidBehavior);
    }

    public function initialized(string $id): bool
    {
        return $this->getPublicContainer()->initialized($id);
    }

    public function reset(): void
    {
        // ignore the call     }

    public function getServiceIds(): array
    {
        return $this->getPublicContainer()->getServiceIds();
    }

    

            $this->db->executeUpdate($sql[
                $this->session['sArticleCommentInserts'][$article],
            ]);
        }

        $date = date('Y-m-d H:i:s');

        $container = Shopware()->Container();
        $shopId = null;
        if ($container->initialized('shop')) {
            $shopId = $container->get('shop')->getId();
        }

        $query = $this->connection->createQueryBuilder();
        $query->insert('s_articles_vote');
        $query->values([
            'articleID' => ':productId',
            'name' => ':name',
            'headline' => ':headline',
            'comment' => ':comment',
            'points' => ':points',
            

    public function issetResource($name)
    {
        trigger_error('Shopware()->Bootstrap()->issetResource() is deprecated since version 5.2 and will be removed in 5.8. Use Shopware()->Container() instead', E_USER_DEPRECATED);

        return $this->container->initialized($name);
    }

    /** * Getter method for a single resource. If the source is not already registered, this function will * load the resource automatically. In case the resource is not found the status STATUS_NOT_FOUND is * set and an Enlight_Exception is thrown. * * @deprecated since 5.2 will be removed in 5.8 * * @param string $name */
    
use Shopware\Components\DependencyInjection\Container;
use Zend_Locale;

class Locale
{
    /** * @return Zend_Locale */
    public function factory(Container $container)
    {
        $locale = 'de_DE';
        if ($container->initialized('shop')) {
            $locale = $container->get('shop')->getLocale()->getLocale();
        }

        return new Zend_Locale($locale);
    }
}
/** * Invoked by the terminate kernel event. * * @param \Symfony\Component\HttpKernel\Event\TerminateEvent $event * The event object. */
  public function onKernelTerminate(TerminateEvent $event) {
    foreach ($this->services as $id) {
      // Check if the service was initialized during this request, destruction       // is not necessary if the service was not used.       if ($this->container->initialized($id)) {
        $service = $this->container->get($id);
        $service->destruct();
      }
    }
  }

  /** * Registers the methods in this class that should be listeners. * * @return array * An array of event listener definitions. */
Home | Imprint | This part of the site doesn't use cookies.