adding zend project folders into old campcaster.

This commit is contained in:
naomiaro 2010-12-07 14:19:27 -05:00
parent 56abfaf28e
commit 7ef0c18b26
4045 changed files with 1054952 additions and 0 deletions

View file

@ -0,0 +1,147 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_CodeGenerator
* @subpackage PHP
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Abstract.php 20096 2010-01-06 02:05:09Z bkarwin $
*/
/**
* @category Zend
* @package Zend_CodeGenerator
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
abstract class Zend_CodeGenerator_Abstract
{
/**
* @var string
*/
protected $_sourceContent = null;
/**
* @var bool
*/
protected $_isSourceDirty = true;
/**
* __construct()
*
* @param array $options
*/
public function __construct($options = array())
{
$this->_init();
if ($options != null) {
// use Zend_Config objects if provided
if ($options instanceof Zend_Config) {
$options = $options->toArray();
}
// pass arrays to setOptions
if (is_array($options)) {
$this->setOptions($options);
}
}
$this->_prepare();
}
/**
* setConfig()
*
* @param Zend_Config $config
* @return Zend_CodeGenerator_Abstract
*/
public function setConfig(Zend_Config $config)
{
$this->setOptions($config->toArray());
return $this;
}
/**
* setOptions()
*
* @param array $options
* @return Zend_CodeGenerator_Abstract
*/
public function setOptions(Array $options)
{
foreach ($options as $optionName => $optionValue) {
$methodName = 'set' . $optionName;
if (method_exists($this, $methodName)) {
$this->{$methodName}($optionValue);
}
}
return $this;
}
/**
* setSourceContent()
*
* @param string $sourceContent
*/
public function setSourceContent($sourceContent)
{
$this->_sourceContent = $sourceContent;
return;
}
/**
* getSourceContent()
*
* @return string
*/
public function getSourceContent()
{
return $this->_sourceContent;
}
/**
* _init() - this is called before the constuctor
*
*/
protected function _init()
{
}
/**
* _prepare() - this is called at construction completion
*
*/
protected function _prepare()
{
}
/**
* generate() - must be implemented by the child
*
*/
abstract public function generate();
/**
* __toString() - casting to a string will in turn call generate()
*
* @return string
*/
final public function __toString()
{
return $this->generate();
}
}

View file

@ -0,0 +1,35 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_CodeGenerator
* @subpackage PHP
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Exception.php 20096 2010-01-06 02:05:09Z bkarwin $
*/
/**
* @see Zend_Exception
*/
require_once 'Zend/Exception.php';
/**
* @category Zend
* @package Zend_CodeGenerator
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_CodeGenerator_Exception extends Zend_Exception
{}

View file

@ -0,0 +1,97 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_CodeGenerator
* @subpackage PHP
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Abstract.php 20096 2010-01-06 02:05:09Z bkarwin $
*/
/**
* @see Zend_CodeGenerator_Abstract
*/
require_once 'Zend/CodeGenerator/Abstract.php';
/**
* @category Zend
* @package Zend_CodeGenerator
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
abstract class Zend_CodeGenerator_Php_Abstract extends Zend_CodeGenerator_Abstract
{
/**
* Line feed to use in place of EOL
*
*/
const LINE_FEED = "\n";
/**
* @var bool
*/
protected $_isSourceDirty = true;
/**
* @var int|string
*/
protected $_indentation = ' ';
/**
* setSourceDirty()
*
* @param bool $isSourceDirty
* @return Zend_CodeGenerator_Php_Abstract
*/
public function setSourceDirty($isSourceDirty = true)
{
$this->_isSourceDirty = ($isSourceDirty) ? true : false;
return $this;
}
/**
* isSourceDirty()
*
* @return bool
*/
public function isSourceDirty()
{
return $this->_isSourceDirty;
}
/**
* setIndentation()
*
* @param string|int $indentation
* @return Zend_CodeGenerator_Php_Abstract
*/
public function setIndentation($indentation)
{
$this->_indentation = $indentation;
return $this;
}
/**
* getIndentation()
*
* @return string|int
*/
public function getIndentation()
{
return $this->_indentation;
}
}

View file

@ -0,0 +1,73 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_CodeGenerator
* @subpackage PHP
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Body.php 20096 2010-01-06 02:05:09Z bkarwin $
*/
/**
* @see Zend_CodeGenerator_Abstract
*/
require_once 'Zend/CodeGenerator/Abstract.php';
/**
* @category Zend
* @package Zend_CodeGenerator
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_CodeGenerator_Php_Body extends Zend_CodeGenerator_Abstract
{
/**
* @var string
*/
protected $_content = null;
/**
* setContent()
*
* @param string $content
* @return Zend_CodeGenerator_Php_Body
*/
public function setContent($content)
{
$this->_content = $content;
return $this;
}
/**
* getContent()
*
* @return string
*/
public function getContent()
{
return (string) $this->_content;
}
/**
* generate()
*
* @return string
*/
public function generate()
{
return $this->getContent();
}
}

View file

@ -0,0 +1,513 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_CodeGenerator
* @subpackage PHP
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Class.php 20096 2010-01-06 02:05:09Z bkarwin $
*/
/**
* @see Zend_CodeGenerator_Php_Abstract
*/
require_once 'Zend/CodeGenerator/Php/Abstract.php';
/**
* @see Zend_CodeGenerator_Php_Member_Container
*/
require_once 'Zend/CodeGenerator/Php/Member/Container.php';
/**
* @see Zend_CodeGenerator_Php_Method
*/
require_once 'Zend/CodeGenerator/Php/Method.php';
/**
* @see Zend_CodeGenerator_Php_Property
*/
require_once 'Zend/CodeGenerator/Php/Property.php';
/**
* @see Zend_CodeGenerator_Php_Docblock
*/
require_once 'Zend/CodeGenerator/Php/Docblock.php';
/**
* @category Zend
* @package Zend_CodeGenerator
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_CodeGenerator_Php_Class extends Zend_CodeGenerator_Php_Abstract
{
/**
* @var Zend_CodeGenerator_Php_Docblock
*/
protected $_docblock = null;
/**
* @var string
*/
protected $_name = null;
/**
* @var bool
*/
protected $_isAbstract = false;
/**
* @var string
*/
protected $_extendedClass = null;
/**
* @var array Array of string names
*/
protected $_implementedInterfaces = array();
/**
* @var array Array of properties
*/
protected $_properties = null;
/**
* @var array Array of methods
*/
protected $_methods = null;
/**
* fromReflection() - build a Code Generation PHP Object from a Class Reflection
*
* @param Zend_Reflection_Class $reflectionClass
* @return Zend_CodeGenerator_Php_Class
*/
public static function fromReflection(Zend_Reflection_Class $reflectionClass)
{
$class = new self();
$class->setSourceContent($class->getSourceContent());
$class->setSourceDirty(false);
if ($reflectionClass->getDocComment() != '') {
$class->setDocblock(Zend_CodeGenerator_Php_Docblock::fromReflection($reflectionClass->getDocblock()));
}
$class->setAbstract($reflectionClass->isAbstract());
$class->setName($reflectionClass->getName());
if ($parentClass = $reflectionClass->getParentClass()) {
$class->setExtendedClass($parentClass->getName());
$interfaces = array_diff($reflectionClass->getInterfaces(), $parentClass->getInterfaces());
} else {
$interfaces = $reflectionClass->getInterfaces();
}
$interfaceNames = array();
foreach($interfaces AS $interface) {
$interfaceNames[] = $interface->getName();
}
$class->setImplementedInterfaces($interfaceNames);
$properties = array();
foreach ($reflectionClass->getProperties() as $reflectionProperty) {
if ($reflectionProperty->getDeclaringClass()->getName() == $class->getName()) {
$properties[] = Zend_CodeGenerator_Php_Property::fromReflection($reflectionProperty);
}
}
$class->setProperties($properties);
$methods = array();
foreach ($reflectionClass->getMethods() as $reflectionMethod) {
if ($reflectionMethod->getDeclaringClass()->getName() == $class->getName()) {
$methods[] = Zend_CodeGenerator_Php_Method::fromReflection($reflectionMethod);
}
}
$class->setMethods($methods);
return $class;
}
/**
* setDocblock() Set the docblock
*
* @param Zend_CodeGenerator_Php_Docblock|array|string $docblock
* @return Zend_CodeGenerator_Php_File
*/
public function setDocblock($docblock)
{
if (is_string($docblock)) {
$docblock = array('shortDescription' => $docblock);
}
if (is_array($docblock)) {
$docblock = new Zend_CodeGenerator_Php_Docblock($docblock);
} elseif (!$docblock instanceof Zend_CodeGenerator_Php_Docblock) {
require_once 'Zend/CodeGenerator/Php/Exception.php';
throw new Zend_CodeGenerator_Php_Exception('setDocblock() is expecting either a string, array or an instance of Zend_CodeGenerator_Php_Docblock');
}
$this->_docblock = $docblock;
return $this;
}
/**
* getDocblock()
*
* @return Zend_CodeGenerator_Php_Docblock
*/
public function getDocblock()
{
return $this->_docblock;
}
/**
* setName()
*
* @param string $name
* @return Zend_CodeGenerator_Php_Class
*/
public function setName($name)
{
$this->_name = $name;
return $this;
}
/**
* getName()
*
* @return string
*/
public function getName()
{
return $this->_name;
}
/**
* setAbstract()
*
* @param bool $isAbstract
* @return Zend_CodeGenerator_Php_Class
*/
public function setAbstract($isAbstract)
{
$this->_isAbstract = ($isAbstract) ? true : false;
return $this;
}
/**
* isAbstract()
*
* @return bool
*/
public function isAbstract()
{
return $this->_isAbstract;
}
/**
* setExtendedClass()
*
* @param string $extendedClass
* @return Zend_CodeGenerator_Php_Class
*/
public function setExtendedClass($extendedClass)
{
$this->_extendedClass = $extendedClass;
return $this;
}
/**
* getExtendedClass()
*
* @return string
*/
public function getExtendedClass()
{
return $this->_extendedClass;
}
/**
* setImplementedInterfaces()
*
* @param array $implementedInterfaces
* @return Zend_CodeGenerator_Php_Class
*/
public function setImplementedInterfaces(Array $implementedInterfaces)
{
$this->_implementedInterfaces = $implementedInterfaces;
return $this;
}
/**
* getImplementedInterfaces
*
* @return array
*/
public function getImplementedInterfaces()
{
return $this->_implementedInterfaces;
}
/**
* setProperties()
*
* @param array $properties
* @return Zend_CodeGenerator_Php_Class
*/
public function setProperties(Array $properties)
{
foreach ($properties as $property) {
$this->setProperty($property);
}
return $this;
}
/**
* setProperty()
*
* @param array|Zend_CodeGenerator_Php_Property $property
* @return Zend_CodeGenerator_Php_Class
*/
public function setProperty($property)
{
if (is_array($property)) {
$property = new Zend_CodeGenerator_Php_Property($property);
$propertyName = $property->getName();
} elseif ($property instanceof Zend_CodeGenerator_Php_Property) {
$propertyName = $property->getName();
} else {
require_once 'Zend/CodeGenerator/Php/Exception.php';
throw new Zend_CodeGenerator_Php_Exception('setProperty() expects either an array of property options or an instance of Zend_CodeGenerator_Php_Property');
}
if (isset($this->_properties[$propertyName])) {
require_once 'Zend/CodeGenerator/Php/Exception.php';
throw new Zend_CodeGenerator_Php_Exception('A property by name ' . $propertyName . ' already exists in this class.');
}
$this->_properties[$propertyName] = $property;
return $this;
}
/**
* getProperties()
*
* @return array
*/
public function getProperties()
{
return $this->_properties;
}
/**
* getProperty()
*
* @param string $propertyName
* @return Zend_CodeGenerator_Php_Property
*/
public function getProperty($propertyName)
{
foreach ($this->_properties as $property) {
if ($property->getName() == $propertyName) {
return $property;
}
}
return false;
}
/**
* hasProperty()
*
* @param string $propertyName
* @return bool
*/
public function hasProperty($propertyName)
{
return isset($this->_properties[$propertyName]);
}
/**
* setMethods()
*
* @param array $methods
* @return Zend_CodeGenerator_Php_Class
*/
public function setMethods(Array $methods)
{
foreach ($methods as $method) {
$this->setMethod($method);
}
return $this;
}
/**
* setMethod()
*
* @param array|Zend_CodeGenerator_Php_Method $method
* @return Zend_CodeGenerator_Php_Class
*/
public function setMethod($method)
{
if (is_array($method)) {
$method = new Zend_CodeGenerator_Php_Method($method);
$methodName = $method->getName();
} elseif ($method instanceof Zend_CodeGenerator_Php_Method) {
$methodName = $method->getName();
} else {
require_once 'Zend/CodeGenerator/Php/Exception.php';
throw new Zend_CodeGenerator_Php_Exception('setMethod() expects either an array of method options or an instance of Zend_CodeGenerator_Php_Method');
}
if (isset($this->_methods[$methodName])) {
require_once 'Zend/CodeGenerator/Php/Exception.php';
throw new Zend_CodeGenerator_Php_Exception('A method by name ' . $methodName . ' already exists in this class.');
}
$this->_methods[$methodName] = $method;
return $this;
}
/**
* getMethods()
*
* @return array
*/
public function getMethods()
{
return $this->_methods;
}
/**
* getMethod()
*
* @param string $methodName
* @return Zend_CodeGenerator_Php_Method
*/
public function getMethod($methodName)
{
foreach ($this->_methods as $method) {
if ($method->getName() == $methodName) {
return $method;
}
}
return false;
}
/**
* hasMethod()
*
* @param string $methodName
* @return bool
*/
public function hasMethod($methodName)
{
return isset($this->_methods[$methodName]);
}
/**
* isSourceDirty()
*
* @return bool
*/
public function isSourceDirty()
{
if (($docblock = $this->getDocblock()) && $docblock->isSourceDirty()) {
return true;
}
foreach ($this->_properties as $property) {
if ($property->isSourceDirty()) {
return true;
}
}
foreach ($this->_methods as $method) {
if ($method->isSourceDirty()) {
return true;
}
}
return parent::isSourceDirty();
}
/**
* generate()
*
* @return string
*/
public function generate()
{
if (!$this->isSourceDirty()) {
return $this->getSourceContent();
}
$output = '';
if (null !== ($docblock = $this->getDocblock())) {
$docblock->setIndentation('');
$output .= $docblock->generate();
}
if ($this->isAbstract()) {
$output .= 'abstract ';
}
$output .= 'class ' . $this->getName();
if (null !== ($ec = $this->_extendedClass)) {
$output .= ' extends ' . $ec;
}
$implemented = $this->getImplementedInterfaces();
if (!empty($implemented)) {
$output .= ' implements ' . implode(', ', $implemented);
}
$output .= self::LINE_FEED . '{' . self::LINE_FEED . self::LINE_FEED;
$properties = $this->getProperties();
if (!empty($properties)) {
foreach ($properties as $property) {
$output .= $property->generate() . self::LINE_FEED . self::LINE_FEED;
}
}
$methods = $this->getMethods();
if (!empty($methods)) {
foreach ($methods as $method) {
$output .= $method->generate() . self::LINE_FEED;
}
}
$output .= self::LINE_FEED . '}' . self::LINE_FEED;
return $output;
}
/**
* _init() - is called at construction time
*
*/
protected function _init()
{
$this->_properties = new Zend_CodeGenerator_Php_Member_Container(Zend_CodeGenerator_Php_Member_Container::TYPE_PROPERTY);
$this->_methods = new Zend_CodeGenerator_Php_Member_Container(Zend_CodeGenerator_Php_Member_Container::TYPE_METHOD);
}
}

View file

@ -0,0 +1,220 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_CodeGenerator
* @subpackage PHP
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Docblock.php 20096 2010-01-06 02:05:09Z bkarwin $
*/
/**
* @see Zend_CodeGenerator_Php_Abstract
*/
require_once 'Zend/CodeGenerator/Php/Abstract.php';
/**
* @see Zend_CodeGenerator_Php_Docblock_Tag
*/
require_once 'Zend/CodeGenerator/Php/Docblock/Tag.php';
/**
* @category Zend
* @package Zend_CodeGenerator
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_CodeGenerator_Php_Docblock extends Zend_CodeGenerator_Php_Abstract
{
/**
* @var string
*/
protected $_shortDescription = null;
/**
* @var string
*/
protected $_longDescription = null;
/**
* @var array
*/
protected $_tags = array();
/**
* @var string
*/
protected $_indentation = '';
/**
* fromReflection() - Build a docblock generator object from a reflection object
*
* @param Zend_Reflection_Docblock $reflectionDocblock
* @return Zend_CodeGenerator_Php_Docblock
*/
public static function fromReflection(Zend_Reflection_Docblock $reflectionDocblock)
{
$docblock = new self();
$docblock->setSourceContent($reflectionDocblock->getContents());
$docblock->setSourceDirty(false);
$docblock->setShortDescription($reflectionDocblock->getShortDescription());
$docblock->setLongDescription($reflectionDocblock->getLongDescription());
foreach ($reflectionDocblock->getTags() as $tag) {
$docblock->setTag(Zend_CodeGenerator_Php_Docblock_Tag::fromReflection($tag));
}
return $docblock;
}
/**
* setShortDescription()
*
* @param string $shortDescription
* @return Zend_CodeGenerator_Php_Docblock
*/
public function setShortDescription($shortDescription)
{
$this->_shortDescription = $shortDescription;
return $this;
}
/**
* getShortDescription()
*
* @return string
*/
public function getShortDescription()
{
return $this->_shortDescription;
}
/**
* setLongDescription()
*
* @param string $longDescription
* @return Zend_CodeGenerator_Php_Docblock
*/
public function setLongDescription($longDescription)
{
$this->_longDescription = $longDescription;
return $this;
}
/**
* getLongDescription()
*
* @return string
*/
public function getLongDescription()
{
return $this->_longDescription;
}
/**
* setTags()
*
* @param array $tags
* @return Zend_CodeGenerator_Php_Docblock
*/
public function setTags(Array $tags)
{
foreach ($tags as $tag) {
$this->setTag($tag);
}
return $this;
}
/**
* setTag()
*
* @param array|Zend_CodeGenerator_Php_Docblock_Tag $tag
* @return Zend_CodeGenerator_Php_Docblock
*/
public function setTag($tag)
{
if (is_array($tag)) {
$tag = new Zend_CodeGenerator_Php_Docblock_Tag($tag);
} elseif (!$tag instanceof Zend_CodeGenerator_Php_Docblock_Tag) {
require_once 'Zend/CodeGenerator/Php/Exception.php';
throw new Zend_CodeGenerator_Php_Exception(
'setTag() expects either an array of method options or an '
. 'instance of Zend_CodeGenerator_Php_Docblock_Tag'
);
}
$this->_tags[] = $tag;
return $this;
}
/**
* getTags
*
* @return array Array of Zend_CodeGenerator_Php_Docblock_Tag
*/
public function getTags()
{
return $this->_tags;
}
/**
* generate()
*
* @return string
*/
public function generate()
{
if (!$this->isSourceDirty()) {
return $this->_docCommentize($this->getSourceContent());
}
$output = '';
if (null !== ($sd = $this->getShortDescription())) {
$output .= $sd . self::LINE_FEED . self::LINE_FEED;
}
if (null !== ($ld = $this->getLongDescription())) {
$output .= $ld . self::LINE_FEED . self::LINE_FEED;
}
foreach ($this->getTags() as $tag) {
$output .= $tag->generate() . self::LINE_FEED;
}
return $this->_docCommentize(trim($output));
}
/**
* _docCommentize()
*
* @param string $content
* @return string
*/
protected function _docCommentize($content)
{
$indent = $this->getIndentation();
$output = $indent . '/**' . self::LINE_FEED;
$content = wordwrap($content, 80, self::LINE_FEED);
$lines = explode(self::LINE_FEED, $content);
foreach ($lines as $line) {
$output .= $indent . ' * ' . $line . self::LINE_FEED;
}
$output .= $indent . ' */' . self::LINE_FEED;
return $output;
}
}

View file

@ -0,0 +1,184 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_CodeGenerator
* @subpackage PHP
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Tag.php 20096 2010-01-06 02:05:09Z bkarwin $
*/
/**
* @see Zend_CodeGenerator_Abstract
*/
require_once 'Zend/CodeGenerator/Php/Abstract.php';
/**
* @see Zend_CodeGenerator_Php_Docblock_Tag_Param
*/
require_once 'Zend/CodeGenerator/Php/Docblock/Tag/Param.php';
/**
* @see Zend_CodeGenerator_Php_Docblock_Tag_Return
*/
require_once 'Zend/CodeGenerator/Php/Docblock/Tag/Return.php';
/**
* @category Zend
* @package Zend_CodeGenerator
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_CodeGenerator_Php_Docblock_Tag extends Zend_CodeGenerator_Php_Abstract
{
/**
* @var Zend_Loader_PluginLoader
*/
protected static $_pluginLoader = null;
/**
* @var string
*/
protected $_name = null;
/**
* @var string
*/
protected $_description = null;
/**
* fromReflection()
*
* @param Zend_Reflection_Docblock_Tag $reflectionTag
* @return Zend_CodeGenerator_Php_Docblock_Tag
*/
public static function fromReflection(Zend_Reflection_Docblock_Tag $reflectionTag)
{
$tagName = $reflectionTag->getName();
$codeGenDocblockTag = self::factory($tagName);
// transport any properties via accessors and mutators from reflection to codegen object
$reflectionClass = new ReflectionClass($reflectionTag);
foreach ($reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
if (substr($method->getName(), 0, 3) == 'get') {
$propertyName = substr($method->getName(), 3);
if (method_exists($codeGenDocblockTag, 'set' . $propertyName)) {
$codeGenDocblockTag->{'set' . $propertyName}($reflectionTag->{'get' . $propertyName}());
}
}
}
return $codeGenDocblockTag;
}
/**
* setPluginLoader()
*
* @param Zend_Loader_PluginLoader $pluginLoader
*/
public static function setPluginLoader(Zend_Loader_PluginLoader $pluginLoader)
{
self::$_pluginLoader = $pluginLoader;
return;
}
/**
* getPluginLoader()
*
* @return Zend_Loader_PluginLoader
*/
public static function getPluginLoader()
{
if (self::$_pluginLoader == null) {
require_once 'Zend/Loader/PluginLoader.php';
self::setPluginLoader(new Zend_Loader_PluginLoader(array(
'Zend_CodeGenerator_Php_Docblock_Tag' => dirname(__FILE__) . '/Tag/'))
);
}
return self::$_pluginLoader;
}
public static function factory($tagName)
{
$pluginLoader = self::getPluginLoader();
try {
$tagClass = $pluginLoader->load($tagName);
} catch (Zend_Loader_Exception $exception) {
$tagClass = 'Zend_CodeGenerator_Php_Docblock_Tag';
}
$tag = new $tagClass(array('name' => $tagName));
return $tag;
}
/**
* setName()
*
* @param string $name
* @return Zend_CodeGenerator_Php_Docblock_Tag
*/
public function setName($name)
{
$this->_name = ltrim($name, '@');
return $this;
}
/**
* getName()
*
* @return string
*/
public function getName()
{
return $this->_name;
}
/**
* setDescription()
*
* @param string $description
* @return Zend_CodeGenerator_Php_Docblock_Tag
*/
public function setDescription($description)
{
$this->_description = $description;
return $this;
}
/**
* getDescription()
*
* @return string
*/
public function getDescription()
{
return $this->_description;
}
/**
* generate()
*
* @return string
*/
public function generate()
{
return '@' . $this->_name . ' ' . $this->_description;
}
}

View file

@ -0,0 +1,98 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_CodeGenerator
* @subpackage PHP
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: License.php 20096 2010-01-06 02:05:09Z bkarwin $
*/
/**
* @see Zend_CodeGenerator_Php_Docblock_Tag
*/
require_once 'Zend/CodeGenerator/Php/Docblock/Tag.php';
/**
* @category Zend
* @package Zend_CodeGenerator
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_CodeGenerator_Php_Docblock_Tag_License extends Zend_CodeGenerator_Php_Docblock_Tag
{
/**
* @var string
*/
protected $_url = null;
/**
* @var string
*/
protected $_description = null;
/**
* fromReflection()
*
* @param Zend_Reflection_Docblock_Tag $reflectionTagReturn
* @return Zend_CodeGenerator_Php_Docblock_Tag_License
*/
public static function fromReflection(Zend_Reflection_Docblock_Tag $reflectionTagLicense)
{
$returnTag = new self();
$returnTag->setName('license');
$returnTag->setUrl($reflectionTagLicense->getUrl());
$returnTag->setDescription($reflectionTagLicense->getDescription());
return $returnTag;
}
/**
* setUrl()
*
* @param string $url
* @return Zend_CodeGenerator_Php_Docblock_Tag_License
*/
public function setUrl($url)
{
$this->_url = $url;
return $this;
}
/**
* getUrl()
*
* @return string
*/
public function getUrl()
{
return $this->_url;
}
/**
* generate()
*
* @return string
*/
public function generate()
{
$output = '@license ' . $this->_url . ' ' . $this->_description . self::LINE_FEED;
return $output;
}
}

View file

@ -0,0 +1,128 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_CodeGenerator
* @subpackage PHP
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Param.php 20096 2010-01-06 02:05:09Z bkarwin $
*/
/**
* @see Zend_CodeGenerator_Php_Docblock_Tag
*/
require_once 'Zend/CodeGenerator/Php/Docblock/Tag.php';
/**
* @category Zend
* @package Zend_CodeGenerator
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_CodeGenerator_Php_Docblock_Tag_Param extends Zend_CodeGenerator_Php_Docblock_Tag
{
/**
* @var string
*/
protected $_datatype = null;
/**
* @var string
*/
protected $_paramName = null;
/**
* @var string
*/
protected $_description = null;
/**
* fromReflection()
*
* @param Zend_Reflection_Docblock_Tag $reflectionTagParam
* @return Zend_CodeGenerator_Php_Docblock_Tag_Param
*/
public static function fromReflection(Zend_Reflection_Docblock_Tag $reflectionTagParam)
{
$paramTag = new self();
$paramTag->setName('param');
$paramTag->setDatatype($reflectionTagParam->getType()); // @todo rename
$paramTag->setParamName($reflectionTagParam->getVariableName());
$paramTag->setDescription($reflectionTagParam->getDescription());
return $paramTag;
}
/**
* setDatatype()
*
* @param string $datatype
* @return Zend_CodeGenerator_Php_Docblock_Tag_Param
*/
public function setDatatype($datatype)
{
$this->_datatype = $datatype;
return $this;
}
/**
* getDatatype
*
* @return string
*/
public function getDatatype()
{
return $this->_datatype;
}
/**
* setParamName()
*
* @param string $paramName
* @return Zend_CodeGenerator_Php_Docblock_Tag_Param
*/
public function setParamName($paramName)
{
$this->_paramName = $paramName;
return $this;
}
/**
* getParamName()
*
* @return string
*/
public function getParamName()
{
return $this->_paramName;
}
/**
* generate()
*
* @return string
*/
public function generate()
{
$output = '@param '
. (($this->_datatype != null) ? $this->_datatype : 'unknown')
. (($this->_paramName != null) ? ' $' . $this->_paramName : '')
. (($this->_description != null) ? ' ' . $this->_description : '');
return $output;
}
}

View file

@ -0,0 +1,98 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_CodeGenerator
* @subpackage PHP
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Return.php 20096 2010-01-06 02:05:09Z bkarwin $
*/
/**
* @see Zend_CodeGenerator_Php_Docblock_Tag
*/
require_once 'Zend/CodeGenerator/Php/Docblock/Tag.php';
/**
* @category Zend
* @package Zend_CodeGenerator
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_CodeGenerator_Php_Docblock_Tag_Return extends Zend_CodeGenerator_Php_Docblock_Tag
{
/**
* @var string
*/
protected $_datatype = null;
/**
* @var string
*/
protected $_description = null;
/**
* fromReflection()
*
* @param Zend_Reflection_Docblock_Tag $reflectionTagReturn
* @return Zend_CodeGenerator_Php_Docblock_Tag_Return
*/
public static function fromReflection(Zend_Reflection_Docblock_Tag $reflectionTagReturn)
{
$returnTag = new self();
$returnTag->setName('return');
$returnTag->setDatatype($reflectionTagReturn->getType()); // @todo rename
$returnTag->setDescription($reflectionTagReturn->getDescription());
return $returnTag;
}
/**
* setDatatype()
*
* @param string $datatype
* @return Zend_CodeGenerator_Php_Docblock_Tag_Return
*/
public function setDatatype($datatype)
{
$this->_datatype = $datatype;
return $this;
}
/**
* getDatatype()
*
* @return string
*/
public function getDatatype()
{
return $this->_datatype;
}
/**
* generate()
*
* @return string
*/
public function generate()
{
$output = '@return ' . $this->_datatype . ' ' . $this->_description;
return $output;
}
}

View file

@ -0,0 +1,37 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_CodeGenerator
* @subpackage PHP
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Exception.php 20096 2010-01-06 02:05:09Z bkarwin $
*/
/**
* @see Zend_CodeGenerator_Exception
*/
require_once 'Zend/CodeGenerator/Exception.php';
/**
* @category Zend
* @package Zend_CodeGenerator
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_CodeGenerator_Php_Exception extends Zend_CodeGenerator_Exception
{
}

View file

@ -0,0 +1,465 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_CodeGenerator
* @subpackage PHP
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: File.php 20096 2010-01-06 02:05:09Z bkarwin $
*/
/**
* @see Zend_CodeGenerator_Php_Abstract
*/
require_once 'Zend/CodeGenerator/Php/Abstract.php';
/**
* @see Zend_CodeGenerator_Php_Class
*/
require_once 'Zend/CodeGenerator/Php/Class.php';
/**
* @category Zend
* @package Zend_CodeGenerator
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_CodeGenerator_Php_File extends Zend_CodeGenerator_Php_Abstract
{
/**
* @var array Array of Zend_CodeGenerator_Php_File
*/
protected static $_fileCodeGenerators = array();
/**#@+
* @var string
*/
protected static $_markerDocblock = '/* Zend_CodeGenerator_Php_File-DocblockMarker */';
protected static $_markerRequire = '/* Zend_CodeGenerator_Php_File-RequireMarker: {?} */';
protected static $_markerClass = '/* Zend_CodeGenerator_Php_File-ClassMarker: {?} */';
/**#@-*/
/**
* @var string
*/
protected $_filename = null;
/**
* @var Zend_CodeGenerator_Php_Docblock
*/
protected $_docblock = null;
/**
* @var array
*/
protected $_requiredFiles = array();
/**
* @var array
*/
protected $_classes = array();
/**
* @var string
*/
protected $_body = null;
public static function registerFileCodeGenerator(Zend_CodeGenerator_Php_File $fileCodeGenerator, $fileName = null)
{
if ($fileName == null) {
$fileName = $fileCodeGenerator->getFilename();
}
if ($fileName == '') {
require_once 'Zend/CodeGenerator/Php/Exception.php';
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;
}
/**
* fromReflectedFilePath() - use this if you intend on generating code generation objects based on the same file.
* This will keep previous changes to the file in tact during the same PHP process
*
* @param string $filePath
* @param bool $usePreviousCodeGeneratorIfItExists
* @param bool $includeIfNotAlreadyIncluded
* @return Zend_CodeGenerator_Php_File
*/
public static function fromReflectedFileName($filePath, $usePreviousCodeGeneratorIfItExists = true, $includeIfNotAlreadyIncluded = true)
{
$realpath = realpath($filePath);
if ($realpath === false) {
if ( ($realpath = Zend_Reflection_file::findRealpathInIncludePath($filePath)) === false) {
require_once 'Zend/CodeGenerator/Php/Exception.php';
throw new Zend_CodeGenerator_Php_Exception('No file for ' . $realpath . ' was found.');
}
}
if ($usePreviousCodeGeneratorIfItExists && isset(self::$_fileCodeGenerators[$realpath])) {
return self::$_fileCodeGenerators[$realpath];
}
if ($includeIfNotAlreadyIncluded && !in_array($realpath, get_included_files())) {
include $realpath;
}
$codeGenerator = self::fromReflection(($fileReflector = new Zend_Reflection_File($realpath)));
if (!isset(self::$_fileCodeGenerators[$fileReflector->getFileName()])) {
self::$_fileCodeGenerators[$fileReflector->getFileName()] = $codeGenerator;
}
return $codeGenerator;
}
/**
* fromReflection()
*
* @param Zend_Reflection_File $reflectionFile
* @return Zend_CodeGenerator_Php_File
*/
public static function fromReflection(Zend_Reflection_File $reflectionFile)
{
$file = new self();
$file->setSourceContent($reflectionFile->getContents());
$file->setSourceDirty(false);
$body = $reflectionFile->getContents();
// @todo this whole area needs to be reworked with respect to how body lines are processed
foreach ($reflectionFile->getClasses() as $class) {
$file->setClass(Zend_CodeGenerator_Php_Class::fromReflection($class));
$classStartLine = $class->getStartLine(true);
$classEndLine = $class->getEndLine();
$bodyLines = explode("\n", $body);
$bodyReturn = array();
for ($lineNum = 1; $lineNum <= count($bodyLines); $lineNum++) {
if ($lineNum == $classStartLine) {
$bodyReturn[] = str_replace('?', $class->getName(), self::$_markerClass); //'/* Zend_CodeGenerator_Php_File-ClassMarker: {' . $class->getName() . '} */';
$lineNum = $classEndLine;
} else {
$bodyReturn[] = $bodyLines[$lineNum - 1]; // adjust for index -> line conversion
}
}
$body = implode("\n", $bodyReturn);
unset($bodyLines, $bodyReturn, $classStartLine, $classEndLine);
}
if (($reflectionFile->getDocComment() != '')) {
$docblock = $reflectionFile->getDocblock();
$file->setDocblock(Zend_CodeGenerator_Php_Docblock::fromReflection($docblock));
$bodyLines = explode("\n", $body);
$bodyReturn = array();
for ($lineNum = 1; $lineNum <= count($bodyLines); $lineNum++) {
if ($lineNum == $docblock->getStartLine()) {
$bodyReturn[] = str_replace('?', $class->getName(), self::$_markerDocblock); //'/* Zend_CodeGenerator_Php_File-ClassMarker: {' . $class->getName() . '} */';
$lineNum = $docblock->getEndLine();
} else {
$bodyReturn[] = $bodyLines[$lineNum - 1]; // adjust for index -> line conversion
}
}
$body = implode("\n", $bodyReturn);
unset($bodyLines, $bodyReturn, $classStartLine, $classEndLine);
}
$file->setBody($body);
return $file;
}
/**
* setDocblock() Set the docblock
*
* @param Zend_CodeGenerator_Php_Docblock|array|string $docblock
* @return Zend_CodeGenerator_Php_File
*/
public function setDocblock($docblock)
{
if (is_string($docblock)) {
$docblock = array('shortDescription' => $docblock);
}
if (is_array($docblock)) {
$docblock = new Zend_CodeGenerator_Php_Docblock($docblock);
} elseif (!$docblock instanceof Zend_CodeGenerator_Php_Docblock) {
require_once 'Zend/CodeGenerator/Php/Exception.php';
throw new Zend_CodeGenerator_Php_Exception('setDocblock() is expecting either a string, array or an instance of Zend_CodeGenerator_Php_Docblock');
}
$this->_docblock = $docblock;
return $this;
}
/**
* Get docblock
*
* @return Zend_CodeGenerator_Php_Docblock
*/
public function getDocblock()
{
return $this->_docblock;
}
/**
* setRequiredFiles
*
* @param array $requiredFiles
* @return Zend_CodeGenerator_Php_File
*/
public function setRequiredFiles($requiredFiles)
{
$this->_requiredFiles = $requiredFiles;
return $this;
}
/**
* getRequiredFiles()
*
* @return array
*/
public function getRequiredFiles()
{
return $this->_requiredFiles;
}
/**
* setClasses()
*
* @param array $classes
* @return Zend_CodeGenerator_Php_File
*/
public function setClasses(Array $classes)
{
foreach ($classes as $class) {
$this->setClass($class);
}
return $this;
}
/**
* getClass()
*
* @param string $name
* @return Zend_CodeGenerator_Php_Class
*/
public function getClass($name = null)
{
if ($name == null) {
reset($this->_classes);
return current($this->_classes);
}
return $this->_classes[$name];
}
/**
* setClass()
*
* @param Zend_CodeGenerator_Php_Class|array $class
* @return Zend_CodeGenerator_Php_File
*/
public function setClass($class)
{
if (is_array($class)) {
$class = new Zend_CodeGenerator_Php_Class($class);
$className = $class->getName();
} elseif ($class instanceof Zend_CodeGenerator_Php_Class) {
$className = $class->getName();
} else {
require_once 'Zend/CodeGenerator/Php/Exception.php';
throw new Zend_CodeGenerator_Php_Exception('Expecting either an array or an instance of Zend_CodeGenerator_Php_Class');
}
// @todo check for dup here
$this->_classes[$className] = $class;
return $this;
}
/**
* setFilename()
*
* @param string $filename
* @return Zend_CodeGenerator_Php_File
*/
public function setFilename($filename)
{
$this->_filename = $filename;
return $this;
}
/**
* getFilename()
*
* @return string
*/
public function getFilename()
{
return $this->_filename;
}
/**
* getClasses()
*
* @return array Array of Zend_CodeGenerator_Php_Class
*/
public function getClasses()
{
return $this->_classes;
}
/**
* setBody()
*
* @param string $body
* @return Zend_CodeGenerator_Php_File
*/
public function setBody($body)
{
$this->_body = $body;
return $this;
}
/**
* getBody()
*
* @return string
*/
public function getBody()
{
return $this->_body;
}
/**
* isSourceDirty()
*
* @return bool
*/
public function isSourceDirty()
{
if (($docblock = $this->getDocblock()) && $docblock->isSourceDirty()) {
return true;
}
foreach ($this->_classes as $class) {
if ($class->isSourceDirty()) {
return true;
}
}
return parent::isSourceDirty();
}
/**
* generate()
*
* @return string
*/
public function generate()
{
if ($this->isSourceDirty() === false) {
return $this->_sourceContent;
}
$output = '';
// start with the body (if there), or open tag
if (preg_match('#(?:\s*)<\?php#', $this->getBody()) == false) {
$output = '<?php' . self::LINE_FEED;
}
// if there are markers, put the body into the output
$body = $this->getBody();
if (preg_match('#/\* Zend_CodeGenerator_Php_File-(.*?)Marker:#', $body)) {
$output .= $body;
$body = '';
}
// Add file docblock, if any
if (null !== ($docblock = $this->getDocblock())) {
$docblock->setIndentation('');
$regex = preg_quote(self::$_markerDocblock, '#');
if (preg_match('#'.$regex.'#', $output)) {
$output = preg_replace('#'.$regex.'#', $docblock->generate(), $output, 1);
} else {
$output .= $docblock->generate() . self::LINE_FEED;
}
}
// newline
$output .= self::LINE_FEED;
// process required files
// @todo marker replacement for required files
$requiredFiles = $this->getRequiredFiles();
if (!empty($requiredFiles)) {
foreach ($requiredFiles as $requiredFile) {
$output .= 'require_once \'' . $requiredFile . '\';' . self::LINE_FEED;
}
$output .= self::LINE_FEED;
}
// process classes
$classes = $this->getClasses();
if (!empty($classes)) {
foreach ($classes as $class) {
$regex = str_replace('?', $class->getName(), self::$_markerClass);
$regex = preg_quote($regex, '#');
if (preg_match('#'.$regex.'#', $output)) {
$output = preg_replace('#'.$regex.'#', $class->generate(), $output, 1);
} else {
$output .= $class->generate() . self::LINE_FEED;
}
}
}
if (!empty($body)) {
// add an extra space betwee clsses and
if (!empty($classes)) {
$output .= self::LINE_FEED;
}
$output .= $body;
}
return $output;
}
public function write()
{
if ($this->_filename == '' || !is_writable(dirname($this->_filename))) {
require_once 'Zend/CodeGenerator/Php/Exception.php';
throw new Zend_CodeGenerator_Php_Exception('This code generator object is not writable.');
}
file_put_contents($this->_filename, $this->generate());
return $this;
}
}

View file

@ -0,0 +1,222 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_CodeGenerator
* @subpackage PHP
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Abstract.php 20096 2010-01-06 02:05:09Z bkarwin $
*/
/**
* @see Zend_CodeGenerator_Php_Abstract
*/
require_once 'Zend/CodeGenerator/Php/Abstract.php';
/**
* @see Zend_CodeGenerator_Php_Abstract
*/
require_once 'Zend/CodeGenerator/Php/Docblock.php';
/**
* @category Zend
* @package Zend_CodeGenerator
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
abstract class Zend_CodeGenerator_Php_Member_Abstract extends Zend_CodeGenerator_Php_Abstract
{
/**#@+
* @param const string
*/
const VISIBILITY_PUBLIC = 'public';
const VISIBILITY_PROTECTED = 'protected';
const VISIBILITY_PRIVATE = 'private';
/**#@-*/
/**
* @var Zend_CodeGenerator_Php_Docblock
*/
protected $_docblock = null;
/**
* @var bool
*/
protected $_isAbstract = false;
/**
* @var bool
*/
protected $_isFinal = false;
/**
* @var bool
*/
protected $_isStatic = false;
/**
* @var const
*/
protected $_visibility = self::VISIBILITY_PUBLIC;
/**
* @var string
*/
protected $_name = null;
/**
* setDocblock() Set the docblock
*
* @param Zend_CodeGenerator_Php_Docblock|array|string $docblock
* @return Zend_CodeGenerator_Php_File
*/
public function setDocblock($docblock)
{
if (is_string($docblock)) {
$docblock = array('shortDescription' => $docblock);
}
if (is_array($docblock)) {
$docblock = new Zend_CodeGenerator_Php_Docblock($docblock);
} elseif (!$docblock instanceof Zend_CodeGenerator_Php_Docblock) {
require_once 'Zend/CodeGenerator/Php/Exception.php';
throw new Zend_CodeGenerator_Php_Exception('setDocblock() is expecting either a string, array or an instance of Zend_CodeGenerator_Php_Docblock');
}
$this->_docblock = $docblock;
return $this;
}
/**
* getDocblock()
*
* @return Zend_CodeGenerator_Php_Docblock
*/
public function getDocblock()
{
return $this->_docblock;
}
/**
* setAbstract()
*
* @param bool $isAbstract
* @return Zend_CodeGenerator_Php_Member_Abstract
*/
public function setAbstract($isAbstract)
{
$this->_isAbstract = ($isAbstract) ? true : false;
return $this;
}
/**
* isAbstract()
*
* @return bool
*/
public function isAbstract()
{
return $this->_isAbstract;
}
/**
* setFinal()
*
* @param bool $isFinal
* @return Zend_CodeGenerator_Php_Member_Abstract
*/
public function setFinal($isFinal)
{
$this->_isFinal = ($isFinal) ? true : false;
return $this;
}
/**
* isFinal()
*
* @return bool
*/
public function isFinal()
{
return $this->_isFinal;
}
/**
* setStatic()
*
* @param bool $isStatic
* @return Zend_CodeGenerator_Php_Member_Abstract
*/
public function setStatic($isStatic)
{
$this->_isStatic = ($isStatic) ? true : false;
return $this;
}
/**
* isStatic()
*
* @return bool
*/
public function isStatic()
{
return $this->_isStatic;
}
/**
* setVisitibility()
*
* @param const $visibility
* @return Zend_CodeGenerator_Php_Member_Abstract
*/
public function setVisibility($visibility)
{
$this->_visibility = $visibility;
return $this;
}
/**
* getVisibility()
*
* @return const
*/
public function getVisibility()
{
return $this->_visibility;
}
/**
* setName()
*
* @param string $name
* @return Zend_CodeGenerator_Php_Member_Abstract
*/
public function setName($name)
{
$this->_name = $name;
return $this;
}
/**
* getName()
*
* @return string
*/
public function getName()
{
return $this->_name;
}
}

View file

@ -0,0 +1,55 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_CodeGenerator
* @subpackage PHP
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Container.php 20096 2010-01-06 02:05:09Z bkarwin $
*/
/**
* @category Zend
* @package Zend_CodeGenerator
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_CodeGenerator_Php_Member_Container extends ArrayObject
{
/**#@+
* @param const string
*/
const TYPE_PROPERTY = 'property';
const TYPE_METHOD = 'method';
/**#@-*/
/**
* @var const|string
*/
protected $_type = self::TYPE_PROPERTY;
/**
* __construct()
*
* @param const|string $type
*/
public function __construct($type = self::TYPE_PROPERTY)
{
$this->_type = $type;
parent::__construct(array(), self::ARRAY_AS_PROPS);
}
}

View file

@ -0,0 +1,234 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_CodeGenerator
* @subpackage PHP
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Method.php 20096 2010-01-06 02:05:09Z bkarwin $
*/
/**
* @see Zend_CodeGenerator_Php_Member_Abstract
*/
require_once 'Zend/CodeGenerator/Php/Member/Abstract.php';
/**
* @see Zend_CodeGenerator_Php_Docblock
*/
require_once 'Zend/CodeGenerator/Php/Docblock.php';
/**
* @see Zend_CodeGenerator_Php_Parameter
*/
require_once 'Zend/CodeGenerator/Php/Parameter.php';
/**
* @category Zend
* @package Zend_CodeGenerator
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_CodeGenerator_Php_Method extends Zend_CodeGenerator_Php_Member_Abstract
{
/**
* @var Zend_CodeGenerator_Php_Docblock
*/
protected $_docblock = null;
/**
* @var bool
*/
protected $_isFinal = false;
/**
* @var array
*/
protected $_parameters = array();
/**
* @var string
*/
protected $_body = null;
/**
* fromReflection()
*
* @param Zend_Reflection_Method $reflectionMethod
* @return Zend_CodeGenerator_Php_Method
*/
public static function fromReflection(Zend_Reflection_Method $reflectionMethod)
{
$method = new self();
$method->setSourceContent($reflectionMethod->getContents(false));
$method->setSourceDirty(false);
if ($reflectionMethod->getDocComment() != '') {
$method->setDocblock(Zend_CodeGenerator_Php_Docblock::fromReflection($reflectionMethod->getDocblock()));
}
$method->setFinal($reflectionMethod->isFinal());
if ($reflectionMethod->isPrivate()) {
$method->setVisibility(self::VISIBILITY_PRIVATE);
} elseif ($reflectionMethod->isProtected()) {
$method->setVisibility(self::VISIBILITY_PROTECTED);
} else {
$method->setVisibility(self::VISIBILITY_PUBLIC);
}
$method->setStatic($reflectionMethod->isStatic());
$method->setName($reflectionMethod->getName());
foreach ($reflectionMethod->getParameters() as $reflectionParameter) {
$method->setParameter(Zend_CodeGenerator_Php_Parameter::fromReflection($reflectionParameter));
}
$method->setBody($reflectionMethod->getBody());
return $method;
}
/**
* setFinal()
*
* @param bool $isFinal
*/
public function setFinal($isFinal)
{
$this->_isFinal = ($isFinal) ? true : false;
}
/**
* setParameters()
*
* @param array $parameters
* @return Zend_CodeGenerator_Php_Method
*/
public function setParameters(Array $parameters)
{
foreach ($parameters as $parameter) {
$this->setParameter($parameter);
}
return $this;
}
/**
* setParameter()
*
* @param Zend_CodeGenerator_Php_Parameter|array $parameter
* @return Zend_CodeGenerator_Php_Method
*/
public function setParameter($parameter)
{
if (is_array($parameter)) {
$parameter = new Zend_CodeGenerator_Php_Parameter($parameter);
$parameterName = $parameter->getName();
} elseif ($parameter instanceof Zend_CodeGenerator_Php_Parameter) {
$parameterName = $parameter->getName();
} else {
require_once 'Zend/CodeGenerator/Php/Exception.php';
throw new Zend_CodeGenerator_Php_Exception('setParameter() expects either an array of method options or an instance of Zend_CodeGenerator_Php_Parameter');
}
$this->_parameters[$parameterName] = $parameter;
return $this;
}
/**
* getParameters()
*
* @return array Array of Zend_CodeGenerator_Php_Parameter
*/
public function getParameters()
{
return $this->_parameters;
}
/**
* setBody()
*
* @param string $body
* @return Zend_CodeGenerator_Php_Method
*/
public function setBody($body)
{
$this->_body = $body;
return $this;
}
/**
* getBody()
*
* @return string
*/
public function getBody()
{
return $this->_body;
}
/**
* generate()
*
* @return string
*/
public function generate()
{
$output = '';
$indent = $this->getIndentation();
if (($docblock = $this->getDocblock()) !== null) {
$docblock->setIndentation($indent);
$output .= $docblock->generate();
}
$output .= $indent;
if ($this->isAbstract()) {
$output .= 'abstract ';
} else {
$output .= (($this->isFinal()) ? 'final ' : '');
}
$output .= $this->getVisibility()
. (($this->isStatic()) ? ' static' : '')
. ' function ' . $this->getName() . '(';
$parameters = $this->getParameters();
if (!empty($parameters)) {
foreach ($parameters as $parameter) {
$parameterOuput[] = $parameter->generate();
}
$output .= implode(', ', $parameterOuput);
}
$output .= ')' . self::LINE_FEED . $indent . '{' . self::LINE_FEED;
if ($this->_body) {
$output .= ' '
. str_replace(self::LINE_FEED, self::LINE_FEED . $indent . $indent, trim($this->_body))
. self::LINE_FEED;
}
$output .= $indent . '}' . self::LINE_FEED;
return $output;
}
}

View file

@ -0,0 +1,250 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_CodeGenerator
* @subpackage PHP
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Parameter.php 20096 2010-01-06 02:05:09Z bkarwin $
*/
/**
* @see Zend_CodeGenerator_Php_Abstract
*/
require_once 'Zend/CodeGenerator/Php/Abstract.php';
/**
* @see Zend_CodeGenerator_Php_ParameterDefaultValue
*/
require_once 'Zend/CodeGenerator/Php/Parameter/DefaultValue.php';
/**
* @category Zend
* @package Zend_CodeGenerator
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_CodeGenerator_Php_Parameter extends Zend_CodeGenerator_Php_Abstract
{
/**
* @var string
*/
protected $_type = null;
/**
* @var string
*/
protected $_name = null;
/**
* @var string
*/
protected $_defaultValue = null;
/**
* @var int
*/
protected $_position = null;
/**
* @var bool
*/
protected $_passedByReference = false;
/**
* fromReflection()
*
* @param Zend_Reflection_Parameter $reflectionParameter
* @return Zend_CodeGenerator_Php_Parameter
*/
public static function fromReflection(Zend_Reflection_Parameter $reflectionParameter)
{
$param = new Zend_CodeGenerator_Php_Parameter();
$param->setName($reflectionParameter->getName());
if($reflectionParameter->isArray()) {
$param->setType('array');
} else {
$typeClass = $reflectionParameter->getClass();
if($typeClass !== null) {
$param->setType($typeClass->getName());
}
}
$param->setPosition($reflectionParameter->getPosition());
if($reflectionParameter->isOptional()) {
$param->setDefaultValue($reflectionParameter->getDefaultValue());
}
$param->setPassedByReference($reflectionParameter->isPassedByReference());
return $param;
}
/**
* setType()
*
* @param string $type
* @return Zend_CodeGenerator_Php_Parameter
*/
public function setType($type)
{
$this->_type = $type;
return $this;
}
/**
* getType()
*
* @return string
*/
public function getType()
{
return $this->_type;
}
/**
* setName()
*
* @param string $name
* @return Zend_CodeGenerator_Php_Parameter
*/
public function setName($name)
{
$this->_name = $name;
return $this;
}
/**
* getName()
*
* @return string
*/
public function getName()
{
return $this->_name;
}
/**
* Set the default value of the parameter.
*
* Certain variables are difficult to expres
*
* @param null|bool|string|int|float|Zend_CodeGenerator_Php_Parameter_DefaultValue $defaultValue
* @return Zend_CodeGenerator_Php_Parameter
*/
public function setDefaultValue($defaultValue)
{
if($defaultValue === null) {
$this->_defaultValue = new Zend_CodeGenerator_Php_Parameter_DefaultValue("null");
} else if(is_array($defaultValue)) {
$defaultValue = str_replace(array("\r", "\n"), "", var_export($defaultValue, true));
$this->_defaultValue = new Zend_CodeGenerator_Php_Parameter_DefaultValue($defaultValue);
} else if(is_bool($defaultValue)) {
if($defaultValue == true) {
$this->_defaultValue = new Zend_CodeGenerator_Php_Parameter_DefaultValue("true");
} else {
$this->_defaultValue = new Zend_CodeGenerator_Php_Parameter_DefaultValue("false");
}
} else {
$this->_defaultValue = $defaultValue;
}
return $this;
}
/**
* getDefaultValue()
*
* @return string
*/
public function getDefaultValue()
{
return $this->_defaultValue;
}
/**
* setPosition()
*
* @param int $position
* @return Zend_CodeGenerator_Php_Parameter
*/
public function setPosition($position)
{
$this->_position = $position;
return $this;
}
/**
* getPosition()
*
* @return int
*/
public function getPosition()
{
return $this->_position;
}
/**
* @return bool
*/
public function getPassedByReference()
{
return $this->_passedByReference;
}
/**
* @param bool $passedByReference
* @return Zend_CodeGenerator_Php_Parameter
*/
public function setPassedByReference($passedByReference)
{
$this->_passedByReference = $passedByReference;
return $this;
}
/**
* generate()
*
* @return string
*/
public function generate()
{
$output = '';
if ($this->_type) {
$output .= $this->_type . ' ';
}
if($this->_passedByReference === true) {
$output .= '&';
}
$output .= '$' . $this->_name;
if ($this->_defaultValue !== null) {
$output .= ' = ';
if (is_string($this->_defaultValue)) {
$output .= '\'' . $this->_defaultValue . '\'';
} else if($this->_defaultValue instanceof Zend_CodeGenerator_Php_ParameterDefaultValue) {
$output .= (string)$this->_defaultValue;
} else {
$output .= $this->_defaultValue;
}
}
return $output;
}
}

View file

@ -0,0 +1,60 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_CodeGenerator
* @subpackage Php
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: DefaultValue.php 20096 2010-01-06 02:05:09Z bkarwin $
*/
/**
* A value-holder object for non-expressable parameter default values, such as null, booleans and empty array()
*
* @category Zend
* @package Zend_CodeGenerator
* @subpackage Php
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_CodeGenerator_Php_Parameter_DefaultValue
{
/**
* @var string
*/
protected $_defaultValue = null;
/**
*
* @param string $defaultValue
* @throws Zend_CodeGenerator_Php_Exception
*/
public function __construct($defaultValue)
{
if(!is_string($defaultValue)) {
require_once "Zend/CodeGenerator/Php/Exception.php";
throw new Zend_CodeGenerator_Php_Exception(
"Can only set a string as default value representation, ".
"but ".gettype($defaultValue)." was given."
);
}
$this->_defaultValue = $defaultValue;
}
public function __toString()
{
return $this->_defaultValue;
}
}

View file

@ -0,0 +1,179 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_CodeGenerator
* @subpackage PHP
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Property.php 20096 2010-01-06 02:05:09Z bkarwin $
*/
/**
* @see Zend_CodeGenerator_Php_Member_Abstract
*/
require_once 'Zend/CodeGenerator/Php/Member/Abstract.php';
/**
* @see Zend_CodeGenerator_Php_Property_DefaultValue
*/
require_once 'Zend/CodeGenerator/Php/Property/DefaultValue.php';
/**
* @category Zend
* @package Zend_CodeGenerator
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_CodeGenerator_Php_Property extends Zend_CodeGenerator_Php_Member_Abstract
{
/**
* @var bool
*/
protected $_isConst = null;
/**
* @var string
*/
protected $_defaultValue = null;
/**
* fromReflection()
*
* @param Zend_Reflection_Property $reflectionProperty
* @return Zend_CodeGenerator_Php_Property
*/
public static function fromReflection(Zend_Reflection_Property $reflectionProperty)
{
$property = new self();
$property->setName($reflectionProperty->getName());
$allDefaultProperties = $reflectionProperty->getDeclaringClass()->getDefaultProperties();
$property->setDefaultValue($allDefaultProperties[$reflectionProperty->getName()]);
if ($reflectionProperty->getDocComment() != '') {
$property->setDocblock(Zend_CodeGenerator_Php_Docblock::fromReflection($reflectionProperty->getDocComment()));
}
if ($reflectionProperty->isStatic()) {
$property->setStatic(true);
}
if ($reflectionProperty->isPrivate()) {
$property->setVisibility(self::VISIBILITY_PRIVATE);
} elseif ($reflectionProperty->isProtected()) {
$property->setVisibility(self::VISIBILITY_PROTECTED);
} else {
$property->setVisibility(self::VISIBILITY_PUBLIC);
}
$property->setSourceDirty(false);
return $property;
}
/**
* setConst()
*
* @param bool $const
* @return Zend_CodeGenerator_Php_Property
*/
public function setConst($const)
{
$this->_isConst = $const;
return $this;
}
/**
* isConst()
*
* @return bool
*/
public function isConst()
{
return ($this->_isConst) ? true : false;
}
/**
* setDefaultValue()
*
* @param Zend_CodeGenerator_Php_Property_DefaultValue|string|array $defaultValue
* @return Zend_CodeGenerator_Php_Property
*/
public function setDefaultValue($defaultValue)
{
// if it looks like
if (is_array($defaultValue)
&& array_key_exists('value', $defaultValue)
&& array_key_exists('type', $defaultValue)) {
$defaultValue = new Zend_CodeGenerator_Php_Property_DefaultValue($defaultValue);
}
if (!($defaultValue instanceof Zend_CodeGenerator_Php_Property_DefaultValue)) {
$defaultValue = new Zend_CodeGenerator_Php_Property_DefaultValue(array('value' => $defaultValue));
}
$this->_defaultValue = $defaultValue;
return $this;
}
/**
* getDefaultValue()
*
* @return Zend_CodeGenerator_Php_Property_DefaultValue
*/
public function getDefaultValue()
{
return $this->_defaultValue;
}
/**
* generate()
*
* @return string
*/
public function generate()
{
$name = $this->getName();
$defaultValue = $this->getDefaultValue();
$output = '';
if (($docblock = $this->getDocblock()) !== null) {
$docblock->setIndentation(' ');
$output .= $docblock->generate();
}
if ($this->isConst()) {
if ($defaultValue != null && !$defaultValue->isValidConstantType()) {
require_once 'Zend/CodeGenerator/Php/Exception.php';
throw new Zend_CodeGenerator_Php_Exception('The property ' . $this->_name . ' is said to be '
. 'constant but does not have a valid constant value.');
}
$output .= $this->_indentation . 'const ' . $name . ' = '
. (($defaultValue !== null) ? $defaultValue->generate() : 'null;');
} else {
$output .= $this->_indentation
. $this->getVisibility()
. (($this->isStatic()) ? ' static' : '')
. ' $' . $name . ' = '
. (($defaultValue !== null) ? $defaultValue->generate() : 'null;');
}
return $output;
}
}

View file

@ -0,0 +1,323 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_CodeGenerator
* @subpackage PHP
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: DefaultValue.php 20096 2010-01-06 02:05:09Z bkarwin $
*/
/**
* @see Zend_CodeGenerator_Php_Abstract
*/
require_once 'Zend/CodeGenerator/Php/Abstract.php';
/**
* @category Zend
* @package Zend_CodeGenerator
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_CodeGenerator_Php_Property_DefaultValue extends Zend_CodeGenerator_Php_Abstract
{
/**#@+
* Constant values
*/
const TYPE_AUTO = 'auto';
const TYPE_BOOLEAN = 'boolean';
const TYPE_BOOL = 'bool';
const TYPE_NUMBER = 'number';
const TYPE_INTEGER = 'integer';
const TYPE_INT = 'int';
const TYPE_FLOAT = 'float';
const TYPE_DOUBLE = 'double';
const TYPE_STRING = 'string';
const TYPE_ARRAY = 'array';
const TYPE_CONSTANT = 'constant';
const TYPE_NULL = 'null';
const TYPE_OTHER = 'other';
/**#@-*/
/**
* @var array of reflected constants
*/
protected static $_constants = array();
/**
* @var mixed
*/
protected $_value = null;
/**
* @var string
*/
protected $_type = self::TYPE_AUTO;
/**
* @var int
*/
protected $_arrayDepth = 1;
/**
* _init()
*
* This method will prepare the constant array for this class
*/
protected function _init()
{
if(count(self::$_constants) == 0) {
$reflect = new ReflectionClass(get_class($this));
self::$_constants = $reflect->getConstants();
unset($reflect);
}
}
/**
* isValidConstantType()
*
* @return bool
*/
public function isValidConstantType()
{
if ($this->_type == self::TYPE_AUTO) {
$type = $this->_getAutoDeterminedType($this->_value);
}
// valid types for constants
$scalarTypes = array(
self::TYPE_BOOLEAN,
self::TYPE_BOOL,
self::TYPE_NUMBER,
self::TYPE_INTEGER,
self::TYPE_INT,
self::TYPE_FLOAT,
self::TYPE_DOUBLE,
self::TYPE_STRING,
self::TYPE_CONSTANT,
self::TYPE_NULL
);
return in_array($type, $scalarTypes);
}
/**
* setValue()
*
* @param mixed $value
* @return Zend_CodeGenerator_Php_Property_DefaultValue
*/
public function setValue($value)
{
$this->_value = $value;
return $this;
}
/**
* getValue()
*
* @return mixed
*/
public function getValue()
{
return $this->_value;
}
/**
* setType()
*
* @param string $type
* @return Zend_CodeGenerator_Php_Property_DefaultValue
*/
public function setType($type)
{
$this->_type = $type;
return $this;
}
/**
* getType()
*
* @return string
*/
public function getType()
{
return $this->_type;
}
/**
* setArrayDepth()
*
* @param int $arrayDepth
* @return Zend_CodeGenerator_Php_Property_DefaultValue
*/
public function setArrayDepth($arrayDepth)
{
$this->_arrayDepth = $arrayDepth;
return $this;
}
/**
* getArrayDepth()
*
* @return int
*/
public function getArrayDepth()
{
return $this->_arrayDepth;
}
/**
* _getValidatedType()
*
* @param string $type
* @return string
*/
protected function _getValidatedType($type)
{
if (($constName = array_search($type, self::$_constants)) !== false) {
return $type;
}
return self::TYPE_AUTO;
}
/**
* _getAutoDeterminedType()
*
* @param mixed $value
* @return string
*/
public function _getAutoDeterminedType($value)
{
switch (gettype($value)) {
case 'boolean':
return self::TYPE_BOOLEAN;
case 'integer':
return self::TYPE_INT;
case 'string':
return self::TYPE_STRING;
case 'double':
case 'float':
case 'integer':
return self::TYPE_NUMBER;
case 'array':
return self::TYPE_ARRAY;
case 'NULL':
return self::TYPE_NULL;
case 'object':
case 'resource':
case 'unknown type':
default:
return self::TYPE_OTHER;
}
}
/**
* generate()
*
* @return string
*/
public function generate()
{
$type = $this->_type;
if ($type != self::TYPE_AUTO) {
$type = $this->_getValidatedType($type);
}
$value = $this->_value;
if ($type == self::TYPE_AUTO) {
$type = $this->_getAutoDeterminedType($value);
if ($type == self::TYPE_ARRAY) {
$rii = new RecursiveIteratorIterator(
$it = new RecursiveArrayIterator($value),
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($rii as $curKey => $curValue) {
if (!$curValue instanceof Zend_CodeGenerator_Php_Property_DefaultValue) {
$curValue = new self(array('value' => $curValue));
$rii->getSubIterator()->offsetSet($curKey, $curValue);
}
$curValue->setArrayDepth($rii->getDepth());
}
$value = $rii->getSubIterator()->getArrayCopy();
}
}
$output = '';
switch ($type) {
case self::TYPE_BOOLEAN:
case self::TYPE_BOOL:
$output .= ( $value ? 'true' : 'false' );
break;
case self::TYPE_STRING:
$output .= "'" . addcslashes($value, "'") . "'";
break;
case self::TYPE_NULL:
$output .= 'null';
break;
case self::TYPE_NUMBER:
case self::TYPE_INTEGER:
case self::TYPE_INT:
case self::TYPE_FLOAT:
case self::TYPE_DOUBLE:
case self::TYPE_CONSTANT:
$output .= $value;
break;
case self::TYPE_ARRAY:
$output .= 'array(';
$curArrayMultiblock = false;
if (count($value) > 1) {
$curArrayMultiblock = true;
$output .= PHP_EOL . str_repeat($this->_indentation, $this->_arrayDepth+1);
}
$outputParts = array();
$noKeyIndex = 0;
foreach ($value as $n => $v) {
$v->setArrayDepth($this->_arrayDepth + 1);
$partV = $v->generate();
$partV = substr($partV, 0, strlen($partV)-1);
if ($n === $noKeyIndex) {
$outputParts[] = $partV;
$noKeyIndex++;
} else {
$outputParts[] = (is_int($n) ? $n : "'" . addcslashes($n, "'") . "'") . ' => ' . $partV;
}
}
$output .= implode(',' . PHP_EOL . str_repeat($this->_indentation, $this->_arrayDepth+1), $outputParts);
if ($curArrayMultiblock == true) {
$output .= PHP_EOL . str_repeat($this->_indentation, $this->_arrayDepth+1);
}
$output .= ')';
break;
case self::TYPE_OTHER:
default:
require_once "Zend/CodeGenerator/Php/Exception.php";
throw new Zend_CodeGenerator_Php_Exception(
"Type '".get_class($value)."' is unknown or cannot be used as property default value."
);
}
$output .= ';';
return $output;
}
}