AppserverIo\Appserver\Doctrine\Utils\ConnectionUtil::fromDatabaseNode PHP Метод

fromDatabaseNode() публичный Метод

Creates an array with the connection parameters for a Doctrine DBAL connection from the passed database node.
public fromDatabaseNode ( AppserverIo\Appserver\Core\Api\Node\DatabaseNodeInterface $databaseNode ) : array
$databaseNode AppserverIo\Appserver\Core\Api\Node\DatabaseNodeInterface The database node to create the connection parameters from
Результат array The DBAL connection parameters
    public function fromDatabaseNode(DatabaseNodeInterface $databaseNode)
    {
        // initialize the connection parameters with the mandatory driver
        $connectionParameters = array('driver' => $databaseNode->getDriver()->getNodeValue()->__toString());
        // initialize the path/memory to the database when we use sqlite for example
        if ($pathNode = $databaseNode->getPath()) {
            $connectionParameters['path'] = $this->getApplication()->getWebappPath() . DIRECTORY_SEPARATOR . $pathNode->getNodeValue()->__toString();
        } elseif ($memoryNode = $databaseNode->getMemory()) {
            $connectionParameters['memory'] = Boolean::valueOf(new String($memoryNode->getNodeValue()->__toString()))->booleanValue();
        } else {
            // do nothing here, because there is NO option
        }
        // add username, if specified
        if ($userNode = $databaseNode->getUser()) {
            $connectionParameters['user'] = $userNode->getNodeValue()->__toString();
        }
        // add password, if specified
        if ($passwordNode = $databaseNode->getPassword()) {
            $connectionParameters['password'] = $passwordNode->getNodeValue()->__toString();
        }
        // add database name if using another PDO driver than sqlite
        if ($databaseNameNode = $databaseNode->getDatabaseName()) {
            $connectionParameters['dbname'] = $databaseNameNode->getNodeValue()->__toString();
        }
        // add database host if using another PDO driver than sqlite
        if ($databaseHostNode = $databaseNode->getDatabaseHost()) {
            $connectionParameters['host'] = $databaseHostNode->getNodeValue()->__toString();
        }
        // add database port if using another PDO driver than sqlite
        if ($databasePortNode = $databaseNode->getDatabasePort()) {
            $connectionParameters['port'] = $databasePortNode->getNodeValue()->__toString();
        }
        // add charset, if specified
        if ($charsetNode = $databaseNode->getCharset()) {
            $connectionParameters['charset'] = $charsetNode->getNodeValue()->__toString();
        }
        // add driver options, if specified
        if ($unixSocketNode = $databaseNode->getUnixSocket()) {
            $connectionParameters['unix_socket'] = $unixSocketNode->getNodeValue()->__toString();
        }
        // add server version, if specified
        if ($serverVersionNode = $databaseNode->getServerVersion()) {
            $connectionParameters['server_version'] = $serverVersionNode->getNodeValue()->__toString();
        }
        // set platform, if specified
        if ($platformNode = $databaseNode->getPlatform()) {
            $platform = $platformNode->getNodeValue()->__toString();
            $connectionParameters['platform'] = new $platform();
        }
        // add driver options, if specified
        if ($driverOptionsNode = $databaseNode->getDriverOptions()) {
            // explode the raw options separated with a semicolon
            $rawOptions = explode(';', $driverOptionsNode->getNodeValue()->__toString());
            // prepare the array with the driver options key/value pair (separated with a =)
            $options = array();
            foreach ($rawOptions as $rawOption) {
                list($key, $value) = explode('=', $rawOption);
                $options[$key] = $value;
            }
            // set the driver options
            $connectionParameters['driverOptions'] = $options;
        }
        // returns the connection parameters
        return $connectionParameters;
    }