Example #1
0
 /**
  * Creates contets for this model
  * 
  * @see Zend_Tool_Project_Context_Filesystem_File::getContents()
  */
 public function getContents()
 {
     $className = ucfirst($this->_modelName);
     $tableName = $this->camelCaseToUnderscore($this->_modelName);
     $codeGenFile = new Zend_CodeGenerator_Php_File(array('fileName' => $this->getPath(), 'classes' => array(new Zend_CodeGenerator_Php_Class(array('name' => $className, 'extendedClass' => 'App_Model', 'properties' => array(new Zend_CodeGenerator_Php_Property(array('name' => '_primary', 'visibility' => Zend_CodeGenerator_Php_Property::VISIBILITY_PROTECTED, 'defaultValue' => 'id')), new Zend_CodeGenerator_Php_Property(array('name' => '_name', 'visibility' => Zend_CodeGenerator_Php_Property::VISIBILITY_PROTECTED, 'defaultValue' => $tableName)), new Zend_CodeGenerator_Php_Property(array('name' => '_rowClass', 'visibility' => Zend_CodeGenerator_Php_Property::VISIBILITY_PROTECTED, 'defaultValue' => 'App_Table_' . $className))))))));
     return $codeGenFile->generate();
 }
Example #2
0
 public function scaffold()
 {
     $form = $this->_getForm();
     $file = new Zend_CodeGenerator_Php_File();
     $file->setClass($form);
     return $file->generate();
 }
Example #3
0
    /**
     * getContents()
     *
     * @return string
     */
    public function getContents()
    {
        $codeGenerator = new Zend_CodeGenerator_Php_File(array('body' => <<<EOS
// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
)));

/** Zend_Application */
require_once 'Zend/Application.php';

// Create application, bootstrap, and run
\$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/application.ini'
);
\$application->bootstrap()
            ->run();
EOS
));
        return $codeGenerator->generate();
    }
Example #4
0
 /**
  * Creates contets for this form
  * 
  * @see Zend_Tool_Project_Context_Filesystem_File::getContents()
  */
 public function getContents()
 {
     $className = ucfirst($this->_formName);
     $moduleName = ucfirst($this->_moduleName);
     $codeGenFile = new Zend_CodeGenerator_Php_File(array('fileName' => $this->getPath(), 'classes' => array(new Zend_CodeGenerator_Php_Class(array('name' => $className, 'extendedClass' => 'App_' . $moduleName . '_Form', 'methods' => array(new Zend_CodeGenerator_Php_Method(array('name' => 'init', 'body' => 'parent::init();', 'visibility' => Zend_CodeGenerator_Php_Method::VISIBILITY_PUBLIC))))))));
     return $codeGenFile->generate();
 }
 /**
  * getContents()
  *
  * @return string
  */
 public function getContents()
 {
     $filter = new Zend_Filter_Word_DashToCamelCase();
     $className = $filter->filter($this->_forControllerName) . 'ControllerTest';
     $codeGenFile = new Zend_CodeGenerator_Php_File(array('requiredFiles' => array('PHPUnit/Framework/TestCase.php'), 'classes' => array(new Zend_CodeGenerator_Php_Class(array('name' => $className, 'extendedClass' => 'PHPUnit_Framework_TestCase', 'methods' => array(new Zend_CodeGenerator_Php_Method(array('name' => 'setUp', 'body' => '        /* Setup Routine */')), new Zend_CodeGenerator_Php_Method(array('name' => 'tearDown', 'body' => '        /* Tear Down Routine */'))))))));
     return $codeGenFile->generate();
 }
Example #6
0
 /**
  * Generates the content of the model file
  *
  * @return string 
  */
 public function getContents()
 {
     $className = $this->getFullClassName($this->_modelName, 'Model');
     $properties = count($this->_fields) ? $this->getProperties($this->_fields) : array();
     $methods = count($this->_fields) ? $this->getMethods($this->_fields, $this->_modelName) : array();
     $codeGenFile = new Zend_CodeGenerator_Php_File(array('fileName' => $this->getPath(), 'classes' => array(new Zend_CodeGenerator_Php_Class(array('name' => $className, 'properties' => $properties, 'methods' => $methods)))));
     return $codeGenFile->generate();
 }
Example #7
0
 /**
  * Generate class
  * @return string generated source code
  */
 public function generate()
 {
     $table = new ModelGenerator_Table_Table($this->_options['tableName']);
     $className = $this->_getNamer()->formatClassName($this->_config->classname);
     $baseModelClassName = $this->_getNamer()->formatClassName($this->_config->base->classname);
     $templates = array('tags' => array());
     foreach ($this->_options['docblock'] as $tag => $value) {
         $templates['tags'][] = array('name' => $tag, 'description' => $value);
     }
     $mapperTable = new Zend_CodeGenerator_Php_Class(array('name' => $className, 'docblock' => new Zend_CodeGenerator_Php_Docblock(array('shortDescription' => $className . PHP_EOL . 'Put your custom methods in this file.', 'tags' => array_merge($templates['tags']))), 'extendedClass' => $baseModelClassName));
     $mapperTableFile = new Zend_CodeGenerator_Php_File(array('classes' => array($mapperTable)));
     return $mapperTableFile->generate();
 }
Example #8
0
 public static function registerFileCodeGenerator(Zend_CodeGenerator_Php_File $fileCodeGenerator, $fileName = null)
 {
     if ($fileName == null) {
         $fileName = $fileCodeGenerator->getFilename();
     }
     if ($fileName == '') {
         throw new Zend_CodeGenerator_Php_Exception('FileName does not exist.');
     }
     // cannot use realpath since the file might not exist, but we do need to have the index
     // in the same DIRECTORY_SEPARATOR that realpath would use:
     $fileName = str_replace(array('\\', '/'), DIRECTORY_SEPARATOR, $fileName);
     self::$_fileCodeGenerators[$fileName] = $fileCodeGenerator;
 }
 public function getContents()
 {
     // Configuring after instantiation
     $methodUp = new Zend_CodeGenerator_Php_Method();
     $methodUp->setName('up')->setBody('// upgrade');
     // Configuring after instantiation
     $methodDown = new Zend_CodeGenerator_Php_Method();
     $methodDown->setName('down')->setBody('// degrade');
     $class = new Zend_CodeGenerator_Php_Class();
     $class->setName('Migration_' . $this->getMigrationName())->setExtendedClass('Core_Migration_Abstract')->setMethod($methodUp)->setMethod($methodDown);
     $file = new Zend_CodeGenerator_Php_File();
     $file->setClass($class)->setFilename($this->getPath());
     return $file->generate();
 }
 /**
  * Generate class
  * @return string generated source code
  */
 public function generate()
 {
     $table = new ModelGenerator_Table_Table($this->_options['tableName']);
     $className = $this->_getNamer()->formatClassName($this->_config->baseMapper->classname);
     $templates = array('tags' => array());
     foreach ($this->_options['docblock'] as $tag => $value) {
         $templates['tags'][] = array('name' => $tag, 'description' => $value);
     }
     $methods = array();
     $tableReferences = array();
     foreach ($table->getUniqueKeys() as $key) {
         $methods[] = new Zend_CodeGenerator_Php_Method(array('name' => 'findBy' . $this->_getNamer()->formatMethodName($key), 'docblock' => new Zend_CodeGenerator_Php_Docblock(array('tags' => array(new Zend_CodeGenerator_Php_Docblock_Tag_Param(array('paramName' => $key, 'dataType' => 'mixed')), array('name' => 'return', 'description' => $this->_getNamer()->formatClassName($this->_config->classname))))), 'parameters' => array(array('name' => $key)), 'body' => 'return $this->findOne(array(\'' . $key . ' = ?\' => $' . $key . '));'));
     }
     $modelTableBase = new Zend_CodeGenerator_Php_Class(array('name' => $className, 'docblock' => new Zend_CodeGenerator_Php_Docblock(array('shortDescription' => $className . PHP_EOL . '*DO NOT* edit this file.', 'tags' => array_merge($templates['tags']))), 'extendedClass' => 'Zend_Db_Table_Abstract', 'properties' => array(array('name' => '_name', 'visiblity' => 'protected', 'defaultValue' => $table->getName()), array('name' => '_primary', 'visiblity' => 'protected', 'defaultValue' => $table->getPrimary()), array('name' => '_dependantTables', 'visiblity' => 'protected', 'defaultValue' => $table->getDependantTables())), 'methods' => $methods));
     $modelTableBaseFile = new Zend_CodeGenerator_Php_File(array('classes' => array($modelTableBase)));
     return $modelTableBaseFile->generate();
 }
Example #11
0
 /**
  * getContents()
  *
  * @return string
  */
 public function getContents()
 {
     $profile = $this->_resource->getProfile();
     $vendor = $profile->getAttribute('vendor');
     $name = $profile->getAttribute('name');
     $className = sprintf($this->getClassPath(), $vendor, $name, ucfirst($this->_className));
     $codeGenFile = new Zend_CodeGenerator_Php_File(array('fileName' => $this->getPath(), 'classes' => array(new Zend_CodeGenerator_Php_Class(array('name' => $className, 'extendedClass' => $this->getExtends(), 'methods' => array())))));
     // store the generator into the registry so that the addAction command can use the same object later
     Zend_CodeGenerator_Php_File::registerFileCodeGenerator($codeGenFile);
     // REQUIRES filename to be set
     return $codeGenFile->generate();
 }
    /**
     * getContents()
     *
     * @return string
     */
    public function getContents()
    {
        $codeGenerator = new Zend_CodeGenerator_Php_File(array('body' => <<<EOS
// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'testing'));

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
)));

Zend_Loader_Autoloader::getInstance();

EOS
));
        return $codeGenerator->generate();
    }
Example #13
0
    /**
     * getContents()
     *
     * @return string
     */
    public function getContents()
    {
        $filter = new Zend_Filter_Word_DashToCamelCase();
        $moduleName = ucfirst($this->_moduleName);
        $className = ucfirst($this->_controllerName) . 'Controller';
        $codeGenFile = new Zend_CodeGenerator_Php_File(array('fileName' => $this->getPath(), 'classes' => array(new Zend_CodeGenerator_Php_Class(array('name' => $className, 'extendedClass' => 'App_' . $moduleName . '_Controller', 'methods' => array(new Zend_CodeGenerator_Php_Method(array('name' => 'init', 'body' => '/* Initialize action controller here */'))))))));
        if ($className == 'ErrorController') {
            $codeGenFile = new Zend_CodeGenerator_Php_File(array('fileName' => $this->getPath(), 'classes' => array(new Zend_CodeGenerator_Php_Class(array('name' => $className, 'extendedClass' => 'App_' . $moduleName . '_Controller', 'properties' => array(new Zend_CodeGenerator_Php_Property(array('name' => '_dispatch404s', 'visibility' => Zend_CodeGenerator_Php_Property::VISIBILITY_PROTECTED, 'defaultValue' => array(Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE, Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER, Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION)))), 'methods' => array(new Zend_CodeGenerator_Php_Method(array('name' => 'init', 'body' => <<<EOS
parent::init();
        
\$this->_helper->layout()->setLayout('layout');
EOS
)), new Zend_CodeGenerator_Php_Method(array('name' => 'errorAction', 'body' => <<<EOS
\$errorInfo = \$this->_getParam('error_handler');

if (in_array(\$errorInfo->type, \$this->_dispatch404s)) {
\t\$this->_dispatch404();
\treturn;
}
        
\$this->getResponse()->setRawHeader('HTTP/1.1 500 Internal Server Error');
        
\$this->title = 'Internal Server Error';
        
\$this->view->exception = \$errorInfo->exception;
EOS
)), new Zend_CodeGenerator_Php_Method(array('name' => 'flagflippersAction', 'body' => <<<EOS
if (Zend_Registry::get('IS_DEVELOPMENT')) {
\t\$this->title = 'Flag and Flipper not found';
            
\t\$this->view->originalController = \$this->_getParam('originalController');
\t\$this->view->originalAction = \$this->_getParam('originalAction');
} else {
\t\$this->_dispatch404();
}
EOS
)), new Zend_CodeGenerator_Php_Method(array('name' => 'forbiddenAction', 'body' => '$this->title = \'Forbidden\';')), new Zend_CodeGenerator_Php_Method(array('name' => '_dispatch404', 'visibility' => Zend_CodeGenerator_Php_Method::VISIBILITY_PROTECTED, 'body' => <<<EOS
\$this->title = 'Page not found';
\$this->getResponse()->setRawHeader('HTTP/1.1 404 Not Found');
        
\$this->render('error-404');
EOS
))))))));
        }
        // store the generator into the registry so that the addAction command can use the same object later
        Zend_CodeGenerator_Php_File::registerFileCodeGenerator($codeGenFile);
        // REQUIRES filename to be set
        return $codeGenFile->generate();
    }
Example #14
0
 public function indexAction()
 {
     $this->_helper->layout()->title = 'PHP Persistent Class Generator';
     $form = new Form_ClassGenerator_Class();
     if ($this->_request->isPost()) {
         $formData = $this->_request->getPost();
         if ($form->isValid($formData)) {
             $formToClass = Model_ClassGenerator_FormToClass::createInstance($formData);
             $class = $formToClass->toClass();
             $file = new Zend_CodeGenerator_Php_File();
             if ($formData['class'][Model_ClassGenerator_FormToClass::$withPersistenceKey]) {
                 $persistence = Model_ClassGenerator_Persistence::createInstance($class);
                 $persistentClass = $persistence->createPersistence();
                 $file->setClass($persistentClass);
             } else {
                 $file->setClass($class);
             }
             $this->view->resultCode = htmlentities($file->generate());
         } else {
             $form->populate($formData);
         }
     }
     $this->view->form = $form;
 }
Example #15
0
 public function getContents()
 {
     $className = $this->getFullClassName($this->_dbTableName, 'Model_DbTable');
     $codeGenFile = new Zend_CodeGenerator_Php_File(array('fileName' => $this->getPath(), 'classes' => array(new Zend_CodeGenerator_Php_Class(array('name' => $className, 'extendedClass' => 'Zend_Db_Table_Abstract', 'properties' => array(new Zend_CodeGenerator_Php_Property(array('name' => '_name', 'visibility' => Zend_CodeGenerator_Php_Property::VISIBILITY_PROTECTED, 'defaultValue' => $this->_actualTableName))))))));
     return $codeGenFile->generate();
 }
Example #16
0
    /**
     * @group ZF-11703
     */
    public function testNewMethodKeepTwoDocBlock()
    {
        $codeGenFile = Zend_CodeGenerator_Php_File::fromReflectedFileName(dirname(__FILE__) . '/_files/zf-11703_1.php', true, true);
        $target = <<<EOS
<?php
/**
 * For manipulating files.
 */


/**
 * Class Foo1
 */
class Foo1
{

    public function bar()
    {
        // action body
    }

    public function bar2()
    {
        // action body
    }


}


EOS;
        $codeGenFile->getClass()->setMethod(array('name' => 'bar2', 'body' => '// action body'));
        $this->assertEquals($target, $codeGenFile->generate());
    }
 /**
  * Generate class
  * @return string generated source code
  */
 public function generate()
 {
     $table = new ModelGenerator_Table_Table($this->_options['tableName']);
     $className = $this->_getNamer()->formatClassName($this->_config->base->classname);
     $templates = array('tags' => array());
     foreach ($this->_options['docblock'] as $tag => $value) {
         $templates['tags'][] = array('name' => $tag, 'description' => $value);
     }
     ////////////////////////////////////////////
     // create model base
     ////////////////////////////////////////////
     $methods = array();
     $properties = array();
     $tmp = array();
     foreach ($table->getProperties() as $property) {
         $tmp[] = array('name' => 'property', 'description' => $property['type'] . ' $' . $property['name'] . ' ' . $property['desc']);
         switch ($property['type']) {
             case 'string':
                 $property['defaultValue'] = '';
                 break;
             case 'int':
             case 'float':
             case 'double':
                 $property['defaultValue'] = null;
                 break;
             default:
                 $property['defaultValue'] = '';
                 break;
         }
         $properties[] = new Zend_CodeGenerator_Php_Property(array('name' => '_' . $property['name'], 'docblock' => new Zend_CodeGenerator_Php_Docblock(array('tags' => array(array('name' => 'var', 'description' => $property['type'])))), 'defaultValue' => $property['defaultValue'], 'visibility' => 'private'));
         // getters and setters
         if (false !== strpos($property['desc'], 'enum')) {
             $types = rtrim(str_replace(array('enum('), '', $property['desc']), ')');
             $types = explode(',', $types);
             $typesImploded = implode(', ', $types);
             $enumBody = 'in_array($' . $property['name'] . ', array(' . $typesImploded . ')) ? $' . $property['name'] . ' : ' . $types[0];
             $body = '$' . $property['name'] . ' = (' . $property['type'] . ') $' . $property['name'] . ';' . PHP_EOL . '$this->_' . $property['name'] . ' = ' . $enumBody . ';' . PHP_EOL . 'return $this;';
         } else {
             $body = '$this->_' . $property['name'] . ' = (' . $property['type'] . ') $' . $property['name'] . ';' . PHP_EOL . 'return $this;';
         }
         $methods[] = new Zend_CodeGenerator_Php_Method(array('name' => 'set' . $this->_getNamer()->formatMethodName($property['name']), 'docblock' => new Zend_CodeGenerator_Php_Docblock(array('shortDescription' => 'Set ' . $property['name'] . ' (' . $property['desc'] . ')', 'tags' => array(new Zend_CodeGenerator_Php_Docblock_Tag_Param(array('paramName' => $property['name'], 'dataType' => $property['type'])), array('name' => 'return', 'description' => $className)))), 'parameters' => array(array('name' => $property['name'])), 'body' => $body));
         $methods[] = new Zend_CodeGenerator_Php_Method(array('name' => 'get' . $this->_getNamer()->formatMethodName($property['name']), 'docblock' => new Zend_CodeGenerator_Php_Docblock(array('shortDescription' => 'Get ' . $property['name'] . ' (' . $property['desc'] . ')', 'tags' => array(array('name' => 'return', 'description' => $property['type'])))), 'parameters' => array(), 'body' => 'return $this->_' . $property['name'] . ';'));
     }
     $toArrayBody[] = '$data = array(';
     foreach ($table->getProperties() as $property) {
         $toArrayBody[] = "\t" . '\'' . $property['name'] . '\' => $this->_' . $property['name'] . ',';
     }
     $toArrayBody[] = ');';
     $toArrayBody[] = '';
     $toArrayBody[] = 'return $data;';
     $methods[] = new Zend_CodeGenerator_Php_Method(array('name' => 'toArray', 'docblock' => new Zend_CodeGenerator_Php_Docblock(array('shortDescription' => 'Convert entity properties to array and return it', 'tags' => array(array('name' => 'return', 'description' => 'array')))), 'parameters' => array(), 'body' => implode(PHP_EOL, $toArrayBody)));
     $modelBase = new Zend_CodeGenerator_Php_Class(array('name' => $className, 'docblock' => new Zend_CodeGenerator_Php_Docblock(array('shortDescription' => $className . PHP_EOL . '*DO NOT* edit this file.', 'tags' => array_merge($tmp, $templates['tags']))), 'methods' => $methods, 'properties' => $properties));
     $modelBaseFile = new Zend_CodeGenerator_Php_File(array('classes' => array($modelBase)));
     return $modelBaseFile->generate();
 }
Example #18
0
 /**
  * getContents()
  *
  * @return array
  */
 public function getContents()
 {
     $codeGenFile = new Zend_CodeGenerator_Php_File(array('classes' => array(new Zend_CodeGenerator_Php_Class(array('name' => 'Bootstrap', 'extendedClass' => 'Zend_Application_Bootstrap_Bootstrap')))));
     return $codeGenFile->generate();
 }
    /**
     * getContents()
     *
     * @return string
     */
    public function getContents()
    {
        $filter = new Zend_Filter_Word_DashToCamelCase();
        $className = $filter->filter($this->_forControllerName) . 'ControllerTest';
        /* @var $controllerDirectoryResource Zend_Tool_Project_Profile_Resource */
        $controllerDirectoryResource = $this->_resource->getParentResource();
        if ($controllerDirectoryResource->getParentResource()->getName() == 'TestApplicationModuleDirectory') {
            $className = $filter->filter(ucfirst($controllerDirectoryResource->getParentResource()->getForModuleName())) . '_' . $className;
        }
        $codeGenFile = new Zend_CodeGenerator_Php_File(array('classes' => array(new Zend_CodeGenerator_Php_Class(array('name' => $className, 'extendedClass' => 'Zend_Test_PHPUnit_ControllerTestCase', 'methods' => array(new Zend_CodeGenerator_Php_Method(array('name' => 'setUp', 'body' => <<<EOS
\$this->bootstrap = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');
parent::setUp();
EOS
))))))));
        return $codeGenFile->generate();
    }
Example #20
0
 /**
  * @return string
  */
 public function getContents()
 {
     $classMapperName = $this->getFullClassName($this->_modelName, 'Model_Mapper');
     $className = $this->getFullClassName($this->_modelName, 'Model');
     $classDbTableName = $this->getFullClassName($this->_modelName, 'Model_DbTable');
     $codeGenFile = new Zend_CodeGenerator_Php_File(array('fileName' => $this->getPath(), 'classes' => array(new Zend_CodeGenerator_Php_Class(array('name' => $classMapperName, 'properties' => array(array('name' => '_dbTable', 'visibility' => 'protected')), 'methods' => array(new Zend_CodeGenerator_Php_Method(array('name' => 'setDbTable', 'parameters' => array(array('name' => 'dbTable')), 'body' => 'if (is_string($dbTable))' . "\n\t" . '$dbTable = new $dbTable();' . "\n\n" . 'if (!$dbTable instanceof Zend_Db_Table_Abstract)' . "\n\t" . 'throw new Exception(\'Invalid table data gateway provided\');' . "\n\n" . '$this->_dbTable = $dbTable;' . "\n\n" . 'return $this;', 'docblock' => new Zend_CodeGenerator_Php_Docblock(array('tags' => array(array('name' => 'param', 'description' => '$dbTable'), array('name' => 'return', 'description' => '$this'), array('name' => 'throws', 'description' => 'Exception')))))), new Zend_CodeGenerator_Php_Method(array('name' => 'getDbTable', 'body' => 'if (null === $this->_dbTable)' . "\n\t" . '$this->setDbTable(\'' . $classDbTableName . '\');' . "\n\n" . 'return $this->_dbTable;', 'docblock' => new Zend_CodeGenerator_Php_Docblock(array('tags' => array(array('name' => 'return', 'description' => $classDbTableName)))))), new Zend_CodeGenerator_Php_Method(array('name' => 'save', 'parameters' => array(array('name' => strtolower($this->_modelName), 'type' => $className)), 'body' => '$data = $this->_getDbData($' . strtolower($this->_modelName) . ');' . "\n\n" . 'if (null == ($id = $' . strtolower($this->_modelName) . '->getId())) {' . "\n\t" . 'unset($data[$this->_getDbPrimary()]);' . "\n\t" . '$this->getDbTable()->insert($data);' . "\n" . '} else {' . "\n\t" . '$this->getDbTable()->update($data, array($this->_getDbPrimary(). \' = ?\' => $id));' . "\n" . '}' . "\n\n" . 'return $this;', 'docblock' => new Zend_CodeGenerator_Php_Docblock(array('tags' => array(array('name' => 'param', 'description' => $className . ' $' . strtolower($this->_modelName)), array('name' => 'return', 'description' => '$this')))))), new Zend_CodeGenerator_Php_Method(array('name' => 'find', 'parameters' => array(array('name' => 'id'), array('name' => strtolower($this->_modelName), 'type' => $className)), 'body' => '$result = $this->getDbTable()->find($id);' . "\n\n" . 'if (0 == count($result)) {' . "\n\t" . 'return null;' . "\n" . '}' . "\n\n" . '$row = $result->current();' . "\n" . '$entry = $this->_setDbData($row, $' . strtolower($this->_modelName) . ');' . "\n\n" . 'return $entry;', 'docblock' => new Zend_CodeGenerator_Php_Docblock(array('tags' => array(array('name' => 'param', 'description' => '$id'), array('name' => 'param', 'description' => $className . ' $' . strtolower($this->_modelName)), array('name' => 'return', 'description' => $className . '|null')))))), new Zend_CodeGenerator_Php_Method(array('name' => 'fetchAll', 'parameters' => array(array('name' => 'select', 'defaultValue' => null)), 'body' => '$resultSet = $this->getDbTable()->fetchAll($select);' . "\n\n" . '$entries   = array();' . "\n" . 'foreach ($resultSet as $row) {' . "\n\t" . '$entry = new ' . $className . '();' . "\n\t" . '$entry = $this->_setDbData($row, $entry);' . "\n\t" . '$entries[] = $entry;' . "\n" . '}' . "\n\n" . 'return $entries;', 'docblock' => new Zend_CodeGenerator_Php_Docblock(array('tags' => array(array('name' => 'param', 'description' => 'null $select'), array('name' => 'return', 'description' => 'array')))))), new Zend_CodeGenerator_Php_Method(array('name' => '_getDbPrimary', 'visibility' => 'protected', 'body' => '$primaryKey = $this->getDbTable()->info(\'primary\');' . "\n\n" . 'return $primaryKey[1];', 'docblock' => new Zend_CodeGenerator_Php_Docblock(array('tags' => array(array('name' => 'return', 'description' => 'mixed')))))), new Zend_CodeGenerator_Php_Method(array('name' => '_getDbData', 'parameters' => array(array('name' => strtolower($this->_modelName), 'type' => $className)), 'visibility' => 'protected', 'body' => '$info = $this->getDbTable()->info();' . "\n" . '$properties = $info[\'cols\'];' . "\n\n" . '$data = array();' . "\n" . 'foreach ($properties as $property) {' . "\n\t" . '$name = $this->_normaliseName($property);' . "\n\n\t" . 'if($property != $this->_getDbPrimary())' . "\n\t\t" . '$data[$property] = $' . strtolower($this->_modelName) . '->__get($name);' . "\n" . '}' . "\n\n" . 'return $data;', 'docblock' => new Zend_CodeGenerator_Php_Docblock(array('tags' => array(array('name' => 'param', 'description' => $className . ' $' . strtolower($this->_modelName)), array('name' => 'return', 'description' => 'array')))))), new Zend_CodeGenerator_Php_Method(array('name' => '_setDbData', 'parameters' => array(array('name' => 'row'), array('name' => 'entry', 'type' => $className)), 'visibility' => 'protected', 'body' => '$info = $this->getDbTable()->info();' . "\n" . '$properties = $info[\'cols\'];' . "\n\n" . 'foreach ($properties as $property) {' . "\n\t" . '$entry->__set($this->_normaliseName($property), $row->$property);' . "\n" . '}' . "\n\n" . 'return $entry;', 'docblock' => new Zend_CodeGenerator_Php_Docblock(array('tags' => array(array('name' => 'param', 'description' => 'Zend_Db_Table_Rowset $row'), array('name' => 'param', 'description' => $className . ' $entry'), array('name' => 'return', 'description' => $className)))))), new Zend_CodeGenerator_Php_Method(array('name' => '_normaliseName', 'parameters' => array(array('name' => 'property')), 'visibility' => 'protected', 'body' => '$name = preg_split(\'~_~\', $property);' . "\n" . '$normaliseName = implode(array_map(\'ucwords\', $name));' . "\n\n" . 'return $normaliseName;', 'docblock' => new Zend_CodeGenerator_Php_Docblock(array('tags' => array(array('name' => 'param', 'description' => '$property string'), array('name' => 'return', 'description' => 'string'))))))))))));
     return $codeGenFile->generate();
 }
Example #21
0
    } else {
        $index_template_replacements = array("OBJECT_NICENAME" => $name, "THEME_GLOBAL_PATH_VAR_NAME" => "theme_global_path", "CREATE_NEW_URL" => "/" . $module_name . "/" . strtolower($name) . "/edit", "ROWSET_VAR" => strtolower($name), "THE_ID" => $identity_col, "INDEX_URL" => $index_url);
    }
    $index_template = Bolts_Common::replaceWithArray($index_template, $index_template_replacements);
    file_put_contents($theme_dir . "/index.tpl", $index_template);
    // add delete method
    $delete_method = new Zend_CodeGenerator_Php_Method();
    $delete_method_body = file_get_contents($basepath . "/modules/bolts/extras/crudify_templates/deleteAction.txt");
    $delete_action_replacements = array("TABLE_OBJECT_VAR" => $table_object_var, "TABLE_CLASSNAME" => $object_name, "ROW_OBJECT_VAR" => $row_object_var, "THE_ID" => $identity_col, "OBJECT_NICENAME" => $name, "ROWSET_VAR" => strtolower($name), "DELETE_URL" => $delete_url, "INDEX_URL" => $index_url);
    $delete_method_body = Bolts_Common::replaceWithArray($delete_method_body, $delete_action_replacements);
    $delete_method->setName('deleteAction')->setBody($delete_method_body);
    $controller_class->setMethod($delete_method);
    // load delete template
    $delete_template = file_get_contents($basepath . "/modules/bolts/extras/crudify_templates/delete.tpl");
    if ($is_admin) {
        $delete_template_replacements = array("OBJECT_NICENAME" => $name, "THEME_GLOBAL_PATH_VAR_NAME" => "admin_theme_global_path", "CREATE_NEW_URL" => "/" . $module_name . "/" . strtolower($name) . "admin/edit", "ROWSET_VAR" => strtolower($name), "THE_ID" => $identity_col, "DELETE_URL" => $delete_url, "INDEX_URL" => $index_url);
    } else {
        $delete_template_replacements = array("OBJECT_NICENAME" => $name, "THEME_GLOBAL_PATH_VAR_NAME" => "theme_global_path", "CREATE_NEW_URL" => "/" . $module_name . "/" . strtolower($name) . "/edit", "ROWSET_VAR" => strtolower($name), "THE_ID" => $identity_col, "DELETE_URL" => $delete_url, "INDEX_URL" => $index_url);
    }
    $delete_template = Bolts_Common::replaceWithArray($delete_template, $delete_template_replacements);
    file_put_contents($theme_dir . "/delete.tpl", $delete_template);
    $controller_file = new Zend_CodeGenerator_Php_File();
    $controller_file->setClass($controller_class);
}
// Render the generated files
file_put_contents($basepath . "/modules/" . $module_name . "/models/" . $object_name . ".php", $model_file->generate());
if ($is_admin) {
    file_put_contents($basepath . "/modules/" . $module_name . "/controllers/" . $name . "adminController.php", $controller_file->generate());
} else {
    file_put_contents($basepath . "/modules/" . $module_name . "/controllers/" . $name . "Controller.php", $controller_file->generate());
}
Example #22
0
 /**
  * getContents()
  *
  * @return string
  */
 public function getContents()
 {
     $filter = new Zend_Filter_Word_DashToCamelCase();
     $className = $filter->filter($this->_projectProviderName) . 'Provider';
     $class = new Zend_CodeGenerator_Php_Class(array('name' => $className, 'extendedClass' => 'Zend_Tool_Project_Provider_Abstract'));
     $methods = array();
     foreach ($this->_actionNames as $actionName) {
         $methods[] = new Zend_CodeGenerator_Php_Method(array('name' => $actionName, 'body' => '        /** @todo Implementation */'));
     }
     if ($methods) {
         $class->setMethods($methods);
     }
     $codeGenFile = new Zend_CodeGenerator_Php_File(array('requiredFiles' => array('Zend/Tool/Project/Provider/Abstract.php', 'Zend/Tool/Project/Provider/Exception.php'), 'classes' => array($class)));
     return $codeGenFile->generate();
 }
Example #23
0
 public function getContents()
 {
     $className = $this->getFullClassName($this->_modelName, 'Model');
     $codeGenFile = new Zend_CodeGenerator_Php_File(array('fileName' => $this->getPath(), 'classes' => array(new Zend_CodeGenerator_Php_Class(array('name' => $className)))));
     return $codeGenFile->generate();
 }
Example #24
0
 /**
  * Method create's new migration file
  *
  * @param  string $module Module name
  * @param null    $migrationBody
  * @param string  $label
  * @param string  $desc
  * @return string Migration name
  */
 public function create($module = null, $migrationBody = null, $label = '', $desc = '')
 {
     $path = $this->getMigrationsDirectoryPath($module);
     list($sec, $msec) = explode(".", microtime(true));
     $_migrationName = date('Ymd_His_') . substr($msec, 0, 2);
     if (!empty($label)) {
         $_migrationName .= '_' . $label;
     }
     // Configuring after instantiation
     $methodUp = new Zend_CodeGenerator_Php_Method();
     $methodUp->setName('up')->setBody('// upgrade');
     // Configuring after instantiation
     $methodDown = new Zend_CodeGenerator_Php_Method();
     $methodDown->setName('down')->setBody('// degrade');
     //add description
     if (!empty($desc)) {
         $methodDesc = new Zend_CodeGenerator_Php_Method();
         $methodDesc->setName('getDescription')->setBody("return '" . addslashes($desc) . "'; ");
     }
     if ($migrationBody) {
         if (isset($migrationBody['up'])) {
             $upBody = '';
             foreach ($migrationBody['up'] as $query) {
                 $upBody .= '$this->query(\'' . $query . '\');' . PHP_EOL;
             }
             $methodUp->setBody($upBody);
         }
         if (isset($migrationBody['down'])) {
             $downBody = '';
             foreach ($migrationBody['down'] as $query) {
                 $downBody .= '$this->query(\'' . $query . '\');' . PHP_EOL;
             }
             $methodDown->setBody($downBody);
         }
     }
     $class = new Zend_CodeGenerator_Php_Class();
     $className = (null !== $module ? ucfirst($module) . '_' : '') . 'Migration_' . $_migrationName;
     $class->setName($className)->setExtendedClass('Core_Migration_Abstract')->setMethod($methodUp)->setMethod($methodDown);
     if (isset($methodDesc)) {
         $class->setMethod($methodDesc);
     }
     $file = new Zend_CodeGenerator_Php_File();
     $file->setClass($class)->setFilename($path . '/' . $_migrationName . '.php')->write();
     return $_migrationName;
 }
    /**
     * create()
     *
     * @return Zend_Tool_Project_Context_Zf_ActionMethod
     */
    public function create()
    {
        $file = $this->_testApplicationControllerPath;
        if (!file_exists($file)) {
            // require_once 'Zend/Tool/Project/Context/Exception.php';
            throw new Zend_Tool_Project_Context_Exception('Could not create action within test controller ' . $file . ' with action name ' . $this->_forActionName);
        }
        $actionParam = $this->getForActionName();
        $controllerParam = $this->_resource->getParentResource()->getForControllerName();
        //$moduleParam = null;//
        /* @var $controllerDirectoryResource Zend_Tool_Project_Profile_Resource */
        $controllerDirectoryResource = $this->_resource->getParentResource()->getParentResource();
        if ($controllerDirectoryResource->getParentResource()->getName() == 'TestApplicationModuleDirectory') {
            $moduleParam = $controllerDirectoryResource->getParentResource()->getForModuleName();
        } else {
            $moduleParam = 'default';
        }
        if ($actionParam == 'index' && $controllerParam == 'Index' && $moduleParam == 'default') {
            $assert = '$this->assertQueryContentContains("div#welcome h3", "This is your project\'s main page");';
        } else {
            $assert = <<<EOS
\$this->assertQueryContentContains(
    'div#view-content p',
    'View script for controller <b>' . \$params['controller'] . '</b> and script/action name <b>' . \$params['action'] . '</b>'
    );
EOS;
        }
        $codeGenFile = Zend_CodeGenerator_Php_File::fromReflectedFileName($file, true, true);
        $codeGenFile->getClass()->setMethod(array('name' => 'test' . ucfirst($actionParam) . 'Action', 'body' => <<<EOS
\$params = array('action' => '{$actionParam}', 'controller' => '{$controllerParam}', 'module' => '{$moduleParam}');
\$urlParams = \$this->urlizeOptions(\$params);
\$url = \$this->url(\$urlParams);
\$this->dispatch(\$url);

// assertions
\$this->assertModule(\$urlParams['module']);
\$this->assertController(\$urlParams['controller']);
\$this->assertAction(\$urlParams['action']);
{$assert}

EOS
));
        file_put_contents($file, $codeGenFile->generate());
        return $this;
    }
Example #26
0
 /**
  * hasActionMethod()
  *
  * @param string $controllerPath
  * @param string $actionName
  * @return bool
  */
 public static function hasActionMethod($controllerPath, $actionName)
 {
     if (!file_exists($controllerPath)) {
         return false;
     }
     $controllerCodeGenFile = Zend_CodeGenerator_Php_File::fromReflectedFileName($controllerPath, true, true);
     return $controllerCodeGenFile->getClass()->hasMethod($actionName . 'Action');
 }
 /**
  * getCodeGenerator()
  *
  * @return Zend_CodeGenerator_Php_Class
  */
 public function getCodeGenerator()
 {
     $codeGenFile = Zend_CodeGenerator_Php_File::fromReflectedFileName($this->getPath());
     $codeGenFileClasses = $codeGenFile->getClasses();
     $class = array_shift($codeGenFileClasses);
     return $class;
 }
Example #28
0
    public function testFromReflectionFile()
    {
        ///$this->markTestSkipped('skipme');
        $file = dirname(__FILE__) . '/_files/TestSampleSingleClass.php';
        require_once $file;
        $codeGenFileFromDisk = Zend_CodeGenerator_Php_File::fromReflection(new Zend_Reflection_File($file));
        $codeGenFileFromDisk->getClass()->setMethod(array('name' => 'foobar'));
        $expectedOutput = <<<EOS
<?php
/**
 * File header here
 * 
 * @author Ralph Schindler <*****@*****.**>
 * 
 */




/**
 * class docblock
 * 
 * @package Zend_Reflection_TestSampleSingleClass
 * 
 */
class Zend_Reflection_TestSampleSingleClass
{

    /**
     * Enter description here...
     * 
     * @return bool
     * 
     */
    public function someMethod()
    {
        /* test test */
    }

    public function foobar()
    {
    }


}




EOS;
        $this->assertEquals($expectedOutput, $codeGenFileFromDisk->generate());
    }
Example #29
0
    /**
     * Generate the action
     *
     * This is a gigantic method used to generate the actual Action code. It
     * uses the properties, description, name, and routes passed to it.
     *
     * This method uses the Zend_CodeGenerator_Php_* family to identify and create the
     * new files.
     *
     * @param string $name  The name of the action
     * @param array $properties An array of properties related to an action
     * @param string $description A description for an action. Default false.
     * @param string $route The custom route for that action. Default false.
     *
     * @return string A generated file.
     */
    private function generateAction($name, $properties, $description = false, $route = false)
    {
        $docblock = new Zend_CodeGenerator_Php_Docblock(array('shortDescription' => 'Required parameters', 'tags' => array(new Zend_CodeGenerator_Php_Docblock_Tag(array('name' => 'var', 'datatype' => 'array', 'description' => 'An array of required parameters.')))));
        $class = new Zend_CodeGenerator_Php_Class();
        $class->setName('Action_' . $name);
        $class->setExtendedClass('Frapi_Action');
        $class->setImplementedInterfaces(array('Frapi_Action_Interface'));
        $tags = array(array('name' => 'link', 'description' => 'http://getfrapi.com'), array('name' => 'author', 'description' => 'Frapi <*****@*****.**>'));
        if ($route !== false) {
            $tags[] = array('name' => 'link', 'description' => $route);
        }
        $classDocblock = new Zend_CodeGenerator_Php_Docblock(array('shortDescription' => 'Action ' . $name . ' ', 'longDescription' => $description !== false ? $description : 'A class generated by Frapi', 'tags' => $tags));
        $class->setDocblock($classDocblock);
        $class->setProperties(array(array('name' => 'requiredParams', 'visibility' => 'protected', 'defaultValue' => $properties, 'docblock' => $docblock), array('name' => 'data', 'visibility' => 'private', 'defaultValue' => array(), 'docblock' => new Zend_CodeGenerator_Php_Docblock(array('shortDescription' => 'The data container to use in toArray()', 'tags' => array(new Zend_CodeGenerator_Php_Docblock_Tag(array('name' => 'var', 'datatype' => 'array', 'description' => 'A container of data to fill and return in toArray()'))))))));
        $methods = array();
        $docblock = new Zend_CodeGenerator_Php_Docblock(array('shortDescription' => 'To Array', 'longDescription' => "This method returns the value found in the database \n" . 'into an associative array.', 'tags' => array(new Zend_CodeGenerator_Php_Docblock_Tag_Return(array('datatype' => 'array')))));
        $toArrayBody = '        ' . "\n";
        if (!empty($properties)) {
            foreach ($properties as $p) {
                $toArrayBody .= '$this->data[\'' . $p . '\'] = ' . '$this->getParam(\'' . $p . '\', self::TYPE_OUTPUT);' . "\n";
            }
        }
        $toArrayBody .= 'return $this->data;';
        $methods[] = new Zend_CodeGenerator_Php_Method(array('name' => 'toArray', 'body' => $toArrayBody, 'docblock' => $docblock));
        $executeActionBody = '';
        if (!empty($properties)) {
            $executeActionBody = '        $valid = $this->hasRequiredParameters($this->requiredParams);
if ($valid instanceof Frapi_Error) {
    return $valid;
}';
        }
        $executeActionBody .= "\n\n" . 'return $this->toArray();';
        $docblockArray = array('shortDescription' => '', 'longDescription' => '', 'tags' => array(new Zend_CodeGenerator_Php_Docblock_Tag_Return(array('datatype' => 'array'))));
        $docblock = new Zend_CodeGenerator_Php_Docblock(array());
        $docblockArray['shortDescription'] = 'Default Call Method';
        $docblockArray['longDescription'] = 'This method is called when no specific ' . 'request handler has been found';
        $methods[] = new Zend_CodeGenerator_Php_Method(array('name' => 'executeAction', 'body' => $executeActionBody, 'docblock' => $docblockArray));
        $docblockArray['shortDescription'] = 'Get Request Handler';
        $docblockArray['longDescription'] = 'This method is called when a request is a GET';
        $methods[] = new Zend_CodeGenerator_Php_Method(array('name' => 'executeGet', 'body' => $executeActionBody, 'docblock' => $docblockArray));
        $docblockArray['shortDescription'] = 'Post Request Handler';
        $docblockArray['longDescription'] = 'This method is called when a request is a POST';
        $methods[] = new Zend_CodeGenerator_Php_Method(array('name' => 'executePost', 'body' => $executeActionBody, 'docblock' => $docblockArray));
        $docblockArray['shortDescription'] = 'Put Request Handler';
        $docblockArray['longDescription'] = 'This method is called when a request is a PUT';
        $methods[] = new Zend_CodeGenerator_Php_Method(array('name' => 'executePut', 'body' => $executeActionBody, 'docblock' => $docblockArray));
        $docblockArray['shortDescription'] = 'Delete Request Handler';
        $docblockArray['longDescription'] = 'This method is called when a request is a DELETE';
        $methods[] = new Zend_CodeGenerator_Php_Method(array('name' => 'executeDelete', 'body' => $executeActionBody, 'docblock' => $docblockArray));
        $docblockArray['shortDescription'] = 'Head Request Handler';
        $docblockArray['longDescription'] = 'This method is called when a request is a HEAD';
        $methods[] = new Zend_CodeGenerator_Php_Method(array('name' => 'executeHead', 'body' => $executeActionBody, 'docblock' => $docblockArray));
        $class->setMethods($methods);
        $file = new Zend_CodeGenerator_Php_File();
        $file->setClass($class);
        return $file->generate();
    }
Example #30
0
 public function getContents()
 {
     $className = $this->getFullClassName($this->_formName, 'Form');
     $codeGenFile = new Zend_CodeGenerator_Php_File(array('fileName' => $this->getPath(), 'classes' => array(new Zend_CodeGenerator_Php_Class(array('name' => $className, 'extendedClass' => 'Zend_Form', 'methods' => array(new Zend_CodeGenerator_Php_Method(array('name' => 'init', 'body' => '/* Form Elements & Other Definitions Here ... */'))))))));
     return $codeGenFile->generate();
 }