/** * Creates the show view * * @param string $entity * @param array $fields * @return string */ protected static function createShowView($entity, $fields) { $upperFirst = new UCFirst(); $lowerFirst = new LCFirst(); $camelCaseToSpace = new CamelCaseToSeparator(); $pluralize = new Pluralize(); $entityPlural = $pluralize->filter($entity); $lowerEntity = $lowerFirst->filter($entity); $data = $rendered = ''; $fields = explode(',', $fields); foreach ($fields as $field) { $fieldAndType = explode(':', $field); list($field, $type) = count($fieldAndType) === 2 ? $fieldAndType : array($field, 'string'); $ucFirst = $upperFirst->filter($field); $field = $camelCaseToSpace->filter($ucFirst); $data .= "'get{$ucFirst}' => 'some {$field}'," . PHP_EOL . " "; $rendered .= '$this->rendered->should->haveSelector(\'p>b\', ' . 'array(\'text\' => \'' . $field . ':\')); $this->rendered->should->contain(\'some ' . $field . '\');'; } return <<<SHOWVIEW <?php namespace {$entityPlural}; use \\PHPSpec\\Context\\Zend\\View as ViewContext; class DescribeShow extends ViewContext { function before() { \$this->assign('{$lowerEntity}', \$this->stub('{$entity}', array( 'getId' => '1', {$data} ))); } function itRendersThe{$entity}() { \$this->render(); {$rendered} } } SHOWVIEW; }
/** * Creates the scaffolded delete action * * @param string $entity * @return string */ protected static function _getDeleteActionContent($entity) { $camelCaseToDash = new CamelCaseToDash(); $lcFirst = new LCFirst(); $pluralize = new Pluralize(); $lc = $lcFirst->filter($entity); $plural = $pluralize->filter($lc); $lowerDashedPlural = $camelCaseToDash->filter($plural); return "\${$lc}Mapper = \$this->get('Model.{$entity}Mapper');\n \${$lc} = \${$lc}Mapper->find(\$this->_request->id);\n \${$lc}Mapper->delete(\${$lc});\n \$this->_redirect('/{$lowerDashedPlural}');"; }
/** * Creates the controller views and actions * * @param Registry $registry * @param Profile $profile * @param string $entity * @param string $commaSeparatedFields * @param string $module */ protected static function _createControllerViewsAndActions(Registry $registry, Profile $profile, $entity, $commaSeparatedFields, $module) { $pluralize = new Pluralize(); $entityPlural = $pluralize->filter($entity); $controllerResource = ControllerProvider::createResource($profile, $entityPlural, $module); $controllerPath = $controllerResource->getContext()->getPath(); self::_addAliasesForTheController($entity, $controllerPath); foreach (self::$_scaffoldActions as $action) { if ($action !== 'add' && $action !== 'update' && $action !== 'delete') { ViewContent::create($registry, $profile, $action, $entity, $commaSeparatedFields, $module); } ActionMethod::create($action, $entity, $controllerPath); } }
/** * Gets the content of scaffolded show view * * @param string $entity * @param array $fields * @return string */ protected static function _getShowViewContent($entity, $fields) { $upperFirst = new UCFirst(); $lowerFirst = new LCFirst(); $camelCaseToSpace = new CamelCaseToSeparator(); $camelCaseToDash = new CamelCaseToDash(); $pluralize = new Pluralize(); $lowerEntity = $lowerFirst->filter($entity); $lowerEntityPlural = $pluralize->filter($lowerEntity); $dashedEntityPlural = $camelCaseToDash->filter($lowerEntityPlural); $properties = ''; foreach ($fields as $field) { $upper = $upperFirst->filter($field); $field = $camelCaseToSpace->filter($field); $field = $upperFirst->filter($field); $properties .= " <p>\n <b>{$field}:</b>\n <?php echo \$this->escape(\$this->{$lowerEntity}->get{$upper}()) ?></h3>\n </p>" . PHP_EOL . PHP_EOL; } return "<h1>Show {$entity}</h1>\n\n{$properties}\n\n<a href=\"<?php echo \$this->baseUrl(" . "'{$dashedEntityPlural}/edit/id/' ." . " (int)\$this->{$lowerEntity}->getId()) ?>\">Edit</a> |\n<a href=\"<?php echo \$this->baseUrl(" . "'{$dashedEntityPlural}') ?>\">Back</a>"; }
/** * Renders the content of the scaffolded controller spec * * @param string $entity * @param array $fields * @param string $module * @return string */ protected static function content($entity, $fields, $module) { $pluralize = new Pluralize(); $dashIt = new CamelCaseToSeparator(); $camelize = new DashToCamelCase(); $lcFirst = new LCFirst(); $ucFirst = new UCFirst(); if ($module === null) { $module = $camelize->filter($module); $namespace = 'namespace ' . $ucFirst->filter($module) . ';' . PHP_EOL; } else { $namespace = ''; } $entityPlural = $pluralize->filter($entity); $lcFirstEntity = $lcFirst->filter($entity); $lcFirstEntityPlural = $lcFirst->filter($entityPlural); $entityPluralDashed = $dashIt->filter($entityPlural); $smallCasedDashedPlural = strtolower($entityPluralDashed); $fieldsAndValues = $rendered = ''; $fields = explode(',', $fields); foreach ($fields as $field) { $fieldAndType = explode(':', $field); list($field, $type) = count($fieldAndType) === 2 ? $fieldAndType : array($field, 'string'); $fieldType = $type === 'text' ? 'textarea' : 'input'; $fieldsAndValues .= "'{$field}' => 'some {$field}'," . PHP_EOL . " "; } $appForm = "Application_Form_{$entity}Form"; return <<<CONTROLLER <?php {$namespace} class Describe{$entityPlural}Controller extends \\PHPSpec\\Context\\Zend\\Controller { // GET index function itAssignsAll{$entityPlural}ToList() { \$mapper = \$this->stub('Application_Model_{$entity}Mapper'); \$mapper->shouldReceive('fetchAll')->andReturn(array()); \$this->inject('Application.Model.{$entity}Mapper', \$mapper); \$this->get('{$smallCasedDashedPlural}/index'); \$this->assigns('{$lcFirstEntityPlural}')->should->be(array()); } // GET show function itAssignsTheRequested{$entity}ToBeShown() { \${$lcFirstEntity} = \$this->stub('Application_Model_{$entity}'); \$mapper = \$this->stub('Application_Model_{$entity}Mapper'); \$mapper->shouldReceive('find')->andReturn(\${$lcFirstEntity}); \$this->inject('Application.Model.{$entity}Mapper', \$mapper); \$this->get('{$smallCasedDashedPlural}/show'); \$this->assigns('{$lcFirstEntity}')->should->be(\${$lcFirstEntity}); } // GET edit function itPopulatesTheEditFormWithA{$entity}() { \${$lcFirstEntity}Array = array( 'id' => 1, {$fieldsAndValues} ); \${$lcFirstEntity} = \$this->stub('Application_Model_{$entity}'); \${$lcFirstEntity}->shouldReceive('toArray') ->andReturn(\${$lcFirstEntity}Array); \$mapper = \$this->stub('Application_Model_{$entity}Mapper'); \$mapper->shouldReceive('find')->andReturn(\${$lcFirstEntity}); \$this->inject('Application.Model.{$entity}Mapper', \$mapper); \$form = \$this->stub('Application_Form_{$entity}Form'); \$form->shouldReceive('populate')->with(\${$lcFirstEntity}Array); \$this->inject('Application.Form.{$entity}Form', \$form); \$this->get('{$smallCasedDashedPlural}/edit/id/1'); \$this->assigns('id')->should->be('1'); \$this->assigns('form')->should->be(\$form); } // GET new function itDisplaysThe{$entity}Form() { \$this->get('{$smallCasedDashedPlural}/new'); \$this->assigns('form')->should->beAnInstanceOf('{$appForm}'); } // POST add function itAddsTheValid{$entity}() { \$mapper = \$this->stub('Application_Model_{$entity}Mapper'); \$mapper->shouldReceive('save')->once(); \$this->inject('Application.Model.{$entity}Mapper', \$mapper); \$form = \$this->stub('Application_Form_{$entity}Form'); \$form->shouldReceive('isValid')->andReturn(true); \$this->inject('Application.Form.{$entity}Form', \$form); \$this->post('{$smallCasedDashedPlural}/add', array( {$fieldsAndValues} )); } // POST update function itUpdatesTheValid{$entity}() { \$mapper = \$this->stub('Application_Model_{$entity}Mapper'); \$mapper->shouldReceive('save'); \$this->inject('Application.Model.{$entity}Mapper', \$mapper); \$form = \$this->stub('Application_Form_{$entity}Form'); \$form->shouldReceive('isValid')->andReturn(true); \$this->inject('Application.Form.{$entity}Form', \$form); \$this->post('{$smallCasedDashedPlural}/update/id/1', array( {$fieldsAndValues} )); } // GET delete function itDeletesThe{$entity}() { \$mapper = \$this->stub('Application_Model_{$entity}Mapper'); \$mapper->shouldReceive('delete'); \$this->inject('Application.Model.{$entity}Mapper', \$mapper); \$this->get('{$smallCasedDashedPlural}/delete/id/1'); \$this->response->should->redirectTo('/{$lcFirstEntityPlural}'); } // this is because we are using the registry as container // not needed with a proper non-static IoC container function after() { \$this->clearContainer(); } } CONTROLLER; }