Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/MontmereLimited/zf2 into …
Browse files Browse the repository at this point in the history
…hotfix/tool-refactoring
  • Loading branch information
weierophinney committed Oct 24, 2011
2 parents 6921c05 + 0befcea commit bf23582
Show file tree
Hide file tree
Showing 13 changed files with 154 additions and 188 deletions.
35 changes: 31 additions & 4 deletions library/Zend/Code/Generator/FileGeneratorRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,37 @@

namespace Zend\Code\Generator;

use Zend\Code\Generator\Exception\RuntimeException;

class FileGeneratorRegistry
{
/**
* @todo Complete this when Zend\Tool get's refractored as it is the only
* piece that cares if its "double reflecting" file generators
*/
/**
* @var array[string]\Zend\Code\Generator\FileGenerator $_fileCodeGenerators registry for Zend\Code\Generator\FileGenerator
*/
static private $_fileCodeGenerators = array();

/**
* Registry for the Zend_Code package. Zend_Tool uses this
*
* @param FileGenerator $fileCodeGenerator
* @param string $fileName
* @throws RuntimeException
*/
public static function registerFileCodeGenerator(FileGenerator $fileCodeGenerator, $fileName = null)
{
if ($fileName == null) {
$fileName = $fileCodeGenerator->getFilename();
}

if ($fileName == '') {
throw new RuntimeException('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;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
*/
namespace Zend\Tool\Project\Context\Content\Engine;

use Zend\CodeGenerator\AbstractCodeGenerator,
Zend\CodeGenerator\Php as PhpCodeGenerator,
use Zend\Code\Generator\AbstractGenerator,
Zend\Code\Generator\FileGenerator,
Zend\Tool\Framework\Client\Storage,
Zend\Tool\Project\Context,
Zend\Tool\Project\Exception;
Expand Down Expand Up @@ -92,12 +92,12 @@ public function getContent(Context $context, $method, $parameters)
if (method_exists($context, 'getCodeGenerator')) {
$codeGenerator = $context->getCodeGenerator();
} else {
$codeGenerator = new PhpCodeGenerator\PhpFile();
$codeGenerator = new FileGenerator();
}

$codeGenerator = include $streamUri;

if (!$codeGenerator instanceof AbstractCodeGenerator) {
if (!$codeGenerator instanceof AbstractGenerator) {
throw new Exception\RuntimeException('Custom file at ' . $streamUri . ' did not return the $codeGenerator object.');
}

Expand Down
19 changes: 10 additions & 9 deletions library/Zend/Tool/Project/Context/Zf/ActionMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,20 @@
namespace Zend\Tool\Project\Context\Zf;

use Zend\Tool\Project\Context\Context,
Zend\CodeGenerator\Php,
Zend\Tool\Project\Context\Exception,
Zend\Tool\Project\Profile\Resource\Resource;
Zend\Tool\Project\Profile\Resource\Resource,
Zend\Code\Generator\FileGenerator,
Zend\Code\Generator\ClassGenerator,
Zend\Code\Generator\MethodGenerator;


/**
* This class is the front most class for utilizing Zend\Tool\Project
*
* A profile is a hierarchical set of resources that keep track of
* items within a specific project.
*
* @uses \Zend\CodeGenerator\Php\PhpFile
* @uses \Zend\Code\Generator\ClassGenerator
* @uses \Zend\Reflection\ReflectionFile
* @uses \Zend\Tool\Project\Context\Exception
* @uses \Zend\Tool\Project\Context
Expand Down Expand Up @@ -186,11 +189,9 @@ public static function createActionMethod($controllerPath, $actionName, $body =
return false;
}

$controllerCodeGenFile = Php\PhpFile::fromReflectedFileName($controllerPath, true, true);
$controllerCodeGenFile->getClass()->setMethod(array(
'name' => $actionName . 'Action',
'body' => $body
));
$controllerCodeGenFile = FileGenerator::fromReflectedFileName($controllerPath, true, true);
$controllerCodeGenFile->setClass(new ClassGenerator(basename($controllerPath, '.php')));
$controllerCodeGenFile->getClass()->setMethod(new MethodGenerator($actionName . 'Action', array(), MethodGenerator::FLAG_PUBLIC, $body));

file_put_contents($controllerPath, $controllerCodeGenFile->generate());
return true;
Expand All @@ -209,7 +210,7 @@ public static function hasActionMethod($controllerPath, $actionName)
return false;
}

$controllerCodeGenFile = Php\PhpFile::fromReflectedFileName($controllerPath, true, true);
$controllerCodeGenFile = FileGenerator::fromReflectedFileName($controllerPath, true, true);
return $controllerCodeGenFile->getClass()->hasMethod($actionName . 'Action');
}

Expand Down
15 changes: 6 additions & 9 deletions library/Zend/Tool/Project/Context/Zf/BootstrapFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
* items within a specific project.
*
* @uses \Zend\Application\Application
* @uses \Zend\CodeGenerator\Php\PhpClass
* @uses \Zend\CodeGenerator\Php\PhpFile
* @uses \Zend\Code\Generator\ClassGenerator
* @uses \Zend\Code\Generator\FileGenerator
* @uses \Zend\Tool\Project\Context\Filesystem\File
* @uses \Zend\Tool\Project\Exception
* @category Zend
Expand Down Expand Up @@ -97,14 +97,11 @@ public function init()
public function getContents()
{

$codeGenFile = new \Zend\CodeGenerator\Php\PhpFile(array(
$codeGenFile = new \Zend\Code\Generator\FileGenerator(array(
'classes' => array(
new \Zend\CodeGenerator\Php\PhpClass(array(
'name' => 'Bootstrap',
'extendedClass' => '\Zend\Application\Bootstrap',
)),
)
));
new \Zend\Code\Generator\ClassGenerator('Bootstrap', null, null, '\Zend\Application\Bootstrap'),
)
));

return $codeGenFile->generate();
}
Expand Down
71 changes: 26 additions & 45 deletions library/Zend/Tool/Project/Context/Zf/ControllerFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,20 @@
*/
namespace Zend\Tool\Project\Context\Zf;

use Zend\CodeGenerator\Php;
use Zend\Code\Generator\FileGeneratorRegistry,
Zend\Code\Generator\MethodGenerator,
Zend\Code\Generator\ClassGenerator,
Zend\Code\Generator\FileGenerator;

/**
* This class is the front most class for utilizing Zend\Tool\Project
*
* A profile is a hierarchical set of resources that keep track of
* items within a specific project.
*
* @uses \Zend\CodeGenerator\Php\PhpClass
* @uses \Zend\CodeGenerator\Php\PhpFile
* @uses \Zend\CodeGenerator\Php\PhpMethod
* @uses \Zend\Code\Generator\ClassGenerator
* @uses \Zend\Code\Generator\FileGenerator
* @uses \Zend\Code\Generator\MethodGenerator
* @category Zend
* @package Zend_Tool
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
Expand Down Expand Up @@ -112,35 +115,19 @@ public function getContents()
$className = ($this->_moduleName) ? ucfirst($this->_moduleName) . '\\' : '';
$className .= ucfirst($this->_controllerName) . 'Controller';

$codeGenFile = new Php\PhpFile(array(
'fileName' => $this->getPath(),
'classes' => array(
new Php\PhpClass(array(
'name' => $className,
'extendedClass' => '\Zend\Controller\Action',
'methods' => array(
new Php\PhpMethod(array(
'name' => 'init',
'body' => '/* Initialize action controller here */',
))
)
))
)
));


$codeGenFile = new FileGenerator();
$codeGenFile->setFilename($this->getPath());
$cg = new ClassGenerator($className);
$cg->setMethod(new MethodGenerator('init', array(), null, '/* Initialize action controller here */'));
$codeGenFile->setClass($cg);

if ($className == 'ErrorController') {

$codeGenFile = new Php\PhpFile(array(
'fileName' => $this->getPath(),
'classes' => array(
new Php\PhpClass(array(
'name' => $className,
'extendedClass' => 'Zend\Controller\Action',
'methods' => array(
new Php\PhpMethod(array(
'name' => 'errorAction',
'body' => <<<'EOS'

$codeGenFile = new FileGenerator();
$codeGenFile->setFilename($this->getPath());
$cg = new ClassGenerator($className);

$cg->setMethods(array(new MethodGenerator('errorAction', array(), null, <<<'EOS'
$errors = $this->_getParam('error_handler');
if (!$errors || !$errors instanceof \ArrayObject) {
Expand Down Expand Up @@ -178,10 +165,7 @@ public function getContents()
$this->view->vars()->request = $errors->request;
EOS
)),
new Php\PhpMethod(array(
'name' => 'getLog',
'body' => <<<'EOS'
), new MethodGenerator('getLog', array(), null, <<<'EOS'
/* @var $bootstrap Zend\Application\Bootstrap */
$bootstrap = $this->getInvokeArg('bootstrap');
if (!$bootstrap->getBroker()->hasPlugin('Log')) {
Expand All @@ -190,16 +174,13 @@ public function getContents()
$log = $bootstrap->getResource('Log');
return $log;
EOS
)),
)
))
)
));

)
);
}

// store the generator into the registry so that the addAction command can use the same object later
Php\PhpFile::registerFileCodeGenerator($codeGenFile); // REQUIRES filename to be set
FileGeneratorRegistry::registerFileCodeGenerator($codeGenFile); // REQUIRES filename to be set
return $codeGenFile->generate();
}

Expand All @@ -211,18 +192,18 @@ public function getContents()
public function addAction($actionName)
{
$classCodeGen = $this->getCodeGenerator();
$classCodeGen->setMethod(array('name' => $actionName . 'Action', 'body' => ' // action body here'));
$classCodeGen->setMethod(new MethodGenerator($actionName . 'Action', array(), null, ' // action body here'));
file_put_contents($this->getPath(), $classCodeGen->generate());
}

/**
* getCodeGenerator()
*
* @return \Zend\CodeGenerator\Php\PhpClass
* @return \Zend\Code\Generator\FileGenerator
*/
public function getCodeGenerator()
{
$codeGenFile = Php\PhpFile::fromReflectedFileName($this->getPath());
$codeGenFile = FileGenerator::fromReflectedFileName($this->getPath());
$codeGenFileClasses = $codeGenFile->getClasses();
$class = array_shift($codeGenFileClasses);
return $class;
Expand Down
29 changes: 10 additions & 19 deletions library/Zend/Tool/Project/Context/Zf/DbTableFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,19 @@
* @namespace
*/
namespace Zend\Tool\Project\Context\Zf;
use Zend\CodeGenerator\Php;
use Zend\Code\Generator\FileGenerator,
Zend\Code\Generator\ClassGenerator,
Zend\Code\Generator\PropertyGenerator;

/**
* This class is the front most class for utilizing Zend\Tool\Project
*
* A profile is a hierarchical set of resources that keep track of
* items within a specific project.
*
* @uses \Zend\CodeGenerator\Php\PhpClass
* @uses \Zend\CodeGenerator\Php\PhpFile
* @uses \Zend\CodeGenerator\Php\PhpProperty
* @uses \Zend\Code\Generator\ClassGenerator
* @uses \Zend\Code\Generator\FileGenerator
* @uses \Zend\Code\Generator\PropertyGenerator
* @uses \Zend\Tool\Project\Context\Zf\AbstractClassFile
* @category Zend
* @package Zend_Tool
Expand Down Expand Up @@ -78,23 +80,12 @@ public function getContents()
{
$className = $this->getFullClassName($this->_dbTableName, 'Model\DbTable');

$codeGenFile = new Php\PhpFile(array(
'fileName' => $this->getPath(),
'classes' => array(
new Php\PhpClass(array(
'name' => $className,
'extendedClass' => '\Zend\Db\Table\AbstractTable',
'properties' => array(
new Php\PhpProperty(array(
'name' => '_name',
'visibility' => Php\PhpProperty::VISIBILITY_PROTECTED,
'defaultValue' => $this->_actualTableName
))
),

))
$codeGenFile = new FileGenerator($this->getPath());
$codeGenFile->setClass(new ClassGenerator($className, null, null, '\Zend\Db\Table\AbstractTable', null, array(
new PropertyGenerator('_name', $this->_actualTableName, PropertyGenerator::FLAG_PROTECTED),
)
));

return $codeGenFile->generate();
}

Expand Down
30 changes: 11 additions & 19 deletions library/Zend/Tool/Project/Context/Zf/FormFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,18 @@
* @namespace
*/
namespace Zend\Tool\Project\Context\Zf;
use Zend\Code\Generator\MethodGenerator,
Zend\Code\Generator\ClassGenerator;

/**
* This class is the front most class for utilizing Zend\Tool\Project
*
* A profile is a hierarchical set of resources that keep track of
* items within a specific project.
*
* @uses \Zend\CodeGenerator\Php\PhpClass
* @uses \Zend\CodeGenerator\Php\PhpFile
* @uses \Zend\CodeGenerator\Php\PhpMethod
* @uses \Zend\Code\Generator\ClassGenerator
* @uses \Zend\Code\Generator\FileGenerator
* @uses \Zend\Code\Generator\MethodGenerator
* @uses \Zend\Tool\Project\Context\Zf\AbstractClassFile
* @category Zend
* @package Zend_Tool
Expand Down Expand Up @@ -95,22 +97,12 @@ public function getContents()

$className = $this->getFullClassName($this->_formName, 'Form');

$codeGenFile = new \Zend\CodeGenerator\Php\PhpFile(array(
'fileName' => $this->getPath(),
'classes' => array(
new \Zend\CodeGenerator\Php\PhpClass(array(
'name' => $className,
'extendedClass' => '\Zend\Form\Form',
'methods' => array(
new \Zend\CodeGenerator\Php\PhpMethod(array(
'name' => 'init',
'body' => '/* Form Elements & Other Definitions Here ... */',
))
)

))
)
));
$codeGenFile = new \Zend\Code\Generator\FileGenerator();
$codeGenFile->setFilename($this->getPath());
$codeGenFile->setClass(new \Zend\Code\Generator\ClassGenerator($className, null, null, '\Zend\Form\Form', array(), array(),
new MethodGenerator('init', array(), MethodGenerator::FLAG_PUBLIC, '/* Form Elements & Other Definitions Here ... */')
));

return $codeGenFile->generate();
}
}
Loading

0 comments on commit bf23582

Please sign in to comment.