. */ namespace Doctrine\ORM\Tools\Console\Command; use Symfony\Components\Console\Input\InputArgument, Symfony\Components\Console\Input\InputOption, Symfony\Components\Console; /** * Command to generate repository classes for mapping information. * * @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @link www.doctrine-project.org * @since 2.0 * @version $Revision$ * @author Benjamin Eberlei * @author Guilherme Blanco * @author Jonathan Wage * @author Roman Borschel */ class GenerateRepositoriesCommand extends Console\Command\Command { private static $_template = '; use \Doctrine\ORM\EntityRepository; /** * * * This class was generated by the Doctrine ORM. Add your own custom * repository methods below. */ class extends EntityRepository { }'; /** * @see Console\Command\Command */ protected function configure() { $this ->setName('orm:generate-repositories') ->setDescription('Generate repository classes from your mapping information.') ->setDefinition(array( new InputArgument( 'from-path', InputArgument::REQUIRED, 'The path of mapping information.' ), new InputArgument( 'dest-path', InputArgument::REQUIRED, 'The path to generate your repository classes.' ), new InputOption( 'from', null, InputOption::PARAMETER_REQUIRED | InputOption::PARAMETER_IS_ARRAY, 'Optional paths of mapping information.', array() ) )) ->setHelp(<<getHelper('em')->getEntityManager(); $reader = new ClassMetadataReader(); $reader->setEntityManager($em); // Process source directories $fromPaths = array_merge(array($input->getArgument('from-path')), $input->getOption('from')); foreach ($fromPaths as $dirName) { $dirName = realpath($dirName); if ( ! file_exists($dirName)) { throw new \InvalidArgumentException( sprintf("Mapping directory '%s' does not exist.", $dirName) ); } else if ( ! is_readable($dirName)) { throw new \InvalidArgumentException( sprintf("Mapping directory '%s' does not have read permissions.", $dirName) ); } $reader->addMappingSource($dirName); } // Process destination directory $destPath = realpath($input->getArgument('dest-path')); if ( ! file_exists($destPath)) { throw new \InvalidArgumentException( sprintf("Entities destination directory '%s' does not exist.", $destPath) ); } else if ( ! is_writable($destPath)) { throw new \InvalidArgumentException( sprintf("Entities destination directory '%s' does not have write permissions.", $destPath) ); } // Retrieving ClassMetadatas $metadatas = $reader->getMetadatas(); if ( ! empty($metadatas)) { $numRepositories = 0; foreach ($metadatas as $metadata) { if ($metadata->customRepositoryClassName) { $output->write( sprintf('Processing repository "%s"', $metadata->customRepositoryClassName) . PHP_EOL ); $this->_generateRepositoryClass($metadata, $destPath); $numRepositories++; } } if ($numRepositories) { // Outputting information message $output->write(PHP_EOL . sprintf('Repository classes generated to "%s"', $destPath) . PHP_EOL); } else { $output->write('No Repository classes were found to be processed.' . PHP_EOL); } } else { $output->write('No Metadata Classes to process.' . PHP_EOL); } } private function _generateRepositoryClass($metadata, $destPath) { $code = $this->_generateRepositoryClass($metadata->customRepositoryClassName); $path = $destPath . DIRECTORY_SEPARATOR . str_replace('\\', \DIRECTORY_SEPARATOR, $metadata->customRepositoryClassName) . '.php'; $dir = dirname($path); if ( ! is_dir($dir)) { mkdir($dir, 0777, true); } file_put_contents($path, $code); } }