createPDO example


    /** * @param array<string, mixed> $dbConfig * * @return PDO */
    public static function createPDO(array $dbConfig)
    {
        if (isset($dbConfig['factory']) && class_exists($dbConfig['factory'])) {
            $factory = $dbConfig['factory'];

            return $factory::createPDO($dbConfig);
        }

        $password = $dbConfig['password'] ?? '';
        $connectionString = self::buildConnectionString($dbConfig);

        try {
            $conn = new PDO(
                'mysql:' . $connectionString,
                $dbConfig['username'],
                $password,
                $dbConfig['pdoOptions'] ?? null
            );

    public function boot($skipDatabase = false)
    {
        if ($this->booted) {
            return;
        }

        if (!$skipDatabase) {
            $dbConn = $this->config['db'];
            $this->connection = Components\DependencyInjection\Bridge\Db::createPDO($dbConn);
            $this->initializePlugins();
        }

        $this->initializeContainer();
        $this->initializeShopware();

        foreach ($this->getBundles() as $bundle) {
            $bundle->setContainer($this->container);

            if ((!$bundle instanceof Plugin) || $bundle->isActive()) {
                $bundle->boot();
            }

        $sessionOptions = (array) $container->getParameter('shopware.session');
        if (isset($sessionOptions['save_handler']) && $sessionOptions['save_handler'] !== 'db') {
            return null;
        }

        $dbOptions = $container->getParameter('shopware.db');
        if (!\is_array($dbOptions)) {
            throw new RuntimeException('Parameter shopware.db has to be an array');
        }

        $conn = Db::createPDO($dbOptions);

        return new PdoSessionHandler(
            $conn,
            [
                'db_table' => 's_core_sessions',
                'db_id_col' => 'id',
                'db_data_col' => 'data',
                'db_expiry_col' => 'expiry',
                'db_time_col' => 'modified',
                'lock_mode' => $sessionOptions['locking'] ? PdoSessionHandler::LOCK_TRANSACTIONAL : PdoSessionHandler::LOCK_NONE,
            ]
        );
private function createSaveHandler(Container $container): ?PdoSessionHandler
    {
        /** @var array<string, string> $sessionOptions */
        $sessionOptions = $container->getParameter('shopware.backendsession');
        if (isset($sessionOptions['save_handler']) && $sessionOptions['save_handler'] !== 'db') {
            return null;
        }

        /** @var array<string, string> $dbOptions */
        $dbOptions = $container->getParameter('shopware.db');
        $conn = Db::createPDO($dbOptions);

        return new PdoSessionHandler(
            $conn,
            [
                'db_table' => 's_core_sessions_backend',
                'db_id_col' => 'id',
                'db_data_col' => 'data',
                'db_expiry_col' => 'expiry',
                'db_time_col' => 'modified',
                'lock_mode' => $sessionOptions['locking'] ? PdoSessionHandler::LOCK_TRANSACTIONAL : PdoSessionHandler::LOCK_NONE,
            ]
        );
Home | Imprint | This part of the site doesn't use cookies.