/** * Create a new model * * @param string $name * @param string $module */ public function create($name, $commaSeparatedFields = '', $module = null) { $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION); if (!is_dir('spec')) { throw new ProviderException('Please run zf generate phpspec, to create the environment'); } $originalName = $name; $name = UCFirst::apply($name); $tableName = Pluralize::apply($name); $dbTableName = strtolower($tableName); // determine if testing is enabled in the project // Zend_Tool_Project_Provider_Test::isTestingEnabled( // $this->_loadedProfile // ); $testingEnabled = false; $testModelResource = null; // Check that there is not a dash or underscore, // return if doesnt match regex if (preg_match('#[_-]#', $name)) { throw new ProviderException('Model names should be camel cased.'); } if (self::hasResource($this->_loadedProfile, $name, $module)) { throw new ProviderException('This project already has a model named ' . $name); } // get request/response object $request = $this->_registry->getRequest(); $response = $this->_registry->getResponse(); // alert the user about inline converted names $tense = $request->isPretend() ? 'would be' : 'is'; if ($name !== $originalName) { $response->appendContent('Note: The canonical model name that ' . $tense . ' used with other providers is "' . $name . '";' . ' not "' . $originalName . '" as supplied', array('color' => array('yellow'))); } $commaSeparatedFields = trim($commaSeparatedFields); $fields = empty($commaSeparatedFields) ? array() : explode(',', $commaSeparatedFields); try { $modelResource = self::createResource($this->_loadedProfile, $name, $fields, $module); $mapperResource = parent::createResource($this->_loadedProfile, $name . "Mapper", $module); $dbTableResource = DbTableProvider::createResource($this->_loadedProfile, $tableName, strtolower($tableName), $module); } catch (Exception $e) { $response->setException($e); return; } //model spec $modelPath = str_replace(basename($modelResource->getContext()->getPath()), '', $modelResource->getContext()->getPath()); $basePath = realpath($modelPath . '/../..'); $modelSpecPath = realpath($basePath . '/spec/models') . '/' . $name . 'Spec.php'; $specContent = $this->_getSpecContent($name, $fields); // migrations if (!is_dir($basePath . "/db")) { mkdir($basePath . "/db"); } if (!is_dir($basePath . "/db/migrate")) { mkdir($basePath . "/db/migrate"); } $files = glob($basePath . "/db/migrate/*.php"); natsort($files); $nextVersion = empty($files) ? 1 : 1 + (int) substr(basename(array_pop($files)), 0, 3); $migrationClass = "Create{$tableName}Table"; $fileName = sprintf("%1\$03d", $nextVersion, $migrationClass) . "-{$migrationClass}"; $migrationPath = $basePath . "/db/migrate/" . $fileName . ".php"; $migrationContent = $this->_getMigrationContent($migrationClass, $dbTableName, $fields); // do the creation if ($request->isPretend()) { $response->appendContent('Would create a model at ' . $modelResource->getContext()->getPath()); $response->appendContent('Would create a db table at ' . $dbTableResource->getContext()->getPath()); $response->appendContent('Would create a mapper at ' . $mapperResource->getContext()->getPath()); $response->appendContent('Would create a spec at ' . $modelSpecPath); $response->appendContent('Would create migration scripts at ' . $migrationPath); } else { $response->appendContent('Creating a model at ' . $modelResource->getContext()->getPath()); $modelResource->create(); $response->appendContent('Creating a db table at ' . $dbTableResource->getContext()->getPath()); $dbTableResource->create(); $response->appendContent('Creating a mapper at ' . $mapperResource->getContext()->getPath()); $mapperContent = $this->_getMapperContent($name); file_put_contents($mapperResource->getContext()->getPath(), $mapperContent); $response->appendContent('Creating a spec at ' . $modelSpecPath); file_put_contents($modelSpecPath, $specContent); $response->appendContent('Creating migration scripts at ' . $migrationPath); file_put_contents($migrationPath, $migrationContent); $this->_storeProfile(); } }
/** * Creates the content for the spec file * * @param string $name * @param string $controllerName * @return string */ protected function _getSpecContent($name, $controllerName) { $dashToCamelCase = new DashToCamelCase(); $camelCaseToDash = new CamelCaseToDash(); return ' function itShouldBeSuccessfulToGet' . UCFirst::apply($dashToCamelCase->filter($name)) . '() { $this->get(\'' . strtolower($camelCaseToDash->filter($controllerName)) . '/' . $name . '\'); $this->response->should->beSuccess(); } }'; }
/** * Creates the content of the view spec file * * @param string $name * @param string $controllerName * @param string $module * @return string */ protected function _getSpecContent($name, $controllerName, $module) { $namespace = $controllerName; $helperDir = "/../../"; if ($module !== null) { $namespace = UCFirst::apply($module) . "\\{$controllerName}"; $helperDir = "/../../../"; } $specNameFilter = new DashToCamelCase(); $inflectedView = $specNameFilter->filter($name); return <<<CONTENT <?php namespace {$namespace}; require_once __DIR__ . '{$helperDir}SpecHelper.php'; use \\PHPSpec\\Context\\Zend\\View as ViewContext; class Describe{$inflectedView} extends ViewContext { function itRendersTheDefaultContent() { \$this->render(); \$this->rendered->should->contain('{$controllerName}'); \$this->rendered->should->contain('{$name}'); } } CONTENT; }
/** * Gets the content of the controller spec file * * @param string $name * @param string $actions * @return string */ protected function _getSpecContent($name, $actions) { $dashToCamelCase = new DashToCamelCase(); $camelCaseTDash = new CamelCaseToDash(); $examples = array(); foreach ($actions as $action) { $examples[] = 'function itShouldBeSuccessfulToGet' . UCFirst::apply($dashToCamelCase->filter($action)) . '() { $this->get(\'' . strtolower($camelCaseTDash->filter($name)) . '/' . $action . '\'); $this->response->should->beSuccess(); }'; } $examples = implode(PHP_EOL . PHP_EOL . ' ', $examples); return <<<CONTENT <?php require_once __DIR__ . '/../SpecHelper.php'; class Describe{$name}Controller extends \\PHPSpec\\Context\\Zend\\Controller { {$examples} } CONTENT; }
/** * Creates a getter generator * * @param string $varname * @param string $type * @return Zend_CodeGenerator_Php_Method */ protected function _generateGetter($varname, $type) { return new Zend_CodeGenerator_Php_Method(array('name' => "get" . UCFirst::apply($varname), 'body' => 'return $this->_' . $varname . ';', 'docblock' => "Gets the {$varname}" . PHP_EOL . PHP_EOL . "@return {$type}")); }