コード例 #1
0
ファイル: EntityMetadata.php プロジェクト: LartTyler/Link
 public function __construct(string $klass)
 {
     $this->klass = $klass;
     $this->entity = substr($klass, strrpos($klass, '\\') + 1);
     $this->table = strtolower(StringUtil::pluralize($this->entity));
     if (EntitiesConfiguration::instance()->getConnection() === null) {
         throw new \BadMethodCallException('Could not establish database connection');
     }
     $this->reader = new SqlReader($this->table, EntitiesConfiguration::getConnection());
 }
コード例 #2
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     if (!parent::getHelperSet()->has('link.connection')) {
         throw new \BadMethodCallException('You MUST provide a connection helper to run this command.');
     }
     /** @var Connection $connection */
     $connection = parent::getHelper('link.connection')->getConnection();
     $dest = parent::getHelperSet()->has('link.destination') ? $this->getDestinationHelper()->getDestination() : $input->getArgument('destination');
     $klass = str_replace('/', '\\', $input->getArgument('entity'));
     $entityName = substr($klass, strrpos($klass, '\\') + 1);
     $reader = new SqlReader(StringUtil::pluralize(strtolower($entityName)), $connection);
     $relations = [];
     foreach ($reader->getConstraints() as $constraint) {
         if (!$constraint instanceof ForeignKeyConstraint) {
             continue;
         }
         $relations = array_merge($relations, array_map(function (ColumnInterface $column) {
             return $column->getField();
         }, $constraint->getColumns()));
     }
     $classBuilder = ClassBuilder::create($entityName, null, substr($klass, 0, strrpos($klass, '\\')))->extends(Entity::class);
     $commentBuilder = BlockCommentBuilder::create();
     foreach ($reader->getColumns() as $column) {
         if (in_array($column->getField(), $relations)) {
             continue;
         }
         $retType = ColumnTypeHelper::toPhpType($column->getType());
         $commentBuilder->addDocMethod($this->buildGetterName($column, $retType), false, [$retType]);
         if ($column->getField() !== 'id') {
             $field = StringUtil::classify($column->getField());
             $commentBuilder->addDocMethod('set' . $field, false, ['$this'], [lcfirst($field) => ColumnTypeHelper::toPhpType($column->getType(), null)]);
         }
     }
     if (sizeof($relations) > 0) {
         $output->writeln(['<comment>This entity has foreign keys, which means it probably relates another entity (or ', 'entities) in your application. I cannot generate them, so you will need to add them yourself. ', 'Please see https://goo.gl/dFvGYo for information on how to do this.', '</>']);
     }
     $classBuilder->comment($commentBuilder->build());
     if (!file_exists($dest)) {
         mkdir($dest, 0755, true);
     }
     if (file_exists($filePath = $dest . $entityName . '.php') && !$input->getOption('force')) {
         throw new \InvalidArgumentException('File already exists at ' . $dest);
     }
     $result = file_put_contents($filePath, '<?php' . PHP_EOL . $classBuilder->build()->build(1));
     if ($result) {
         $output->writeln('<info>Generated class written to ' . $filePath . '</>');
     } else {
         $output->writeln('<error>Could not write to ' . $filePath . '</>');
     }
 }