Exemplo n.º 1
0
 /**
  * Gets the form elements code to add
  *
  * @param array $fields 
  * @return string
  */
 protected function _getFormElements($fields)
 {
     $upperFirst = new UCFirst();
     $camelCaseToSpace = new CamelCaseToSeparator();
     $elements = '';
     $fields = explode(',', $fields);
     foreach ($fields as $field) {
         list($name, $type) = strpos($field, ':') ? explode(':', $field) : array($field, 'string');
         $ucFirst = $upperFirst->filter($name);
         $label = $camelCaseToSpace->filter($ucFirst);
         switch ($type) {
             case 'text':
                 $elements .= "        \$this->addElement('textarea', " . "'{$name}', array('label' => '{$label}'));" . PHP_EOL;
                 break;
             default:
                 $elements .= "        \$this->addElement('text'," . " '{$name}', array('label' => " . "'{$label}'));" . PHP_EOL;
                 break;
         }
     }
     if (!empty($elements)) {
         $elements .= "        \$this->addElement('submit', 'Save');" . PHP_EOL;
     }
     return $elements;
 }
Exemplo n.º 2
0
 /**
  * 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();
     }
 }
Exemplo n.º 3
0
    /**
     * 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;
    }
Exemplo n.º 4
0
    /**
     * 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();
    }
}';
    }
Exemplo n.º 5
0
    /**
     * 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;
    }
Exemplo n.º 6
0
 /**
  * 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>";
 }
Exemplo n.º 7
0
    /**
     * 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;
    }
Exemplo n.º 8
0
 /**
  * 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}"));
 }