Neos\Kickstarter\Service\GeneratorService::generateModel PHP Method

generateModel() public method

Generate a model for the package with the given model name and fields
public generateModel ( string $packageKey, string $modelName, array $fieldDefinitions, boolean $overwrite = false ) : array
$packageKey string The package key of the controller's package
$modelName string The name of the new model
$fieldDefinitions array The field definitions
$overwrite boolean Overwrite any existing files?
return array An array of generated filenames
    public function generateModel($packageKey, $modelName, array $fieldDefinitions, $overwrite = false)
    {
        list($baseNamespace, $namespaceEntryPath) = $this->getPrimaryNamespaceAndEntryPath($this->packageManager->getPackage($packageKey));
        $modelName = ucfirst($modelName);
        $namespace = trim($baseNamespace, '\\') . '\\Domain\\Model';
        $fieldDefinitions = $this->normalizeFieldDefinitions($fieldDefinitions, $namespace);
        $templatePathAndFilename = 'resource://Neos.Kickstarter/Private/Generator/Model/EntityTemplate.php.tmpl';
        $contextVariables = array();
        $contextVariables['packageKey'] = $packageKey;
        $contextVariables['modelName'] = $modelName;
        $contextVariables['fieldDefinitions'] = $fieldDefinitions;
        $contextVariables['namespace'] = $namespace;
        $fileContent = $this->renderTemplate($templatePathAndFilename, $contextVariables);
        $modelFilename = $modelName . '.php';
        $modelPath = $namespaceEntryPath . '/Domain/Model/';
        $targetPathAndFilename = $modelPath . $modelFilename;
        $this->generateFile($targetPathAndFilename, $fileContent, $overwrite);
        $this->generateTestsForModel($packageKey, $modelName, $overwrite);
        return $this->generatedFiles;
    }

Usage Example

 /**
  * Kickstart a new domain model
  *
  * This command generates a new domain model class. The fields are specified as
  * a variable list of arguments with field name and type separated by a colon
  * (for example "title:string" "size:int" "type:MyType").
  *
  * @param string $packageKey The package key of the package for the domain model
  * @param string $modelName The name of the new domain model class
  * @param boolean $force Overwrite any existing model.
  * @return string
  * @see neos.kickstarter:kickstart:repository
  */
 public function modelCommand($packageKey, $modelName, $force = false)
 {
     $this->validatePackageKey($packageKey);
     if (!$this->packageManager->isPackageAvailable($packageKey)) {
         $this->outputLine('Package "%s" is not available.', array($packageKey));
         exit(2);
     }
     $this->validateModelName($modelName);
     $fieldsArguments = $this->request->getExceedingArguments();
     $fieldDefinitions = array();
     foreach ($fieldsArguments as $fieldArgument) {
         list($fieldName, $fieldType) = explode(':', $fieldArgument, 2);
         $fieldDefinitions[$fieldName] = array('type' => $fieldType);
         if (strpos($fieldType, 'array') !== false) {
             $fieldDefinitions[$fieldName]['typeHint'] = 'array';
         } elseif (strpos($fieldType, '\\') !== false) {
             if (strpos($fieldType, '<') !== false) {
                 $fieldDefinitions[$fieldName]['typeHint'] = substr($fieldType, 0, strpos($fieldType, '<'));
             } else {
                 $fieldDefinitions[$fieldName]['typeHint'] = $fieldType;
             }
         }
     }
     $generatedFiles = $this->generatorService->generateModel($packageKey, $modelName, $fieldDefinitions, $force);
     $this->outputLine(implode(PHP_EOL, $generatedFiles));
     $this->outputLine('As a new model was generated, don\'t forget to update the database schema with the respective doctrine:* commands.');
 }