Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Moved tools to Advanced topic, included simple setting up tools in In…

…stallation. Reworked CLI tool to print a template when no cli-config.php is defined.
  • Loading branch information...
commit a7d764f6c07c6d7cb07fc4ccf7447e14a90ca569 1 parent bdfe609
Benjamin Eberlei authored March 17, 2013
41  bin/doctrine.php
@@ -13,32 +13,47 @@
13 13
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14 14
  *
15 15
  * This software consists of voluntary contributions made by many individuals
16  
- * and is licensed under the LGPL. For more information, see
  16
+ * and is licensed under the MIT license. For more information, see
17 17
  * <http://www.doctrine-project.org>.
18 18
  */
19 19
 
  20
+use Symfony\Component\Console\Helper\HelperSet;
  21
+use Doctrine\ORM\Tools\Console\ConsoleRunner;
  22
+
20 23
 (@include_once __DIR__ . '/../vendor/autoload.php') || @include_once __DIR__ . '/../../../autoload.php';
21  
-$configFile = getcwd() . DIRECTORY_SEPARATOR . 'cli-config.php';
22 24
 
23  
-$helperSet = null;
24  
-$commands = array();
25  
-if (file_exists($configFile)) {
26  
-    if ( ! is_readable($configFile)) {
27  
-        trigger_error(
28  
-            'Configuration file [' . $configFile . '] does not have read permission.', E_ERROR
29  
-        );
  25
+$directories = array(getcwd(), getcwd() . DIRECTORY_SEPARATOR . 'config');
  26
+
  27
+$configFile = null;
  28
+foreach ($directories as $directory) {
  29
+    $configFile = $directory . DIRECTORY_SEPARATOR . 'cli-config.php';
  30
+
  31
+    if (file_exists($configFile)) {
  32
+        break;
30 33
     }
  34
+}
  35
+
  36
+if ( ! file_exists($configFile)) {
  37
+    ConsoleRunner::printCliConfigTemplate();
  38
+    exit(1);
  39
+}
  40
+
  41
+if ( ! is_readable($configFile)) {
  42
+    echo 'Configuration file [' . $configFile . '] does not have read permission.' . "\n";
  43
+    exit(1);
  44
+}
31 45
 
32  
-    require $configFile;
  46
+$commands = array();
  47
+
  48
+$helperSet = require $configFile;
33 49
 
  50
+if ( ! ($helperSet instanceof HelperSet)) {
34 51
     foreach ($GLOBALS as $helperSetCandidate) {
35  
-        if ($helperSetCandidate instanceof \Symfony\Component\Console\Helper\HelperSet) {
  52
+        if ($helperSetCandidate instanceof HelperSet) {
36 53
             $helperSet = $helperSetCandidate;
37 54
             break;
38 55
         }
39 56
     }
40 57
 }
41 58
 
42  
-$helperSet = ($helperSet) ?: new \Symfony\Component\Console\Helper\HelperSet();
43  
-
44 59
 \Doctrine\ORM\Tools\Console\ConsoleRunner::run($helperSet, $commands);
2  docs/en/index.rst
Source Rendered
@@ -27,7 +27,6 @@ Getting Started
27 27
 
28 28
 * **Setup**:
29 29
   :doc:`Installation & Configuration <reference/configuration>` |
30  
-  :doc:`Commandline Tools <reference/tools>`
31 30
 
32 31
 * **Tutorial**:
33 32
   :doc:`Code First <tutorials/getting-started>` |
@@ -71,6 +70,7 @@ Advanced Topics
71 70
   * :doc:`Architecture <reference/architecture>`
72 71
   * :doc:`Advanced Configuration <reference/advanced-configuration>`
73 72
   * :doc:`Limitations and knowns issues <reference/limitations-and-known-issues>`
  73
+  * :doc:`Commandline Tools <reference/tools>`
74 74
   * :doc:`Transactions and Concurrency <reference/transactions-and-concurrency>`
75 75
   * :doc:`Filters <reference/filters>`
76 76
   * :doc:`NamingStrategy <reference/namingstrategy>`
28  docs/en/reference/configuration.rst
Source Rendered
@@ -92,3 +92,31 @@ Configuration <reference/advanced-configuration>` section.
92 92
     You can learn more about the database connection configuration in the
93 93
     `Doctrine DBAL connection configuration reference <http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html>`_.
94 94
 
  95
+Setting up the Commandline Tool
  96
+-------------------------------
  97
+
  98
+Doctrine ships with a number of command line tools that are very helpful
  99
+during development. You can call this command from the Composer binary
  100
+directory:
  101
+
  102
+.. code-block::
  103
+
  104
+    $ php vendor/bin/doctrine
  105
+
  106
+You need to register your applications EntityManager to the console tool
  107
+to make use of the tasks by creating a ``cli-config.php`` file with the
  108
+following content:
  109
+
  110
+.. code-block:: php
  111
+
  112
+    <?php
  113
+    // cli-config.php
  114
+    require_once 'my_bootstrap.php';
  115
+
  116
+    // Any way to access the EntityManager from  your application
  117
+    $em = GetMyEntityManager();
  118
+    
  119
+    $helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
  120
+        'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()),
  121
+        'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em)
  122
+    ));
19  docs/en/reference/tools.rst
Source Rendered
@@ -4,30 +4,29 @@ Tools
4 4
 Doctrine Console
5 5
 ----------------
6 6
 
7  
-The Doctrine Console is a Command Line Interface tool for
8  
-simplifying common tasks during the development of a project that
9  
-uses Doctrine 2.
  7
+The Doctrine Console is a Command Line Interface tool for simplifying common
  8
+administration tasks during the development of a project that uses Doctrine 2.
10 9
 
11  
-Take a look at the :doc:`Configuration <configuration>` for more
12  
-information how to setup the console command.
  10
+Take a look at the :doc:`Installation and Configuration <configuration>`
  11
+chapter for more information how to setup the console command.
13 12
 
14  
-Getting Help
15  
-~~~~~~~~~~~~
  13
+Display Help Information
  14
+~~~~~~~~~~~~~~~~~~~~~~~~
16 15
 
17  
-Type ``php vendor/bin/doctrine-orm`` on the command line and you should see an
  16
+Type ``php vendor/bin/doctrine`` on the command line and you should see an
18 17
 overview of the available commands or use the --help flag to get
19 18
 information on the available commands. If you want to know more
20 19
 about the use of generate entities for example, you can call:
21 20
 
22 21
 .. code-block:: php
23 22
 
24  
-    $> php vendor/bin/doctrine-orm orm:generate-entities --help
  23
+    $> php vendor/bin/doctrine orm:generate-entities --help
25 24
 
26 25
 
27 26
 Configuration
28 27
 ~~~~~~~~~~~~~
29 28
 
30  
-Whenever the ``doctrine-orm`` command line tool is invoked, it can
  29
+Whenever the ``doctrine`` command line tool is invoked, it can
31 30
 access all Commands that were registered by developer. There is no
32 31
 auto-detection mechanism at work. The Doctrine binary
33 32
 already registers all the commands that currently ship with
41  lib/Doctrine/ORM/Tools/Console/ConsoleRunner.php
@@ -23,9 +23,28 @@
23 23
 use Symfony\Component\Console\Helper\HelperSet;
24 24
 use Doctrine\ORM\Version;
25 25
 
  26
+use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
  27
+use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
  28
+
  29
+/**
  30
+ * Handles running the Console Tools inside Symfony Console context.
  31
+ */
26 32
 class ConsoleRunner
27 33
 {
28 34
     /**
  35
+     * Create a Symfony Console HelperSet
  36
+     *
  37
+     * @return HelperSet
  38
+     */
  39
+    public static function createHelperSet(EntityManager $entityManager)
  40
+    {
  41
+        return new HelperSet(array(
  42
+            'db' => new ConnectionHelper($entityManager->getConnection()),
  43
+            'em' => new EntityManagerHelper($entityManager)
  44
+        ));
  45
+    }
  46
+
  47
+    /**
29 48
      * Runs console with the given helperset.
30 49
      *
31 50
      * @param \Symfony\Component\Console\Helper\HelperSet  $helperSet
@@ -73,4 +92,26 @@ static public function addCommands(Application $cli)
73 92
             new \Doctrine\ORM\Tools\Console\Command\InfoCommand()
74 93
         ));
75 94
     }
  95
+
  96
+    static public function printCliConfigTemplate()
  97
+    {
  98
+        echo <<<'HELP'
  99
+You are missing a "cli-config.php" or "config/cli-config.php" file in your
  100
+project, which is required to get the Doctrine Console working. You can use the
  101
+following sample as a template:
  102
+
  103
+<?php
  104
+use Doctrine\ORM\Tools\Console\ConsoleRunner;
  105
+
  106
+// replace with file to your own project bootstrap
  107
+require_once 'bootstrap.php';
  108
+
  109
+// replace with mechanism to retrieve EntityManager in your app
  110
+$entityManager = GetEntityManager();
  111
+
  112
+return ConsoleRunner::createHelperSet($entityManager);
  113
+
  114
+HELP;
  115
+
  116
+    }
76 117
 }

0 notes on commit a7d764f

Please sign in to comment.
Something went wrong with that request. Please try again.