protected function getUserEntityClass(InputInterface $input, OutputInterface $output) { if ($this->userEntityClass === '') { if ($input->getOption('user_entity')) { list($userBundle, $userEntity) = Validators::validateEntityName($input->getOption('user_entity')); $this->userEntityClass = $this->getContainer()->get('doctrine')->getEntityNamespace($userBundle) . '\\' . $userEntity; } else { list($userBundle, $userEntity) = $this->getHelperSet()->get('dialog')->askAndValidate($output, 'Please enter the User Entity shortcut name: ', 'Sonata\\AdminBundle\\Command\\Validators::validateEntityName'); // Entity exists? $this->userEntityClass = $this->getContainer()->get('doctrine')->getEntityNamespace($userBundle) . '\\' . $userEntity; } } return $this->userEntityClass; }
/** * {@inheritDoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $modelClass = Validators::validateClass($input->getArgument('model')); $modelClassBasename = current(array_slice(explode('\\', $modelClass), -1)); $bundle = $this->getBundle($input->getOption('bundle') ?: $this->getBundleNameFromClass($modelClass)); $adminClassBasename = $input->getOption('admin') ?: $modelClassBasename . 'Admin'; $adminClassBasename = Validators::validateAdminClassBasename($adminClassBasename); $managerType = $input->getOption('manager') ?: $this->getDefaultManagerType(); $modelManager = $this->getModelManager($managerType); $skeletonDirectory = __DIR__ . '/../Resources/skeleton'; $adminGenerator = new AdminGenerator($modelManager, $skeletonDirectory); try { $adminGenerator->generate($bundle, $adminClassBasename, $modelClass); $output->writeln(sprintf('%sThe admin class "<info>%s</info>" has been generated under the file "<info>%s</info>".', PHP_EOL, $adminGenerator->getClass(), realpath($adminGenerator->getFile()))); } catch (\Exception $e) { $this->writeError($output, $e->getMessage()); } if ($controllerClassBasename = $input->getOption('controller')) { $controllerClassBasename = Validators::validateControllerClassBasename($controllerClassBasename); $controllerGenerator = new ControllerGenerator($skeletonDirectory); try { $controllerGenerator->generate($bundle, $controllerClassBasename); $output->writeln(sprintf('%sThe controller class "<info>%s</info>" has been generated under the file "<info>%s</info>".', PHP_EOL, $controllerGenerator->getClass(), realpath($controllerGenerator->getFile()))); } catch (\Exception $e) { $this->writeError($output, $e->getMessage()); } } if ($servicesFile = $input->getOption('services')) { $adminClass = $adminGenerator->getClass(); $file = sprintf('%s/Resources/config/%s', $bundle->getPath(), $servicesFile); $servicesManipulator = new ServicesManipulator($file); $controllerName = $controllerClassBasename ? sprintf('%s:%s', $bundle->getName(), substr($controllerClassBasename, 0, -10)) : 'SonataAdminBundle:CRUD'; try { $id = $input->getOption('id') ?: $this->getAdminServiceId($bundle->getName(), $adminClassBasename); $servicesManipulator->addResource($id, $modelClass, $adminClass, $controllerName, $managerType); $output->writeln(sprintf('%sThe service "<info>%s</info>" has been appended to the file <info>"%s</info>".', PHP_EOL, $id, realpath($file))); } catch (\Exception $e) { $this->writeError($output, $e->getMessage()); } } return 0; }
/** * @dataProvider getValidateServiceIdWithExceptionTests */ public function testValidateServiceIdWithException($value) { $this->setExpectedException('\\InvalidArgumentException'); Validators::validateServiceId($value); }