/**
  * Kickstart a new action controller
  *
  * Generates an Action Controller with the given name in the specified package.
  * In its default mode it will create just the controller containing a sample
  * indexAction.
  *
  * By specifying the --generate-actions flag, this command will also create a
  * set of actions. If no model or repository exists which matches the
  * controller name (for example "CoffeeRepository" for "CoffeeController"),
  * an error will be shown.
  *
  * Likewise the command exits with an error if the specified package does not
  * exist. By using the --generate-related flag, a missing package, model or
  * repository can be created alongside, avoiding such an error.
  *
  * By specifying the --generate-templates flag, this command will also create
  * matching Fluid templates for the actions created. This option can only be
  * used in combination with --generate-actions.
  *
  * The default behavior is to not overwrite any existing code. This can be
  * overridden by specifying the --force flag.
  *
  * @param string $packageKey The package key of the package for the new controller with an optional subpackage, (e.g. "MyCompany.MyPackage/Admin").
  * @param string $controllerName The name for the new controller. This may also be a comma separated list of controller names.
  * @param boolean $generateActions Also generate index, show, new, create, edit, update and delete actions.
  * @param boolean $generateTemplates Also generate the templates for each action.
  * @param boolean $generateRelated Also create the mentioned package, related model and repository if neccessary.
  * @param boolean $force Overwrite any existing controller or template code. Regardless of this flag, the package, model and repository will never be overwritten.
  * @return string
  * @see typo3.kickstart:kickstart:commandcontroller
  */
 public function actionControllerCommand($packageKey, $controllerName, $generateActions = FALSE, $generateTemplates = TRUE, $generateRelated = FALSE, $force = FALSE)
 {
     $subpackageName = '';
     if (strpos($packageKey, '/') !== FALSE) {
         list($packageKey, $subpackageName) = explode('/', $packageKey, 2);
     }
     $this->validatePackageKey($packageKey);
     if (!$this->packageManager->isPackageAvailable($packageKey)) {
         if ($generateRelated === FALSE) {
             $this->outputLine('Package "%s" is not available.', array($packageKey));
             $this->outputLine('Hint: Use --generate-related for creating it!');
             exit(2);
         }
         $this->packageManager->createPackage($packageKey);
     }
     $generatedFiles = array();
     $generatedModels = FALSE;
     $controllerNames = \TYPO3\Flow\Utility\Arrays::trimExplode(',', $controllerName);
     if ($generateActions === TRUE) {
         foreach ($controllerNames as $currentControllerName) {
             $modelClassName = str_replace('.', '\\', $packageKey) . '\\Domain\\Model\\' . $currentControllerName;
             if (!class_exists($modelClassName)) {
                 if ($generateRelated === TRUE) {
                     $generatedFiles += $this->generatorService->generateModel($packageKey, $currentControllerName, array('name' => array('type' => 'string')));
                     $generatedModels = TRUE;
                 } else {
                     $this->outputLine('The model %s does not exist, but is necessary for creating the respective actions.', array($modelClassName));
                     $this->outputLine('Hint: Use --generate-related for creating it!');
                     exit(3);
                 }
             }
             $repositoryClassName = str_replace('.', '\\', $packageKey) . '\\Domain\\Repository\\' . $currentControllerName . 'Repository';
             if (!class_exists($repositoryClassName)) {
                 if ($generateRelated === TRUE) {
                     $generatedFiles += $this->generatorService->generateRepository($packageKey, $currentControllerName);
                 } else {
                     $this->outputLine('The repository %s does not exist, but is necessary for creating the respective actions.', array($repositoryClassName));
                     $this->outputLine('Hint: Use --generate-related for creating it!');
                     exit(4);
                 }
             }
         }
     }
     foreach ($controllerNames as $currentControllerName) {
         if ($generateActions === TRUE) {
             $generatedFiles += $this->generatorService->generateCrudController($packageKey, $subpackageName, $currentControllerName, $force);
         } else {
             $generatedFiles += $this->generatorService->generateActionController($packageKey, $subpackageName, $currentControllerName, $force);
         }
         if ($generateTemplates === TRUE) {
             $generatedFiles += $this->generatorService->generateLayout($packageKey, 'Default', $force);
             if ($generateActions === TRUE) {
                 $generatedFiles += $this->generatorService->generateView($packageKey, $subpackageName, $currentControllerName, 'Index', 'Index', $force);
                 $generatedFiles += $this->generatorService->generateView($packageKey, $subpackageName, $currentControllerName, 'New', 'New', $force);
                 $generatedFiles += $this->generatorService->generateView($packageKey, $subpackageName, $currentControllerName, 'Edit', 'Edit', $force);
                 $generatedFiles += $this->generatorService->generateView($packageKey, $subpackageName, $currentControllerName, 'Show', 'Show', $force);
             } else {
                 $generatedFiles += $this->generatorService->generateView($packageKey, $subpackageName, $currentControllerName, 'Index', 'SampleIndex', $force);
             }
         }
     }
     $this->outputLine(implode(PHP_EOL, $generatedFiles));
     if ($generatedModels === TRUE) {
         $this->outputLine('As new models were generated, don\'t forget to update the database schema with the respective doctrine:* commands.');
     }
 }