/** * Asks for the prefix and sets it on the InputInterface as the 'prefix' option, if this option is not set yet. * Will set the default to a snake_cased namespace when the namespace has been set on the InputInterface. * * @param array $text What you want printed before the prefix is asked. If null is provided it'll write a default text. * @param string $namespace An optional namespace. If this is set it'll create the default based on this prefix. * If it's not provided it'll check if the InputInterface already has the namespace option. * * @return string The prefix. But it's also been set on the InputInterface. */ protected function askForPrefix(array $text = null, $namespace = null) { $prefix = $this->assistant->getOptionOrDefault('prefix', null); if (is_null($text)) { $text = array('You can add a prefix to the table names of the generated entities for example: ' . '<comment>projectname_bundlename_</comment>', 'Enter an underscore \'_\' if you don\'t want a prefix.', ''); } if (is_null($prefix)) { if (count($text) > 0) { $this->assistant->writeLine($text); } if (is_null($namespace) || empty($namespace)) { $namespace = $this->assistant->getOption('namespace'); } else { $namespace = $this->fixNamespace($namespace); } $defaultPrefix = GeneratorUtils::cleanPrefix($this->convertNamespaceToSnakeCase($namespace)); $prefix = GeneratorUtils::cleanPrefix($this->assistant->ask('Tablename prefix', $defaultPrefix)); $this->assistant->setOption('prefix', $prefix); } return $prefix; }
/** * Get an array of fields that need to be added to the entity. * * @param BundleInterface $bundle * @param array $reservedFields * * @return array */ protected function askEntityFields(BundleInterface $bundle, array $reservedFields = array('id')) { $this->assistant->writeLine('<info>Available field types:</info> '); $typeSelect = $this->getTypes(true); foreach ($typeSelect as $type) { $this->assistant->writeLine(sprintf('<comment>- %s</comment>', $type)); } $fields = array(); $self = $this; $typeStrings = $this->getTypes(); $mediaTypeSelect = $this->getMediaTypes(); $generator = $this->getGenerator(); $container = $this->getContainer(); while (true) { $this->assistant->writeLine(''); $fieldName = $this->assistant->askAndValidate('New field name (press <return> to stop adding fields)', function ($name) use($fields, $self, $reservedFields, $generator, $container) { // The fields cannot exist in the reserved field list if (in_array($name, $reservedFields)) { throw new \InvalidArgumentException(sprintf('Field "%s" is already defined in the parent class', $name)); } // The fields cannot exist already if (isset($fields[$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)); } // Only accept a-z if (!preg_match('/^[a-zA-Z][a-zA-Z_0-9]+$/', $name) && $name != '') { throw new \InvalidArgumentException(sprintf('Name "%s" is invalid', $name)); } return $name; }); // When <return> is entered if (!$fieldName) { break; } $typeId = $this->assistant->askSelect('Field type', $typeSelect); // If single -or multipe entity reference in chosen, we need to ask for the entity name if (in_array($typeStrings[$typeId], array('single_ref', 'multi_ref'))) { $bundleName = $bundle->getName(); $question = "Reference entity name (eg. {$bundleName}:FaqItem, {$bundleName}:Blog/Comment)"; $name = $this->assistant->askAndValidate($question, function ($name) use($fields, $self, $bundle, $generator, $container) { $parts = explode(':', $name); // Should contain colon if (count($parts) != 2) { throw new \InvalidArgumentException(sprintf('"%s" is an invalid entity name', $name)); } // Check reserved words if ($generator->isReservedKeyword($parts[1])) { throw new \InvalidArgumentException(sprintf('"%s" contains a reserved word', $name)); } $em = $container->get('doctrine')->getEntityManager(); try { $em->getClassMetadata($name); } catch (\Exception $e) { throw new \InvalidArgumentException(sprintf('Entity "%s" not found', $name)); } return $name; }, null, array($bundleName)); $extra = $name; } else { $extra = null; } // If image type, force image media filter if ($typeStrings[$typeId] == 'image') { $extra = 'image'; } // If media type, ask for media filter if ($typeStrings[$typeId] == 'media') { $mediaTypeId = $this->assistant->askSelect('Media filter', $mediaTypeSelect); $extra = strtolower($mediaTypeSelect[$mediaTypeId]); } if ($typeStrings[$typeId] == 'image' || $typeStrings[$typeId] == 'media') { // Ask the allowed mimetypes for the media ojbect $mimeTypes = $this->assistant->ask('Do you want to limit the possible file types? Then specify a comma-seperated list of types (example: image/png,image/svg+xml), otherwise press ENTER', null); if (isset($mimeTypes)) { $mimeTypes = explode(',', $mimeTypes); } $data = array('name' => $fieldName, 'type' => $typeStrings[$typeId], 'extra' => $extra, 'mimeTypes' => $mimeTypes); if ($extra == 'image') { $minHeight = $maxHeight = $minWidth = $maxWidth = null; if ($this->assistant->askConfirmation('Do you want to add validation of the dimensions of the media object? (y/n)', 'n', '?', false)) { // Ask the minimum height allowed for the image $lengthValidation = function ($length) { if (is_numeric($length) && $length < 0 || !is_numeric($length) && !empty($length)) { throw new \InvalidArgumentException(sprintf('"%s" is not a valid length', $length)); } else { return $length; } }; $minHeight = $this->assistant->askAndValidate('What is the minimum height for the media object? (in pixels)', $lengthValidation); // Ask the maximum height allowed for the image $maxHeight = $this->assistant->askAndValidate('What is the maximum height for the media object? (in pixels)', $lengthValidation); // Ask the minimum width allowed for the image $minWidth = $this->assistant->askAndValidate('What is the minimum width for the media object? (in pixels)', $lengthValidation); //Ask the maximum width allowed for the image $maxWidth = $this->assistant->askAndValidate('What is the maximum width for the media object? (in pixels)', $lengthValidation); } $data = array('name' => $fieldName, 'type' => 'image', 'extra' => $extra, 'minHeight' => $minHeight, 'maxHeight' => $maxHeight, 'minWidth' => $minWidth, 'maxWidth' => $maxWidth, 'mimeTypes' => $mimeTypes); } } else { $data = array('name' => $fieldName, 'type' => $typeStrings[$typeId], 'extra' => $extra); } $fields[$fieldName] = $data; } return $fields; }