2009-10-15 00:18:36 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Doctrine\ORM\Tools\Cli\Tasks;
|
|
|
|
|
2009-12-21 20:38:14 +03:00
|
|
|
use Doctrine\Common\Cli\Tasks\AbstractTask,
|
|
|
|
Doctrine\Common\Cli\CliException,
|
2009-11-17 04:29:20 +03:00
|
|
|
Doctrine\Common\Cli\Option,
|
|
|
|
Doctrine\Common\Cli\OptionGroup;
|
|
|
|
|
2009-10-15 00:18:36 +04:00
|
|
|
/**
|
|
|
|
* Task to (re)generate the proxy classes used by doctrine.
|
|
|
|
*
|
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @link www.doctrine-project.org
|
|
|
|
* @since 2.0
|
|
|
|
* @version $Revision: 3938 $
|
|
|
|
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
|
|
|
* @author Jonathan Wage <jonwage@gmail.com>
|
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
|
|
|
*/
|
|
|
|
class GenerateProxiesTask extends AbstractTask
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2009-11-17 04:29:20 +03:00
|
|
|
public function buildDocumentation()
|
2009-10-15 00:18:36 +04:00
|
|
|
{
|
2009-12-21 20:38:14 +03:00
|
|
|
$classDir = new OptionGroup(OptionGroup::CARDINALITY_1_1, array(
|
|
|
|
new Option('class-dir', '<PATH>', 'Specified directory where mapping classes are located.')
|
|
|
|
));
|
|
|
|
|
2009-11-17 04:29:20 +03:00
|
|
|
$toDir = new OptionGroup(OptionGroup::CARDINALITY_0_1, array(
|
|
|
|
new Option('to-dir', '<PATH>', 'Generates the classes in the specified directory.')
|
|
|
|
));
|
2009-10-15 00:18:36 +04:00
|
|
|
|
2009-11-17 04:29:20 +03:00
|
|
|
$doc = $this->getDocumentation();
|
|
|
|
$doc->setName('generate-proxies')
|
|
|
|
->setDescription('Generates proxy classes for entity classes.')
|
|
|
|
->getOptionGroup()
|
2009-12-21 20:38:14 +03:00
|
|
|
->addOption($classDir)
|
2009-11-17 04:29:20 +03:00
|
|
|
->addOption($toDir);
|
2009-10-15 00:18:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public function validate()
|
|
|
|
{
|
2009-12-21 20:38:14 +03:00
|
|
|
$arguments = $this->getArguments();
|
|
|
|
$em = $this->getConfiguration()->getAttribute('em');
|
|
|
|
|
|
|
|
if ($em === null) {
|
|
|
|
throw new CliException(
|
|
|
|
"Attribute 'em' of CLI Configuration is not defined or it is not a valid EntityManager."
|
|
|
|
);
|
|
|
|
}
|
2009-10-15 00:18:36 +04:00
|
|
|
|
2009-12-21 20:38:14 +03:00
|
|
|
$metadataDriver = $em->getConfiguration()->getMetadataDriverImpl();
|
2009-10-15 00:18:36 +04:00
|
|
|
|
|
|
|
if ($metadataDriver instanceof \Doctrine\ORM\Mapping\Driver\AnnotationDriver) {
|
2009-12-21 20:38:14 +03:00
|
|
|
if (isset($arguments['class-dir'])) {
|
2010-01-28 15:46:12 +03:00
|
|
|
$metadataDriver->addPaths((array) $arguments['class-dir']);
|
2009-12-21 20:38:14 +03:00
|
|
|
} else {
|
|
|
|
throw new CliException(
|
|
|
|
'The supplied configuration uses the annotation metadata driver. ' .
|
|
|
|
"The 'class-dir' argument is required for this driver."
|
|
|
|
);
|
2009-10-15 00:18:36 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-12-21 20:38:14 +03:00
|
|
|
* @inheritdoc
|
2009-10-15 00:18:36 +04:00
|
|
|
*/
|
|
|
|
public function run()
|
|
|
|
{
|
2009-12-21 20:38:14 +03:00
|
|
|
$arguments = $this->getArguments();
|
|
|
|
$printer = $this->getPrinter();
|
2009-10-15 00:18:36 +04:00
|
|
|
|
2009-12-22 18:45:16 +03:00
|
|
|
$em = $this->getConfiguration()->getAttribute('em');
|
2009-12-21 20:38:14 +03:00
|
|
|
$cmf = $em->getMetadataFactory();
|
2009-12-16 00:06:32 +03:00
|
|
|
$classes = $cmf->getAllMetadata();
|
2009-10-30 03:15:53 +03:00
|
|
|
$factory = $em->getProxyFactory();
|
2009-10-15 00:18:36 +04:00
|
|
|
|
|
|
|
if (empty($classes)) {
|
|
|
|
$printer->writeln('No classes to process.', 'INFO');
|
2009-12-21 20:38:14 +03:00
|
|
|
} else {
|
2010-03-19 07:19:15 +03:00
|
|
|
foreach ($classes as $class) {
|
|
|
|
$printer->writeln(
|
|
|
|
sprintf('Processing entity "%s"', $printer->format($class->name, 'KEYWORD'))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2009-12-21 20:38:14 +03:00
|
|
|
$factory->generateProxyClasses(
|
|
|
|
$classes, isset($arguments['to-dir']) ? $arguments['to-dir'] : null
|
|
|
|
);
|
2010-03-19 07:19:15 +03:00
|
|
|
|
|
|
|
$printer->writeln('');
|
|
|
|
|
2009-12-21 20:38:14 +03:00
|
|
|
$printer->writeln(
|
2010-03-19 07:19:15 +03:00
|
|
|
sprintf('Proxy classes generated to "%s"',
|
|
|
|
$printer->format(isset($arguments['to-dir']) ? $arguments['to-dir'] : $em->getConfiguration()->getProxyDir(), 'KEYWORD'))
|
2009-12-21 20:38:14 +03:00
|
|
|
);
|
|
|
|
}
|
2009-10-15 00:18:36 +04:00
|
|
|
}
|
|
|
|
}
|