Skip to content

Commit 56fc267

Browse files
committed
[MODM-112] Adding missing console commands for generating hydrator and proxy classes.
1 parent 8bb20c1 commit 56fc267

File tree

6 files changed

+332
-3
lines changed

6 files changed

+332
-3
lines changed

lib/Doctrine/ODM/MongoDB/Hydrator/HydratorFactory.php

+19
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,25 @@ public function getHydratorFor($className)
148148
return $this->hydrators[$className];
149149
}
150150

151+
/**
152+
* Generates hydrator classes for all given classes.
153+
*
154+
* @param array $classes The classes (ClassMetadata instances) for which to generate hydrators.
155+
* @param string $toDir The target directory of the hydrator classes. If not specified, the
156+
* directory configured on the Configuration of the DocumentManager used
157+
* by this factory is used.
158+
*/
159+
public function generateHydratorClasses(array $classes, $toDir = null)
160+
{
161+
$hydratorDir = $toDir ?: $this->hydratorDir;
162+
$hydratorDir = rtrim($hydratorDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
163+
foreach ($classes as $class) {
164+
$hydratorClassName = str_replace('\\', '', $class->name) . 'Proxy';
165+
$hydratorFileName = $hydratorDir . $hydratorClassName . '.php';
166+
$this->generateHydratorClass($class, $hydratorClassName, $hydratorFileName);
167+
}
168+
}
169+
151170
private function generateHydratorClass(ClassMetadata $class, $hydratorClassName, $fileName)
152171
{
153172
$code = '';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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 proxy 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 GenerateProxiesCommand extends Console\Command\Command
42+
{
43+
/**
44+
* @see Console\Command\Command
45+
*/
46+
protected function configure()
47+
{
48+
$this
49+
->setName('odm:generate:proxies')
50+
->setDescription('Generates proxy 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 proxy classes. If none is provided, it will attempt to grab from configuration.'
59+
),
60+
))
61+
->setHelp(<<<EOT
62+
Generates proxy 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()->getProxyDir();
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("Proxies destination directory '<info>%s</info>' does not exist.", $destPath)
91+
);
92+
} else if ( ! is_writable($destPath)) {
93+
throw new \InvalidArgumentException(
94+
sprintf("Proxies 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 Proxies
106+
$dm->getProxyFactory()->generateProxyClasses($metadatas, $destPath);
107+
108+
// Outputting information message
109+
$output->write(PHP_EOL . sprintf('Proxy classes generated to "<info>%s</INFO>"', $destPath) . PHP_EOL);
110+
} else {
111+
$output->write('No Metadata Classes to process.' . PHP_EOL);
112+
}
113+
}
114+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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;
23+
24+
/**
25+
* Used by CLI Tools to restrict entity-based commands to given patterns.
26+
*
27+
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
28+
* @link www.doctrine-project.com
29+
* @since 1.0
30+
* @version $Revision$
31+
* @author Benjamin Eberlei <kontakt@beberlei.de>
32+
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
33+
* @author Jonathan Wage <jonwage@gmail.com>
34+
* @author Roman Borschel <roman@code-factory.org>
35+
*/
36+
class MetadataFilter extends \FilterIterator implements \Countable
37+
{
38+
/**
39+
* Filter Metadatas by one or more filter options.
40+
*
41+
* @param array $metadatas
42+
* @param array|string $filter
43+
* @return array
44+
*/
45+
static public function filter(array $metadatas, $filter)
46+
{
47+
$metadatas = new MetadataFilter(new \ArrayIterator($metadatas), $filter);
48+
return iterator_to_array($metadatas);
49+
}
50+
51+
private $_filter = array();
52+
53+
public function __construct(\ArrayIterator $metadata, $filter)
54+
{
55+
$this->_filter = (array)$filter;
56+
parent::__construct($metadata);
57+
}
58+
59+
public function accept()
60+
{
61+
if (count($this->_filter) == 0) {
62+
return true;
63+
}
64+
65+
$it = $this->getInnerIterator();
66+
$metadata = $it->current();
67+
68+
foreach ($this->_filter AS $filter) {
69+
if (strpos($metadata->name, $filter) !== false) {
70+
return true;
71+
}
72+
}
73+
return false;
74+
}
75+
76+
public function count()
77+
{
78+
return count($this->getInnerIterator());
79+
}
80+
}

tools/sandbox/cli-config.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
require_once 'config.php';
44

55
$helpers = array(
6-
new Doctrine\ODM\MongoDB\Tools\Console\Helper\DocumentManagerHelper($dm),
6+
'dm' => new Doctrine\ODM\MongoDB\Tools\Console\Helper\DocumentManagerHelper($dm),
77
);

tools/sandbox/mongodb.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
require __DIR__ . DIRECTORY_SEPARATOR . 'cli-config.php';
44

55
$helperSet = isset($helperSet) ? $helperSet : new \Symfony\Component\Console\Helper\HelperSet();
6-
foreach ($helpers as $helper) {
7-
$helperSet->set($helper);
6+
foreach ($helpers as $name => $helper) {
7+
$helperSet->set($helper, $name);
88
}
99

1010
$cli = new \Symfony\Component\Console\Application('Doctrine ODM MongoDB Command Line Interface', Doctrine\ODM\MongoDB\Version::VERSION);
1111
$cli->setCatchExceptions(true);
1212
$cli->setHelperSet($helperSet);
1313
$cli->addCommands(array(
14+
new \Doctrine\ODM\MongoDB\Tools\Console\Command\GenerateProxiesCommand(),
15+
new \Doctrine\ODM\MongoDB\Tools\Console\Command\GenerateHydratorsCommand(),
1416
new \Doctrine\ODM\MongoDB\Tools\Console\Command\Schema\CreateCommand(),
1517
new \Doctrine\ODM\MongoDB\Tools\Console\Command\Schema\DropCommand(),
1618
));

0 commit comments

Comments
 (0)