예제 #1
0
 /**
  * Create action for the view-spec provider
  *
  * @param string $name 
  * @param string $controllerName 
  * @param string $module 
  * @return void
  */
 public function create($name, $controllerName, $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');
     }
     $response = $this->_registry->getResponse();
     $request = $this->_registry->getRequest();
     $originalName = $name;
     $name = LCFirst::apply($name);
     $camelCaseToDashFilter = new CamelCaseToDash();
     $name = strtolower($camelCaseToDashFilter->filter($name));
     // alert the user about inline converted names
     $tense = $request->isPretend() ? 'would be' : 'is';
     if ($name !== $originalName) {
         $response->appendContent('Note: The canonical view name that ' . $tense . ' used with other providers is "' . $name . '";' . ' not "' . $originalName . '" as supplied', array('color' => array('yellow')));
     }
     try {
         $viewResource = self::createResource($this->_loadedProfile, $name, $controllerName, $module);
     } catch (Exception $e) {
         $response->setException($e);
         return;
     }
     // view spec
     $inflectedController = strtolower($camelCaseToDashFilter->filter($controllerName));
     $viewPath = str_replace(basename($viewResource->getContext()->getPath()), '', $viewResource->getContext()->getPath());
     $basePath = str_replace("application/views/scripts/{$inflectedController}", '', $viewPath);
     $specNameFilter = new DashToCamelCase();
     $specName = UCFirst::apply($specNameFilter->filter($name));
     $moduleController = $inflectedController;
     if ($module !== null) {
         $moduleController .= strtolower($camelCaseToDashFilter->filter($module));
     }
     $viewSpecPath = realpath($basePath . '/spec/views') . '/' . $moduleController . '/' . $specName . 'Spec.php';
     $specContent = $this->_getSpecContent($name, $controllerName, $module);
     if ($request->isPretend()) {
         $response->appendContent('Would create a view script in location ' . $viewResource->getContext()->getPath());
         $response->appendContent('Would create a spec at ' . $viewSpecPath);
     } else {
         $response->appendContent('Creating a view script in location ' . $viewResource->getContext()->getPath());
         $viewResource->create();
         $response->appendContent('Creating a spec at ' . $viewSpecPath);
         if (!is_dir($basePath . '/spec/views')) {
             mkdir($basePath . '/spec/views');
         }
         if (!is_dir($basePath . '/spec/views/' . $inflectedController)) {
             mkdir($basePath . '/spec/views/' . $inflectedController);
         }
         file_put_contents($viewSpecPath, $specContent);
     }
 }
예제 #2
0
 /**
  * Implements the create action for the action-spec provider
  *
  * @param string $name 
  * @param string $controllerName 
  * @param string $module 
  * @return void
  */
 public function create($name, $controllerName, $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');
     }
     $response = $this->_registry->getResponse();
     $request = $this->_registry->getRequest();
     $originalName = $name;
     $name = LCFirst::apply($name);
     $camelCaseToDashFilter = new CamelCaseToDash();
     $name = strtolower($camelCaseToDashFilter->filter($name));
     // alert the user about inline converted names
     $tense = $request->isPretend() ? 'would be' : 'is';
     if ($name !== $originalName) {
         $response->appendContent('Note: The canonical action name that ' . $tense . ' used with other providers is "' . $name . '";' . ' not "' . $originalName . '" as supplied', array('color' => array('yellow')));
     }
     try {
         $controllerResource = ControllerProvider::createResource($this->_loadedProfile, $controllerName, $module);
         $actionResource = self::createResource($this->_loadedProfile, $name, $controllerName, $module);
     } catch (Exception $e) {
         $response->setException($e);
         return;
     }
     // action spec
     $controllerPath = str_replace(basename($controllerResource->getContext()->getPath()), '', $controllerResource->getContext()->getPath());
     $basePath = realpath($controllerPath . '/../..');
     $controllerSpecPath = realpath($basePath . '/spec/controllers') . '/' . $controllerName . 'ControllerSpec.php';
     $specContent = $this->_getSpecContent($name, $controllerName);
     if ($request->isPretend()) {
         $response->appendContent('Would create an action named ' . $name . ' inside controller at ' . $controllerResource->getContext()->getPath());
         $response->appendContent('Would create an action spec at ' . $controllerSpecPath);
     } else {
         $response->appendContent('Creating an action named ' . $name . ' inside controller at ' . $controllerResource->getContext()->getPath());
         $actionResource->create();
         $response->appendContent('Creating an action spec at ' . $controllerSpecPath);
         $content = file_get_contents($controllerSpecPath);
         file_put_contents($controllerSpecPath, str_replace("\n}", $specContent, $content));
     }
 }
예제 #3
0
    /**
     * Creates mapper content
     *
     * @param string $name 
     * @return string
     */
    protected function _getMapperContent($name)
    {
        $property = LCFirst::apply($name);
        $tableName = Pluralize::apply($name);
        return <<<MAPPER
<?php

use Application_Model_{$name} as {$name};
use Application_Model_DbTable_{$tableName} as {$tableName}Dao;

class Application_Model_{$name}Mapper
{
    /**
     * Data access object. Typically a db table, but can be any data provider
     * object.
     */
    protected \$_{$property}Dao;
    
    /**
     * Gets the data access object
     */
    protected function getDao()
    {
        if (\$this->_{$property}Dao === null) {
            \$this->_{$property}Dao = new {$tableName}Dao;
        }
        return \$this->_{$property}Dao;
    }
    
    /**
     * Sets the data access object (for tests purposes) 
     *
     */
     protected function setDao(\$dao)
     {
         \$this->_{$property}Dao = \$dao;
     }
    
    /**
     * Retrives a model given entity primary key value
     */
    public function find(\$primaryKey)
    {
        \$records = \$this->getDao()->find(\$primaryKey);
        if (count(\$records)) {
            return {$name}::create(\$records->current()->toArray());
        }
    }
    
    /**
     * Retrives a collection (using arrays) given a condition, order, count
     * and offset
     */
    public function fetchAll(
        \$condition = null, \$order = null, \$count = null, \$offset = null)
    {
        \$records = \$this->getDao()->fetchAll(
            \$condition, \$order, \$count, \$offset
        );
        \${$property}s = array();
        while (\$records->valid()) {
            \${$property}s[] = {$name}::create(\$records->current()->toArray());
            \$records->next();
        }
        return \${$property}s;
    }
    
    /**
     * Persists the value of the entity in the data source
     */
    public function save({$name} \${$property})
    {
        if (\${$property}->getId()) {
            return \$this->getDao()->update(
                \${$property}->toArray(), 'id=' . (int)\${$property}->getId()
            );
        }
        return \$this->getDao()->insert(\${$property}->toArray());
    }
    
    /**
     * Deletes the entity from the data source
     */
    public function delete({$name} \${$property})
    {
        return \$this->getDao()->delete('id=' . (int)\${$property}->getId());
    }
}
MAPPER;
    }
예제 #4
0
 /**
  * Creates a toArray() generator
  * 
  * @param array $fields
  * @return Zend_CodeGenerator_Php_Method
  */
 protected function _generateToArray(array $fields)
 {
     $camelCase = new UnderscoreToCamelCase();
     $body = 'return array(' . PHP_EOL;
     foreach ($fields as $field) {
         $varAndType = explode(':', $field);
         list($varname, $type) = count($varAndType) > 1 ? $varAndType : array($varAndType[0], 'mixed');
         $key = $varname;
         $body .= '    \'' . $varname . '\' => $this->_' . LCFirst::apply($camelCase->filter($varname)) . ',' . PHP_EOL;
     }
     if (!empty($fields)) {
         $body = substr($body, 0, strlen($body) - 2) . PHP_EOL;
     }
     $body .= ');';
     return new Zend_CodeGenerator_Php_Method(array('name' => "toArray", 'body' => $body, 'docblock' => "Returns the model serialized as an array" . PHP_EOL . PHP_EOL . "@return array"));
 }