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

Drop helperset usage in favor of the DependencyFactory #898

Merged
merged 18 commits into from
Jan 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
60 changes: 6 additions & 54 deletions bin/doctrine-migrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,11 @@

use Doctrine\Migrations\Tools\Console\ConsoleRunner;
use Phar;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Helper\QuestionHelper;
use const DIRECTORY_SEPARATOR;
use const E_USER_ERROR;
use const PHP_EOL;
use const STDERR;
use function extension_loaded;
use function file_exists;
use function getcwd;
use function is_readable;
use function trigger_error;
use function fwrite;

(static function () : void {
$autoloadFiles = [
Expand All @@ -24,7 +19,6 @@
];

$autoloaderFound = false;

foreach ($autoloadFiles as $autoloadFile) {
if (! file_exists($autoloadFile)) {
continue;
Expand All @@ -36,57 +30,15 @@

if (! $autoloaderFound) {
if (extension_loaded('phar') && Phar::running() !== '') {
echo 'The PHAR was built without dependencies!' . PHP_EOL;
fwrite(STDERR, 'The PHAR was built without dependencies!' . PHP_EOL);
exit(1);
}

echo 'vendor/autoload.php could not be found. Did you run `composer install`?', PHP_EOL;
fwrite(STDERR, 'vendor/autoload.php could not be found. Did you run `composer install`?' . PHP_EOL);
exit(1);
}

// Support for using the Doctrine ORM convention of providing a `cli-config.php` file.
$configurationDirectories = [
getcwd(),
getcwd() . DIRECTORY_SEPARATOR . 'config',
];

$configurationFile = null;
foreach ($configurationDirectories as $configurationDirectory) {
$configurationFilePath = $configurationDirectory . DIRECTORY_SEPARATOR . 'cli-config.php';

if (! file_exists($configurationFilePath)) {
continue;
}

$configurationFile = $configurationFilePath;
break;
}

$helperSet = null;
if ($configurationFile !== null) {
if (! is_readable($configurationFile)) {
trigger_error('Configuration file [' . $configurationFile . '] does not have read permission.', E_USER_ERROR);
exit(1);
}

$helperSet = require $configurationFile;

if (! $helperSet instanceof HelperSet) {
foreach ($GLOBALS as $helperSetCandidate) {
if (! $helperSetCandidate instanceof HelperSet) {
continue;
}

$helperSet = $helperSetCandidate;
break;
}
}
}

$helperSet = $helperSet ?? new HelperSet();
$helperSet->set(new QuestionHelper(), 'question');

$commands = [];
$dependencyFactory = ConsoleRunner::findDependencyFactory();

ConsoleRunner::run($helperSet, $commands);
ConsoleRunner::run([], $dependencyFactory);
})();
50 changes: 32 additions & 18 deletions docs/en/reference/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ Here are details about what each configuration option does:


Here the possible options for ``table_storage``:

+----------------------------+------------+------------------------------+----------------------------------------------------------------------------------+
| Name | Required | Default | Description |
+============================+============+==============================+==================================================================================+
Expand Down Expand Up @@ -262,6 +263,23 @@ the following command:

$ mysqladmin create migrations_docs_example


If you have already a DBAL connection available in your application, ``migrations-db.php`` can return it directly:

.. code-block:: php

<?php
use Doctrine\DBAL\DriverManager;

return DriverManager::getConnection([
'dbname' => 'migrations_docs_example',
'user' => 'root',
'password' => '',
'host' => 'localhost',
'driver' => 'pdo_mysql',
]);


Advanced
~~~~~~~~

Expand All @@ -279,16 +297,16 @@ out of the root of your project.
require 'vendor/autoload.php';

use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
use Symfony\Component\Console\Helper\HelperSet;
use Doctrine\Migrations\Configuration\Configuration\PhpFile;
use Doctrine\Migrations\Configuration\Connection\ExistingConnection;
use Doctrine\Migrations\DependencyFactory;
goetas marked this conversation as resolved.
Show resolved Hide resolved

$dbParams = include 'migrations-db.php';
$config = new PhpFile('migrations.php'); // Or use one of the Doctrine\Migrations\Configuration\Configuration\* loaders

$connection = DriverManager::getConnection($dbParams);
$conn = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'memory' => true]);

return DependencyFactory::fromConnection($config, new ExistingConnection($conn));

return new HelperSet([
'db' => new ConnectionHelper($connection),
]);

The above setup assumes you are not using the ORM. If you want to use the ORM, first require it in your project
with composer:
Expand All @@ -305,24 +323,20 @@ Now update your ``cli-config.php`` in the root of your project to look like the

require 'vendor/autoload.php';

use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Console\Helper\HelperSet;
use Doctrine\ORM\Tools\Setup;
use Doctrine\Migrations\Configuration\EntityManager\ExistingEntityManager;
use Doctrine\Migrations\DependencyFactory;
goetas marked this conversation as resolved.
Show resolved Hide resolved

$config = new PhpFile('migrations.php'); // Or use one of the Doctrine\Migrations\Configuration\Configuration\* loaders

$paths = [__DIR__.'/lib/MyProject/Entities'];
$isDevMode = true;

$dbParams = include 'migrations-db.php';

$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
$entityManager = EntityManager::create($dbParams, $config);
$entityManager = EntityManager::create(['driver' => 'pdo_sqlite', 'memory' => true], $config);

return new HelperSet([
'em' => new EntityManagerHelper($entityManager),
'db' => new ConnectionHelper($entityManager->getConnection()),
]);
return DependencyFactory::fromEntityManager($config, new ExistingEntityManager($entityManager));

Make sure to create the directory where your ORM entities will be located:

Expand Down
32 changes: 15 additions & 17 deletions docs/en/reference/custom-configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ Once you have your custom integration setup, you can modify it to look like the
require_once __DIR__.'/vendor/autoload.php';

use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
use Doctrine\Migrations\Configuration\Configuration;
use Doctrine\Migrations\Configuration\Connection\ExistingConnection;
use Doctrine\Migrations\Configuration\Configuration\ExistingConfiguration;
use Doctrine\Migrations\DependencyFactory;
une Doctrine\Migrations\Metadata\Storage\TableMetadataStorageConfiguration;
use Doctrine\Migrations\Tools\Console\Command;
use Doctrine\Migrations\Tools\Console\Helper\ConfigurationHelper;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Helper\QuestionHelper;

$dbParams = [
'dbname' => 'migrations_docs_example',
Expand All @@ -45,24 +44,23 @@ Once you have your custom integration setup, you can modify it to look like the

$configuration->setMetadataStorageConfiguration($storageConfiguration);

$helperSet = new HelperSet();
$helperSet->set(new QuestionHelper(), 'question');
$helperSet->set(new ConnectionHelper($connection), 'db');
$helperSet->set(new ConfigurationHelper($connection, $configuration));
$dependencyFactory = DependencyFactory::fromConnection(
new ExistingConfiguration($configuration),
new ExistingConnection($connection)
);

$cli = new Application('Doctrine Migrations');
$cli->setCatchExceptions(true);
$cli->setHelperSet($helperSet);

$cli->addCommands(array(
new Command\DumpSchemaCommand(),
new Command\ExecuteCommand(),
new Command\GenerateCommand(),
new Command\LatestCommand(),
new Command\MigrateCommand(),
new Command\RollupCommand(),
new Command\StatusCommand(),
new Command\VersionCommand()
new Command\DumpSchemaCommand($dependencyFactory),
new Command\ExecuteCommand($dependencyFactory),
new Command\GenerateCommand($dependencyFactory),
new Command\LatestCommand($dependencyFactory),
new Command\MigrateCommand($dependencyFactory),
new Command\RollupCommand($dependencyFactory),
new Command\StatusCommand($dependencyFactory),
new Command\VersionCommand($dependencyFactory)
));

$cli->run();
Expand Down
32 changes: 16 additions & 16 deletions docs/en/reference/custom-integration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ Now place the following code in the ``migrations`` file:
require_once __DIR__.'/vendor/autoload.php';

use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
use Doctrine\Migrations\DependencyFactory;
use Doctrine\Migrations\Configuration\Configuration\PhpFile;
use Doctrine\Migrations\Configuration\Connection\ExistingConnection;
use Doctrine\Migrations\Tools\Console\Command;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Helper\QuestionHelper;

$dbParams = [
'dbname' => 'migrations_test',
'dbname' => 'migrations_docs_example',
'user' => 'root',
'password' => '',
'host' => 'localhost',
Expand All @@ -36,27 +36,27 @@ Now place the following code in the ``migrations`` file:

$connection = DriverManager::getConnection($dbParams);

$helperSet = new HelperSet();
$helperSet->set(new QuestionHelper(), 'question');
$helperSet->set(new ConnectionHelper($connection), 'db');
$config = new PhpFile('migrations.php'); // Or use one of the Doctrine\Migrations\Configuration\Configuration\* loaders

$dependencyFactory = DependencyFactory::fromConnection($config, new ExistingConnection($conn));

$cli = new Application('Doctrine Migrations');
$cli->setCatchExceptions(true);
$cli->setHelperSet($helperSet);

$cli->addCommands(array(
new Command\DumpSchemaCommand(),
new Command\ExecuteCommand(),
new Command\GenerateCommand(),
new Command\LatestCommand(),
new Command\MigrateCommand(),
new Command\RollupCommand(),
new Command\StatusCommand(),
new Command\VersionCommand()
new Command\DumpSchemaCommand($dependencyFactory),
new Command\ExecuteCommand($dependencyFactory),
new Command\GenerateCommand($dependencyFactory),
new Command\LatestCommand($dependencyFactory),
new Command\MigrateCommand($dependencyFactory),
new Command\RollupCommand($dependencyFactory),
new Command\StatusCommand($dependencyFactory),
new Command\VersionCommand($dependencyFactory)
));

$cli->run();


Now you can execute the migrations console application like this:

.. code-block:: bash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

declare(strict_types=1);

namespace Doctrine\Migrations\Configuration\Loader;
namespace Doctrine\Migrations\Configuration\Configuration;

use Closure;
use Doctrine\Migrations\Configuration\Configuration;
use Doctrine\Migrations\Configuration\Exception\InvalidConfigurationKey;
use Doctrine\Migrations\Configuration\Exception\UnableToLoadResource;
use Doctrine\Migrations\Configuration\Configuration\Exception\InvalidConfigurationKey;
use Doctrine\Migrations\Metadata\Storage\TableMetadataStorageConfiguration;
use Doctrine\Migrations\Tools\BooleanStringFormatter;
use function assert;
Expand All @@ -16,20 +15,21 @@
use function is_bool;
use function is_callable;

/**
* @internal
*/
final class ArrayLoader implements Loader
final class ConfigurationArray implements ConfigurationLoader
{
/** @var array<string,mixed> */
private $configurations;

/**
* @param mixed $array
* @param array<string,mixed> $configurations
*/
public function load($array) : Configuration
public function __construct(array $configurations)
{
if (! is_array($array)) {
throw UnableToLoadResource::with(static::class);
}
$this->configurations = $configurations;
}

public function getConfiguration() : Configuration
{
$configMap = [
'migrations_paths' => static function ($paths, Configuration $configuration) : void {
foreach ($paths as $namespace => $path) {
Expand Down Expand Up @@ -63,7 +63,7 @@ public function load($array) : Configuration
];

$object = new Configuration();
self::applyConfigs($configMap, $object, $array);
self::applyConfigs($configMap, $object, $this->configurations);

if ($object->getMetadataStorageConfiguration() === null) {
$object->setMetadataStorageConfiguration(new TableMetadataStorageConfiguration());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Configuration\Configuration;

use function dirname;
use function realpath;

abstract class ConfigurationFile implements ConfigurationLoader
{
/** @var string */
protected $file;

public function __construct(string $file)
{
$this->file = $file;
}

/**
* @param array<string,string> $directories
*
* @return array<string,string>
*/
final protected function getDirectoriesRelativeToFile(array $directories, string $file) : array
{
foreach ($directories as $ns => $dir) {
$path = realpath(dirname($file) . '/' . $dir);

$directories[$ns] = $path !== false ? $path : $dir;
}

return $directories;
}
}
Loading