* @author Jonathan Wage * @author Roman Borschel */ class GenerateProxiesTask extends AbstractTask { /** * @inheritdoc */ public function buildDocumentation() { $classDir = new OptionGroup(OptionGroup::CARDINALITY_1_1, array( new Option('class-dir', '', 'Specified directory where mapping classes are located.') )); $toDir = new OptionGroup(OptionGroup::CARDINALITY_0_1, array( new Option('to-dir', '', 'Generates the classes in the specified directory.') )); $doc = $this->getDocumentation(); $doc->setName('generate-proxies') ->setDescription('Generates proxy classes for entity classes.') ->getOptionGroup() ->addOption($classDir) ->addOption($toDir); } /** * @inheritdoc */ public function validate() { $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." ); } $metadataDriver = $em->getConfiguration()->getMetadataDriverImpl(); if ($metadataDriver instanceof \Doctrine\ORM\Mapping\Driver\AnnotationDriver) { if (isset($arguments['class-dir'])) { $metadataDriver->addPaths((array) $arguments['class-dir']); } else { throw new CliException( 'The supplied configuration uses the annotation metadata driver. ' . "The 'class-dir' argument is required for this driver." ); } } return true; } /** * @inheritdoc */ public function run() { $arguments = $this->getArguments(); $printer = $this->getPrinter(); $em = $this->getConfiguration()->getAttribute('em'); $cmf = $em->getMetadataFactory(); $classes = $cmf->getAllMetadata(); $factory = $em->getProxyFactory(); if (empty($classes)) { $printer->writeln('No classes to process.', 'INFO'); } else { $factory->generateProxyClasses( $classes, isset($arguments['to-dir']) ? $arguments['to-dir'] : null ); $printer->writeln( 'Proxy classes generated to: ' . (isset($arguments['to-dir']) ? $arguments['to-dir'] : $em->getConfiguration()->getProxyDir()) ); } } }