1
+ <?php
2
+ /*
3
+ * $Id$
4
+ *
5
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16
+ *
17
+ * This software consists of voluntary contributions made by many individuals
18
+ * and is licensed under the LGPL. For more information, see
19
+ * <http://www.doctrine-project.org>.
20
+ */
21
+
22
+ namespace Doctrine \ODM \MongoDB \Tools \Console \Command ;
23
+
24
+ use Symfony \Component \Console \Input \InputArgument ,
25
+ Symfony \Component \Console \Input \InputOption ,
26
+ Symfony \Component \Console ,
27
+ Doctrine \ODM \MongoDB \Tools \Console \MetadataFilter ;
28
+
29
+ /**
30
+ * Command to (re)generate the hydrator classes used by doctrine.
31
+ *
32
+ * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
33
+ * @link www.doctrine-project.org
34
+ * @since 2.0
35
+ * @version $Revision$
36
+ * @author Benjamin Eberlei <kontakt@beberlei.de>
37
+ * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
38
+ * @author Jonathan Wage <jonwage@gmail.com>
39
+ * @author Roman Borschel <roman@code-factory.org>
40
+ */
41
+ class GenerateHydratorsCommand extends Console \Command \Command
42
+ {
43
+ /**
44
+ * @see Console\Command\Command
45
+ */
46
+ protected function configure ()
47
+ {
48
+ $ this
49
+ ->setName ('odm:generate:hydrators ' )
50
+ ->setDescription ('Generates hydrator classes for document classes. ' )
51
+ ->setDefinition (array (
52
+ new InputOption (
53
+ 'filter ' , null , InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY ,
54
+ 'A string pattern used to match documents that should be processed. '
55
+ ),
56
+ new InputArgument (
57
+ 'dest-path ' , InputArgument::OPTIONAL ,
58
+ 'The path to generate your hydrator classes. If none is provided, it will attempt to grab from configuration. '
59
+ ),
60
+ ))
61
+ ->setHelp (<<<EOT
62
+ Generates hydrator classes for document classes.
63
+ EOT
64
+ );
65
+ }
66
+
67
+ /**
68
+ * @see Console\Command\Command
69
+ */
70
+ protected function execute (Console \Input \InputInterface $ input , Console \Output \OutputInterface $ output )
71
+ {
72
+ $ dm = $ this ->getHelper ('dm ' )->getDocumentManager ();
73
+
74
+ $ metadatas = $ dm ->getMetadataFactory ()->getAllMetadata ();
75
+ $ metadatas = MetadataFilter::filter ($ metadatas , $ input ->getOption ('filter ' ));
76
+
77
+ // Process destination directory
78
+ if (($ destPath = $ input ->getArgument ('dest-path ' )) === null ) {
79
+ $ destPath = $ dm ->getConfiguration ()->getHydratorDir ();
80
+ }
81
+
82
+ if ( ! is_dir ($ destPath )) {
83
+ mkdir ($ destPath , 0777 , true );
84
+ }
85
+
86
+ $ destPath = realpath ($ destPath );
87
+
88
+ if ( ! file_exists ($ destPath )) {
89
+ throw new \InvalidArgumentException (
90
+ sprintf ("Hydrators destination directory '<info>%s</info>' does not exist. " , $ destPath )
91
+ );
92
+ } else if ( ! is_writable ($ destPath )) {
93
+ throw new \InvalidArgumentException (
94
+ sprintf ("Hydrators destination directory '<info>%s</info>' does not have write permissions. " , $ destPath )
95
+ );
96
+ }
97
+
98
+ if (count ($ metadatas )) {
99
+ foreach ($ metadatas as $ metadata ) {
100
+ $ output ->write (
101
+ sprintf ('Processing document "<info>%s</info>" ' , $ metadata ->name ) . PHP_EOL
102
+ );
103
+ }
104
+
105
+ // Generating Hydrators
106
+ $ dm ->getHydratorFactory ()->generateHydratorClasses ($ metadatas , $ destPath );
107
+
108
+ // Outputting information message
109
+ $ output ->write (PHP_EOL . sprintf ('Hydrator classes generated to "<info>%s</INFO>" ' , $ destPath ) . PHP_EOL );
110
+ } else {
111
+ $ output ->write ('No Metadata Classes to process. ' . PHP_EOL );
112
+ }
113
+ }
114
+ }
0 commit comments