From a7d764f6c07c6d7cb07fc4ccf7447e14a90ca569 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Sun, 17 Mar 2013 12:16:42 +0100 Subject: [PATCH] Moved tools to Advanced topic, included simple setting up tools in Installation. Reworked CLI tool to print a template when no cli-config.php is defined. --- bin/doctrine.php | 41 +++++++++++++------ docs/en/index.rst | 2 +- docs/en/reference/configuration.rst | 28 +++++++++++++ docs/en/reference/tools.rst | 19 ++++----- .../ORM/Tools/Console/ConsoleRunner.php | 41 +++++++++++++++++++ 5 files changed, 107 insertions(+), 24 deletions(-) diff --git a/bin/doctrine.php b/bin/doctrine.php index 2d50be4fb..c73556284 100755 --- a/bin/doctrine.php +++ b/bin/doctrine.php @@ -13,32 +13,47 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * This software consists of voluntary contributions made by many individuals - * and is licensed under the LGPL. For more information, see + * and is licensed under the MIT license. For more information, see * . */ +use Symfony\Component\Console\Helper\HelperSet; +use Doctrine\ORM\Tools\Console\ConsoleRunner; + (@include_once __DIR__ . '/../vendor/autoload.php') || @include_once __DIR__ . '/../../../autoload.php'; -$configFile = getcwd() . DIRECTORY_SEPARATOR . 'cli-config.php'; -$helperSet = null; -$commands = array(); -if (file_exists($configFile)) { - if ( ! is_readable($configFile)) { - trigger_error( - 'Configuration file [' . $configFile . '] does not have read permission.', E_ERROR - ); +$directories = array(getcwd(), getcwd() . DIRECTORY_SEPARATOR . 'config'); + +$configFile = null; +foreach ($directories as $directory) { + $configFile = $directory . DIRECTORY_SEPARATOR . 'cli-config.php'; + + if (file_exists($configFile)) { + break; } +} - require $configFile; +if ( ! file_exists($configFile)) { + ConsoleRunner::printCliConfigTemplate(); + exit(1); +} +if ( ! is_readable($configFile)) { + echo 'Configuration file [' . $configFile . '] does not have read permission.' . "\n"; + exit(1); +} + +$commands = array(); + +$helperSet = require $configFile; + +if ( ! ($helperSet instanceof HelperSet)) { foreach ($GLOBALS as $helperSetCandidate) { - if ($helperSetCandidate instanceof \Symfony\Component\Console\Helper\HelperSet) { + if ($helperSetCandidate instanceof HelperSet) { $helperSet = $helperSetCandidate; break; } } } -$helperSet = ($helperSet) ?: new \Symfony\Component\Console\Helper\HelperSet(); - \Doctrine\ORM\Tools\Console\ConsoleRunner::run($helperSet, $commands); diff --git a/docs/en/index.rst b/docs/en/index.rst index da89cf227..21196c57f 100644 --- a/docs/en/index.rst +++ b/docs/en/index.rst @@ -27,7 +27,6 @@ Getting Started * **Setup**: :doc:`Installation & Configuration ` | - :doc:`Commandline Tools ` * **Tutorial**: :doc:`Code First ` | @@ -71,6 +70,7 @@ Advanced Topics * :doc:`Architecture ` * :doc:`Advanced Configuration ` * :doc:`Limitations and knowns issues ` + * :doc:`Commandline Tools ` * :doc:`Transactions and Concurrency ` * :doc:`Filters ` * :doc:`NamingStrategy ` diff --git a/docs/en/reference/configuration.rst b/docs/en/reference/configuration.rst index 4835b9cb2..4743971fe 100644 --- a/docs/en/reference/configuration.rst +++ b/docs/en/reference/configuration.rst @@ -92,3 +92,31 @@ Configuration ` section. You can learn more about the database connection configuration in the `Doctrine DBAL connection configuration reference `_. +Setting up the Commandline Tool +------------------------------- + +Doctrine ships with a number of command line tools that are very helpful +during development. You can call this command from the Composer binary +directory: + +.. code-block:: + + $ php vendor/bin/doctrine + +You need to register your applications EntityManager to the console tool +to make use of the tasks by creating a ``cli-config.php`` file with the +following content: + +.. code-block:: php + + new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()), + 'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em) + )); diff --git a/docs/en/reference/tools.rst b/docs/en/reference/tools.rst index 12ed71a0c..b65863ed3 100644 --- a/docs/en/reference/tools.rst +++ b/docs/en/reference/tools.rst @@ -4,30 +4,29 @@ Tools Doctrine Console ---------------- -The Doctrine Console is a Command Line Interface tool for -simplifying common tasks during the development of a project that -uses Doctrine 2. +The Doctrine Console is a Command Line Interface tool for simplifying common +administration tasks during the development of a project that uses Doctrine 2. -Take a look at the :doc:`Configuration ` for more -information how to setup the console command. +Take a look at the :doc:`Installation and Configuration ` +chapter for more information how to setup the console command. -Getting Help -~~~~~~~~~~~~ +Display Help Information +~~~~~~~~~~~~~~~~~~~~~~~~ -Type ``php vendor/bin/doctrine-orm`` on the command line and you should see an +Type ``php vendor/bin/doctrine`` on the command line and you should see an overview of the available commands or use the --help flag to get information on the available commands. If you want to know more about the use of generate entities for example, you can call: .. code-block:: php - $> php vendor/bin/doctrine-orm orm:generate-entities --help + $> php vendor/bin/doctrine orm:generate-entities --help Configuration ~~~~~~~~~~~~~ -Whenever the ``doctrine-orm`` command line tool is invoked, it can +Whenever the ``doctrine`` command line tool is invoked, it can access all Commands that were registered by developer. There is no auto-detection mechanism at work. The Doctrine binary already registers all the commands that currently ship with diff --git a/lib/Doctrine/ORM/Tools/Console/ConsoleRunner.php b/lib/Doctrine/ORM/Tools/Console/ConsoleRunner.php index 5122ac8de..f263a4f0b 100644 --- a/lib/Doctrine/ORM/Tools/Console/ConsoleRunner.php +++ b/lib/Doctrine/ORM/Tools/Console/ConsoleRunner.php @@ -23,8 +23,27 @@ use Symfony\Component\Console\Application; use Symfony\Component\Console\Helper\HelperSet; use Doctrine\ORM\Version; +use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper; +use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper; + +/** + * Handles running the Console Tools inside Symfony Console context. + */ class ConsoleRunner { + /** + * Create a Symfony Console HelperSet + * + * @return HelperSet + */ + public static function createHelperSet(EntityManager $entityManager) + { + return new HelperSet(array( + 'db' => new ConnectionHelper($entityManager->getConnection()), + 'em' => new EntityManagerHelper($entityManager) + )); + } + /** * Runs console with the given helperset. * @@ -73,4 +92,26 @@ class ConsoleRunner new \Doctrine\ORM\Tools\Console\Command\InfoCommand() )); } + + static public function printCliConfigTemplate() + { + echo <<<'HELP' +You are missing a "cli-config.php" or "config/cli-config.php" file in your +project, which is required to get the Doctrine Console working. You can use the +following sample as a template: + +