|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace LaravelDoctrine\ORM\Console; |
| 4 | + |
| 5 | +use Doctrine\Common\Persistence\ManagerRegistry; |
| 6 | +use Doctrine\ORM\EntityManagerInterface; |
| 7 | +use Doctrine\ORM\Tools\Console\MetadataFilter; |
| 8 | +use Doctrine\ORM\Tools\EntityRepositoryGenerator; |
| 9 | +use InvalidArgumentException; |
| 10 | + |
| 11 | +class GenerateRepositoriesCommand extends Command |
| 12 | +{ |
| 13 | + /** |
| 14 | + * The name and signature of the console command. |
| 15 | + * @var string |
| 16 | + */ |
| 17 | + protected $signature = 'doctrine:generate:repositories |
| 18 | + {dest-path? : The path to generate your proxy classes. If none is provided, it will attempt to grab from configuration.} |
| 19 | + {-- filter=* : A string pattern used to match entities that should be processed.} |
| 20 | + {--em= : Generate proxies for a specific entity manager }'; |
| 21 | + |
| 22 | + /** |
| 23 | + * The console command description. |
| 24 | + * @var string |
| 25 | + */ |
| 26 | + protected $description = 'Generate repository classes from your entity classes.'; |
| 27 | + |
| 28 | + /** |
| 29 | + * Execute the console command. |
| 30 | + * |
| 31 | + * @param ManagerRegistry $registry |
| 32 | + */ |
| 33 | + public function handle(ManagerRegistry $registry) |
| 34 | + { |
| 35 | + $names = $this->option('em') ? [$this->option('em')] : $registry->getManagerNames(); |
| 36 | + |
| 37 | + foreach ($names as $name) { |
| 38 | + /** @var EntityManagerInterface $em */ |
| 39 | + $em = $registry->getManager($name); |
| 40 | + |
| 41 | + $this->comment(''); |
| 42 | + $this->message('Generating repositories for <info>' . $name . '</info> entity manager...', 'blue'); |
| 43 | + |
| 44 | + $metadatas = $em->getMetadataFactory()->getAllMetadata(); |
| 45 | + $metadatas = MetadataFilter::filter($metadatas, $this->option('filter')); |
| 46 | + |
| 47 | + $repositoryName = $em->getConfiguration()->getDefaultRepositoryClassName(); |
| 48 | + |
| 49 | + $destPath = base_path($this->argument('dest-path') ?:'app/Repositories'); |
| 50 | + |
| 51 | + if (!is_dir($destPath)) { |
| 52 | + mkdir($destPath, 0777, true); |
| 53 | + } |
| 54 | + |
| 55 | + $destPath = realpath($destPath); |
| 56 | + |
| 57 | + if (!file_exists($destPath)) { |
| 58 | + throw new InvalidArgumentException( |
| 59 | + sprintf("Repositories destination directory '<info>%s</info>' does not exist.", |
| 60 | + $em->getConfiguration()->getProxyDir()) |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + if (!is_writable($destPath)) { |
| 65 | + throw new InvalidArgumentException( |
| 66 | + sprintf("Repositories destination directory '<info>%s</info>' does not have write permissions.", |
| 67 | + $destPath) |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + if (empty($metadatas)) { |
| 72 | + $this->error('No Metadata Classes to process.'); |
| 73 | + |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + $numRepositories = 0; |
| 78 | + $generator = new EntityRepositoryGenerator(); |
| 79 | + |
| 80 | + $generator->setDefaultRepositoryName($repositoryName); |
| 81 | + |
| 82 | + foreach ($metadatas as $metadata) { |
| 83 | + if ($metadata->customRepositoryClassName) { |
| 84 | + $this->comment( |
| 85 | + sprintf('Processing repository "<info>%s</info>"', $metadata->customRepositoryClassName) |
| 86 | + ); |
| 87 | + |
| 88 | + $generator->writeEntityRepositoryClass($metadata->customRepositoryClassName, $destPath); |
| 89 | + |
| 90 | + ++$numRepositories; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + if ($numRepositories === 0) { |
| 95 | + $this->error('No Repository classes were found to be processed.'); |
| 96 | + |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + $this->comment(sprintf('Repository classes generated to "<info>%s</info>"', $destPath)); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments