protected function getAssistant() { $output = $this->getMock('Symfony\\Component\\Console\\Output\\OutputInterface'); $commandAssistant = new CommandAssistant(); $commandAssistant->setOutput($output); return $commandAssistant; }
/** * 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; }
/** * 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]); } $data = array('name' => $fieldName, 'type' => $typeStrings[$typeId], 'extra' => $extra); $fields[$fieldName] = $data; } return $fields; }