Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add dbal related code from DoctrineBundle #1

Closed
wants to merge 36 commits into from
Closed
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
99e4572
add dbal config & extension
dmaicher Nov 23, 2019
3a589e9
cleanup config: remove orm stuff
dmaicher Nov 23, 2019
dfca364
require doctrine/dbal
dmaicher Dec 10, 2019
193d4ec
move more dbal related things
dmaicher Dec 10, 2019
afcbb2c
add data collector + some fixes
dmaicher Dec 10, 2019
300d236
fix data collector
dmaicher Dec 10, 2019
5f7fec1
add ProfilerController
dmaicher Dec 10, 2019
6d75488
add ConnectionRegistry
dmaicher Dec 10, 2019
d7d6ce7
use registry
dmaicher Feb 29, 2020
20b3cb2
start adding tests
dmaicher Feb 29, 2020
ebe8d49
add some more tests
dmaicher Feb 29, 2020
7356d86
add some more tests
dmaicher Feb 29, 2020
c944ac6
adjust composer requirements
dmaicher Mar 7, 2020
af74eec
adjust composer requirements
dmaicher Mar 7, 2020
7630931
CS fixes
dmaicher Mar 7, 2020
4f5ec77
add more tests
dmaicher Mar 7, 2020
2c6f5bd
add schema + extension tests
dmaicher Mar 8, 2020
98fb67e
cleanup
dmaicher Mar 8, 2020
4f78716
more cleanup
dmaicher Mar 8, 2020
25dc5ad
address first review comments
dmaicher Mar 8, 2020
ded7eb8
add RunSqlDoctrineCommand proxy
dmaicher Mar 8, 2020
bd278bf
cs fix
dmaicher Mar 8, 2020
cdc3fd4
add 2 more commands + tests
dmaicher May 11, 2020
ea8b8be
define command services + cleanup
dmaicher May 11, 2020
ed66ca2
cleanup
dmaicher May 11, 2020
1a3d3e6
use correct version of factory from 2.0.x
dmaicher May 11, 2020
d05284d
fix CS
dmaicher May 11, 2020
479e43f
remove unused class
dmaicher May 11, 2020
fe82cd8
fix registry + add test
dmaicher May 11, 2020
4ccb226
drop support for phpunit 7
dmaicher May 11, 2020
1c46aa5
fix phpunit 7 compat
dmaicher May 11, 2020
4d595a3
fix phpunit warnings
dmaicher May 11, 2020
5436c29
fix CS
dmaicher May 11, 2020
4df8eea
fix deprecations for doctrine/dbal >= 2.11
dmaicher May 13, 2020
8d51194
fix CS
dmaicher May 13, 2020
a7361d8
fix comments
dmaicher May 13, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,24 @@
],
"require": {
"php": "^7.1",
"doctrine/dbal": "^2.9",
"jdorn/sql-formatter": "^1.2.16",
"psr/container": "^1.0",
"symfony/config": "^4.3.3|^5.0",
"symfony/console": "^3.4.30|^4.3.3|^5.0",
"symfony/dependency-injection": "^4.3.3|^5.0",
"symfony/doctrine-bridge": "^4.3.7|^5.0",
"symfony/framework-bundle": "^3.4.30|^4.3.3|^5.0",
"symfony/http-kernel": "^3.4.30|^4.3.3|^5.0"
},
"require-dev": {
"doctrine/coding-standard": "^6.0",
"phpunit/phpunit": "^7.5 || ^8.4",
"symfony/phpunit-bridge": "^5.0"
"phpunit/phpunit": "^7.5 || ^8.5",
"symfony/phpunit-bridge": "^5.0",
"symfony/twig-bridge": "^3.4.30|^4.3.3|^5.0",
"symfony/web-profiler-bundle": "^3.4.30|^4.3.3|^5.0",
"symfony/yaml": "^3.4.30|^4.3.3|^5.0",
"twig/twig": "^1.34|^2.12"
},
"config": {
"sort-packages": true
Expand All @@ -31,6 +43,9 @@
"autoload": {
"psr-4": { "Doctrine\\Bundle\\DBALBundle\\": "src" }
},
"autoload-dev": {
"psr-4": { "Doctrine\\Bundle\\DBALBundle\\Tests\\": "tests" }
},
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
Expand Down
53 changes: 53 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0"?>
<ruleset>
<arg name="basepath" value=""/>
<arg name="extensions" value="php"/>
<arg name="parallel" value="80"/>
<arg name="cache" value=".phpcs-cache"/>
<arg name="colors"/>

<!-- Ignore warnings, show progress of the run and show sniff names -->
<arg value="nps"/>

<file>.</file>
<exclude-pattern>vendor/*</exclude-pattern>

<rule ref="Doctrine">
<exclude name="SlevomatCodingStandard.Classes.ClassConstantVisibility"/>
<exclude name="SlevomatCodingStandard.Exceptions.ReferenceThrowableOnly"/>
<exclude name="SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFallbackGlobalName"/>
<exclude name="SlevomatCodingStandard.TypeHints.DeclareStrictTypes"/>
<exclude name="SlevomatCodingStandard.TypeHints.NullableTypeForNullDefaultValue"/>
<exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint"/>
<exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingReturnTypeHint"/>
<exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingReturnTypeHint"/>
<exclude name="SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperator.NullCoalesceOperatorNotUsed"/>
<exclude name="SlevomatCodingStandard.Classes.SuperfluousInterfaceNaming.SuperfluousSuffix"/>
<exclude name="SlevomatCodingStandard.Classes.SuperfluousAbstractClassNaming.SuperfluousPrefix"/>
</rule>

<rule ref="PSR1.Classes.ClassDeclaration.MultipleClasses">
<exclude-pattern>tests/*</exclude-pattern>
</rule>
<rule ref="Squiz.Classes.ClassFileName.NoMatch">
<exclude-pattern>tests/*</exclude-pattern>
</rule>
<rule ref="SlevomatCodingStandard.Classes.UnusedPrivateElements.UnusedProperty">
<exclude-pattern>tests/Fixtures/*</exclude-pattern>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingTraversableParameterTypeHintSpecification">
<exclude-pattern>src/DependencyInjection/*</exclude-pattern>
<exclude-pattern>src/Twig/DoctrineDBALExtension.php</exclude-pattern>
<exclude-pattern>tests/*</exclude-pattern>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingTraversablePropertyTypeHintSpecification">
<exclude-pattern>src/DependencyInjection/*</exclude-pattern>
<exclude-pattern>src/Twig/DoctrineDBALExtension.php</exclude-pattern>
<exclude-pattern>tests/*</exclude-pattern>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingTraversableReturnTypeHintSpecification">
<exclude-pattern>src/DependencyInjection/*</exclude-pattern>
<exclude-pattern>src/Twig/DoctrineDBALExtension.php</exclude-pattern>
<exclude-pattern>tests/*</exclude-pattern>
</rule>
</ruleset>
23 changes: 23 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.1/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
failOnRisky="true"
>
<testsuites>
<testsuite name="Doctrine DBAL Bundle Test Suite">
<directory>tests/</directory>
<exclude>tests/Fixtures</exclude>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./src/</directory>
</whitelist>
</filter>
</phpunit>
16 changes: 16 additions & 0 deletions src/Command/Proxy/ApplicationConnectionHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Doctrine\Bundle\DBALBundle\Command\Proxy;

use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
use Symfony\Bundle\FrameworkBundle\Console\Application;

trait ApplicationConnectionHelper
{
private function setConnectionHelper(Application $application, ?string $connectionName)
{
$connection = $application->getKernel()->getContainer()->get('doctrine.dbal.connection_registry')->getConnection($connectionName);
$helperSet = $application->getHelperSet();
$helperSet->set(new ConnectionHelper($connection), 'db');
}
}
45 changes: 45 additions & 0 deletions src/Command/Proxy/RunSqlDoctrineCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Doctrine\Bundle\DBALBundle\Command\Proxy;

use Doctrine\DBAL\Tools\Console\Command\RunSqlCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Execute a SQL query and output the results.
*/
class RunSqlDoctrineCommand extends RunSqlCommand
{
use ApplicationConnectionHelper;

/**
* {@inheritDoc}
*/
protected function configure()
{
parent::configure();

$this
->setName('doctrine:query:sql')
->addOption('connection', null, InputOption::VALUE_OPTIONAL, 'The connection to use for this command')
->setHelp(<<<EOT
The <info>%command.name%</info> command executes the given SQL query and
outputs the results:

<info>php %command.full_name% "SELECT * FROM users"</info>
EOT
);
}

/**
* {@inheritDoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->setConnectionHelper($this->getApplication(), $input->getOption('connection'));

return parent::execute($input, $output);
}
}
173 changes: 173 additions & 0 deletions src/ConnectionFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
<?php

namespace Doctrine\Bundle\DBALBundle;

use Doctrine\Common\EventManager;
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Exception\DriverException;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
use const E_USER_DEPRECATED;
use function get_class;
use function trigger_error;

class ConnectionFactory
{
/** @var mixed[][] */
private $typesConfig = [];

/** @var bool */
private $initialized = false;

/**
* @param mixed[][] $typesConfig
*/
public function __construct(array $typesConfig)
{
$this->typesConfig = $typesConfig;
}

/**
* Create a connection by name.
*
* @param mixed[] $params
* @param string[]|Type[] $mappingTypes
*
* @return Connection
*/
public function createConnection(array $params, Configuration $config = null, EventManager $eventManager = null, array $mappingTypes = [])
{
if (! $this->initialized) {
$this->initializeTypes();
}

$connection = DriverManager::getConnection($params, $config, $eventManager);

if (! empty($mappingTypes)) {
$platform = $this->getDatabasePlatform($connection);
foreach ($mappingTypes as $dbType => $doctrineType) {
$platform->registerDoctrineTypeMapping($dbType, $doctrineType);
}
}

if (! empty($this->typesConfig)) {
$this->markTypesCommented($this->getDatabasePlatform($connection));
}

return $connection;
}

/**
* Try to get the database platform.
*
* This could fail if types should be registered to an predefined/unused connection
* and the platform version is unknown.
* For details have a look at DoctrineBundle issue #673.
*
* @return AbstractPlatform
*
* @throws DBALException
*/
private function getDatabasePlatform(Connection $connection)
{
try {
return $connection->getDatabasePlatform();
} catch (DriverException $driverException) {
throw new DBALException(
'An exception occured while establishing a connection to figure out your platform version.' . PHP_EOL .
"You can circumvent this by setting a 'server_version' configuration value" . PHP_EOL . PHP_EOL .
'For further information have a look at:' . PHP_EOL .
'https://github.com/doctrine/DoctrineBundle/issues/673',
0,
$driverException
);
}
}

/**
* initialize the types
*/
private function initializeTypes()
{
foreach ($this->typesConfig as $typeName => $typeConfig) {
if (Type::hasType($typeName)) {
Type::overrideType($typeName, $typeConfig['class']);
} else {
Type::addType($typeName, $typeConfig['class']);
}
}

$this->initialized = true;
}

private function markTypesCommented(AbstractPlatform $platform) : void
{
foreach ($this->typesConfig as $typeName => $typeConfig) {
$type = Type::getType($typeName);
$requiresSQLCommentHint = $type->requiresSQLCommentHint($platform);

// Attribute is missing, make sure a type that doesn't require a comment is marked as commented
// This is deprecated behaviour that will be dropped in 2.0.
if ($typeConfig['commented'] === null) {
if (! $requiresSQLCommentHint) {
@trigger_error(
sprintf(
'The type "%s" was implicitly marked as commented due to the configuration. This is deprecated and will be removed in DoctrineBundle 2.0. Either set the "commented" attribute in the configuration to "false" or mark the type as commented in "%s::requiresSQLCommentHint()."',
$typeName,
get_class($type)
),
E_USER_DEPRECATED
);

$platform->markDoctrineTypeCommented($type);
}

continue;
}

// The following logic generates appropriate deprecation notices telling the user how to update their type configuration.
if ($typeConfig['commented']) {
if (! $requiresSQLCommentHint) {
@trigger_error(
sprintf(
'The type "%s" was marked as commented in its configuration but not in the type itself. This is deprecated and will be removed in DoctrineBundle 2.0. Please update the return value of "%s::requiresSQLCommentHint()."',
$typeName,
get_class($type)
),
E_USER_DEPRECATED
);

$platform->markDoctrineTypeCommented($type);

continue;
}

@trigger_error(
sprintf(
'The type "%s" was explicitly marked as commented in its configuration. This is no longer necessary and will be removed in DoctrineBundle 2.0. Please remove the "commented" attribute from the type configuration.',
$typeName
),
E_USER_DEPRECATED
);

continue;
}

if (! $requiresSQLCommentHint) {
continue;
}

@trigger_error(
sprintf(
'The type "%s" was marked as uncommented in its configuration but commented in the type itself. This is deprecated and will be removed in DoctrineBundle 2.0. Please update the return value of "%s::requiresSQLCommentHint()" or remove the "commented" attribute from the type configuration.',
$typeName,
get_class($type)
),
E_USER_DEPRECATED
);
}
}
}
36 changes: 36 additions & 0 deletions src/ConnectionRegistry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Doctrine\Bundle\DBALBundle;

use Doctrine\DBAL\Connection;

interface ConnectionRegistry
{
/**
* Gets the default connection name.
*
* @return string The default connection name.
*/
public function getDefaultConnectionName() : string;

/**
* Gets the named connection.
*
* @param string $name The connection name (null for the default one).
*/
public function getConnection(?string $name = null) : Connection;

/**
* Gets an array of all registered connections.
*
* @return array<string, Connection> An array of Connection instances.
*/
public function getConnections() : array;

/**
* Gets all connection names.
*
* @return array<string> An array of connection names.
*/
public function getConnectionNames() : array;
}
Loading