/**
  * Asks for the namespace and sets it on the InputInterface as the 'namespace' option, if this option is not set yet.
  *
  * @param array $text    What you want printed before the namespace is asked.
  *
  * @return string The namespace. But it's also been set on the InputInterface.
  */
 public function askForNamespace(array $text = null)
 {
     $namespace = $this->input->hasOption('namespace') ? $this->input->getOption('namespace') : null;
     // When the Namespace is filled in return it immediately if valid.
     try {
         if (!is_null($namespace) && !empty($namespace)) {
             Validators::validateBundleNamespace($namespace);
             return $namespace;
         }
     } catch (\Exception $error) {
         $this->writeError(array("Namespace '{$namespace}' is incorrect. Please provide a correct value.", $error->getMessage()));
         exit;
     }
     $ownBundles = $this->getOwnBundles();
     if (count($ownBundles) <= 0) {
         $this->writeError("Looks like you don't have created a bundle for your project, create one first.", true);
     }
     $namespace = '';
     // If we only have 1 or more bundles, we can prefill it.
     if (count($ownBundles) > 0) {
         $namespace = $ownBundles[1]['namespace'] . '/' . $ownBundles[1]['name'];
     }
     $namespaces = $this->getNamespaceAutoComplete($this->kernel);
     if (!is_null($text) && count($text) > 0) {
         $this->output->writeln($text);
     }
     $namespace = $this->dialog->askAndValidate($this->output, $this->dialog->getQuestion('Bundle Namespace', $namespace), array('Sensio\\Bundle\\GeneratorBundle\\Command\\Validators', 'validateBundleNamespace'), false, $namespace, $namespaces);
     if ($this->input->hasOption('namespace')) {
         $this->input->setOption('namespace', $namespace);
     }
     return $namespace;
 }
 public function addActions(InputInterface $input, OutputInterface $output, DialogHelper $dialog)
 {
     $output->writeln(array('', 'Instead of starting with a blank controller, you can add some actions now. An action', 'is a PHP function or method that executes, for example, when a given route is matched.', 'Actions should be suffixed by <comment>Action</comment>.', ''));
     $templateNameValidator = function ($name) {
         if ('default' == $name) {
             return $name;
         }
         if (2 != substr_count($name, ':')) {
             throw new \InvalidArgumentException(sprintf('Template name "%s" does not have 2 colons', $name));
         }
         return $name;
     };
     $actions = $this->parseActions($input->getOption('actions'));
     while (true) {
         // name
         $output->writeln('');
         $actionName = $dialog->askAndValidate($output, $dialog->getQuestion('New action name (press <return> to stop adding actions)', null), function ($name) use($actions) {
             if (null == $name) {
                 return $name;
             }
             if (isset($actions[$name])) {
                 throw new \InvalidArgumentException(sprintf('Action "%s" is already defined', $name));
             }
             if ('Action' != substr($name, -6)) {
                 throw new \InvalidArgumentException(sprintf('Name "%s" is not suffixed by Action', $name));
             }
             return $name;
         });
         if (!$actionName) {
             break;
         }
         // route
         $route = $dialog->ask($output, $dialog->getQuestion('Action route', '/' . substr($actionName, 0, -6)), '/' . substr($actionName, 0, -6));
         $placeholders = $this->getPlaceholdersFromRoute($route);
         // template
         $defaultTemplate = $input->getOption('controller') . ':' . substr($actionName, 0, -6) . '.html.' . $input->getOption('template-format');
         $template = $dialog->askAndValidate($output, $dialog->getQuestion('Templatename (optional)', $defaultTemplate), $templateNameValidator, false, 'default');
         // adding action
         $actions[$actionName] = array('name' => $actionName, 'route' => $route, 'placeholders' => $placeholders, 'template' => $template);
     }
     return $actions;
 }
 private function addFields(InputInterface $input, OutputInterface $output, DialogHelper $dialog)
 {
     $fields = $this->parseFields($input->getOption('fields'));
     $output->writeln(array('', 'Instead of starting with a blank entity, you can add some fields now.', 'Note that the primary key will be added automatically (named <comment>id</comment>).', ''));
     $output->write('<info>Available types:</info> ');
     $types = array_keys(Type::getTypesMap());
     $count = 20;
     foreach ($types as $i => $type) {
         if ($count > 50) {
             $count = 0;
             $output->writeln('');
         }
         $count += strlen($type);
         $output->write(sprintf('<comment>%s</comment>', $type));
         if (count($types) != $i + 1) {
             $output->write(', ');
         } else {
             $output->write('.');
         }
     }
     $output->writeln('');
     $fieldValidator = function ($type) use($types) {
         // FIXME: take into account user-defined field types
         if (!in_array($type, $types)) {
             throw new \InvalidArgumentException(sprintf('Invalid type "%s".', $type));
         }
         return $type;
     };
     $lengthValidator = function ($length) {
         if (!$length) {
             return $length;
         }
         $result = filter_var($length, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1)));
         if (false === $result) {
             throw new \InvalidArgumentException(sprintf('Invalid length "%s".', $length));
         }
         return $length;
     };
     while (true) {
         $output->writeln('');
         $self = $this;
         $name = $dialog->askAndValidate($output, $dialog->getQuestion('New field name (press <return> to stop adding fields)', null), function ($name) use($fields, $self) {
             if (isset($fields[$name]) || 'id' == $name) {
                 throw new \InvalidArgumentException(sprintf('Field "%s" is already defined.', $name));
             }
             // check reserved words
             if ($self->getGenerator()->isReservedKeyword($name)) {
                 throw new \InvalidArgumentException(sprintf('Name "%s" is a reserved word.', $name));
             }
             return $name;
         });
         if (!$name) {
             break;
         }
         $defaultType = 'string';
         if (substr($name, -3) == '_at') {
             $defaultType = 'datetime';
         } elseif (substr($name, -3) == '_id') {
             $defaultType = 'integer';
         }
         $type = $dialog->askAndValidate($output, $dialog->getQuestion('Field type', $defaultType), $fieldValidator, false, $defaultType);
         $data = array('fieldName' => $name, 'type' => $type);
         if ($type == 'string') {
             $data['length'] = $dialog->askAndValidate($output, $dialog->getQuestion('Field length', 255), $lengthValidator, false, 255);
         }
         $fields[$name] = $data;
     }
     return $fields;
 }
 /**
  * Add the entity fields
  *
  * @param InputInterface $input
  * @param OutputInterface $output
  * @param DialogHelper $dialog
  *
  * @return array
  */
 private function addFields(InputInterface $input, OutputInterface $output, DialogHelper $dialog)
 {
     $fields = $this->parseFields($input->getOption('fields'));
     $output->writeln(array('', 'Instead of starting with a blank entity, you can add some fields now.', 'Note that the primary key will be added automatically (named <comment>id</comment>) as well as the <comment>active</comment> field.', ''));
     $output->write('<info>Available types:</info> ');
     $types = array_keys(Type::getTypesMap());
     $count = 20;
     foreach ($types as $i => $type) {
         if ($count > 50) {
             $count = 0;
             $output->writeln('');
         }
         $count += strlen($type);
         $output->write(sprintf('<comment>%s</comment>', $type));
         if (count($types) != $i + 1) {
             $output->write(', ');
         } else {
             $output->write('.');
         }
     }
     $output->writeln('');
     $fieldValidator = function ($type) use($types) {
         // FIXME: take into account user-defined field types
         if (!in_array($type, $types)) {
             throw new \InvalidArgumentException(sprintf('Invalid type "%s".', $type));
         }
         return $type;
     };
     $lengthValidator = function ($length) {
         if (!$length) {
             return $length;
         }
         $result = filter_var($length, FILTER_VALIDATE_INT, array('options' => array('min_range' => 1)));
         if (false === $result) {
             throw new \InvalidArgumentException(sprintf('Invalid length "%s".', $length));
         }
         return $length;
     };
     $i18nValidator = function ($isI18n) {
         if (!in_array($isI18n, array('Yes', 'No', 'yes', 'no', 'Y', 'N', 'y', 'n'))) {
             throw new \InvalidArgumentException(sprintf('Invalid answer (Yes/No) "%s".', $isI18n));
         }
         return $isI18n;
     };
     $isNullableValidator = function ($isNullable) {
         if (!in_array($isNullable, array('Yes', 'No', 'yes', 'no', 'Y', 'N', 'y', 'n'))) {
             throw new \InvalidArgumentException(sprintf('Invalid answer (Yes/No) "%s".', $isNullable));
         }
         return substr(strtolower($isNullable), 0, 1) == 'y' ? true : false;
     };
     while (true) {
         $output->writeln('');
         $generator = $this->getGenerator();
         $columnName = $dialog->askAndValidate($output, $dialog->getQuestion('New field name (press <return> to stop adding fields)', null), function ($name) use($fields, $generator) {
             if (isset($fields[$name]) || 'id' == $name) {
                 throw new \InvalidArgumentException(sprintf('Field "%s" is already defined.', $name));
             }
             // check reserved words
             if ($generator->isReservedKeyword($name)) {
                 throw new \InvalidArgumentException(sprintf('Name "%s" is a reserved word.', $name));
             }
             return $name;
         });
         if (!$columnName) {
             break;
         }
         $defaultType = 'string';
         // try to guess the type by the column name prefix/suffix
         if (substr($columnName, -3) == '_at') {
             $defaultType = 'datetime';
         } elseif (substr($columnName, -3) == '_id') {
             $defaultType = 'integer';
         } elseif (substr($columnName, 0, 3) == 'is_') {
             $defaultType = 'boolean';
         } elseif (substr($columnName, 0, 4) == 'has_') {
             $defaultType = 'boolean';
         }
         $type = $dialog->askAndValidate($output, $dialog->getQuestion('Field type', $defaultType), $fieldValidator, false, $defaultType, $types);
         $data = array('columnName' => $columnName, 'fieldName' => lcfirst(Container::camelize($columnName)), 'type' => $type);
         if ($type == 'string') {
             $data['length'] = $dialog->askAndValidate($output, $dialog->getQuestion('Field length', 255), $lengthValidator, false, 255);
         }
         $data['i18n'] = $dialog->askAndValidate($output, $dialog->getQuestion('Field is i18n', 'no'), $i18nValidator, false, 'no');
         if ($columnName != 'slug') {
             $data['nullable'] = $dialog->askAndValidate($output, $dialog->getQuestion('Field is nullable', 'no'), $isNullableValidator, false, 'no');
         }
         $fields[$columnName] = $data;
     }
     return $fields;
 }