Initial commit with Symfony 2.1+Vendors
Signed-off-by: Gergely POLONKAI (W00d5t0ck) <polesz@w00d5t0ck.info>
This commit is contained in:
95
vendor/jms/cg/src/CG/Generator/AbstractPhpMember.php
vendored
Normal file
95
vendor/jms/cg/src/CG/Generator/AbstractPhpMember.php
vendored
Normal file
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
namespace CG\Generator;
|
||||
|
||||
/**
|
||||
* Abstract PHP member class.
|
||||
*
|
||||
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*/
|
||||
abstract class AbstractPhpMember
|
||||
{
|
||||
const VISIBILITY_PRIVATE = 'private';
|
||||
const VISIBILITY_PROTECTED = 'protected';
|
||||
const VISIBILITY_PUBLIC = 'public';
|
||||
|
||||
private $static = false;
|
||||
private $visibility = self::VISIBILITY_PUBLIC;
|
||||
private $name;
|
||||
private $docblock;
|
||||
|
||||
public function __construct($name = null)
|
||||
{
|
||||
$this->setName($name);
|
||||
}
|
||||
|
||||
public function setName($name)
|
||||
{
|
||||
$this->name = $name;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setVisibility($visibility)
|
||||
{
|
||||
if ($visibility !== self::VISIBILITY_PRIVATE
|
||||
&& $visibility !== self::VISIBILITY_PROTECTED
|
||||
&& $visibility !== self::VISIBILITY_PUBLIC) {
|
||||
throw new \InvalidArgumentException(sprintf('The visibility "%s" does not exist.', $visibility));
|
||||
}
|
||||
|
||||
$this->visibility = $visibility;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setStatic($bool)
|
||||
{
|
||||
$this->static = (Boolean) $bool;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setDocblock($doc)
|
||||
{
|
||||
$this->docblock = $doc;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function isStatic()
|
||||
{
|
||||
return $this->static;
|
||||
}
|
||||
|
||||
public function getVisibility()
|
||||
{
|
||||
return $this->visibility;
|
||||
}
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function getDocblock()
|
||||
{
|
||||
return $this->docblock;
|
||||
}
|
||||
}
|
172
vendor/jms/cg/src/CG/Generator/DefaultNavigator.php
vendored
Normal file
172
vendor/jms/cg/src/CG/Generator/DefaultNavigator.php
vendored
Normal file
@@ -0,0 +1,172 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
namespace CG\Generator;
|
||||
|
||||
/**
|
||||
* The default navigator.
|
||||
*
|
||||
* This class is responsible for the default traversal algorithm of the different
|
||||
* code elements.
|
||||
*
|
||||
* Unlike other visitor pattern implementations, this allows to separate the
|
||||
* traversal logic from the objects that are traversed.
|
||||
*
|
||||
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*/
|
||||
class DefaultNavigator
|
||||
{
|
||||
private $constantSortFunc;
|
||||
private $propertySortFunc;
|
||||
private $methodSortFunc;
|
||||
|
||||
/**
|
||||
* Sets a custom constant sorting function.
|
||||
*
|
||||
* @param \Closure $func
|
||||
*/
|
||||
public function setConstantSortFunc(\Closure $func = null)
|
||||
{
|
||||
$this->constantSortFunc = $func;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a custom property sorting function.
|
||||
*
|
||||
* @param \Closure $func
|
||||
*/
|
||||
public function setPropertySortFunc(\Closure $func = null)
|
||||
{
|
||||
$this->propertySortFunc = $func;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a custom method sorting function.
|
||||
*
|
||||
* @param \Closure $func
|
||||
*/
|
||||
public function setMethodSortFunc(\Closure $func = null)
|
||||
{
|
||||
$this->methodSortFunc = $func;
|
||||
}
|
||||
|
||||
public function accept(DefaultVisitorInterface $visitor, PhpClass $class)
|
||||
{
|
||||
$visitor->startVisitingClass($class);
|
||||
|
||||
$constants = $class->getConstants();
|
||||
if (!empty($constants)) {
|
||||
uksort($constants, $this->getConstantSortFunc());
|
||||
|
||||
$visitor->startVisitingConstants();
|
||||
foreach ($constants as $name => $value) {
|
||||
$visitor->visitConstant($name, $value);
|
||||
}
|
||||
$visitor->endVisitingConstants();
|
||||
}
|
||||
|
||||
$properties = $class->getProperties();
|
||||
if (!empty($properties)) {
|
||||
usort($properties, $this->getPropertySortFunc());
|
||||
|
||||
$visitor->startVisitingProperties();
|
||||
foreach ($properties as $property) {
|
||||
$visitor->visitProperty($property);
|
||||
}
|
||||
$visitor->endVisitingProperties();
|
||||
}
|
||||
|
||||
$methods = $class->getMethods();
|
||||
if (!empty($methods)) {
|
||||
usort($methods, $this->getMethodSortFunc());
|
||||
|
||||
$visitor->startVisitingMethods();
|
||||
foreach ($methods as $method) {
|
||||
$visitor->visitMethod($method);
|
||||
}
|
||||
$visitor->endVisitingMethods();
|
||||
}
|
||||
|
||||
$visitor->endVisitingClass($class);
|
||||
}
|
||||
|
||||
private function getConstantSortFunc()
|
||||
{
|
||||
return $this->constantSortFunc ?: 'strcasecmp';
|
||||
}
|
||||
|
||||
private function getMethodSortFunc()
|
||||
{
|
||||
if (null !== $this->methodSortFunc) {
|
||||
return $this->methodSortFunc;
|
||||
}
|
||||
|
||||
static $defaultSortFunc;
|
||||
if (empty($defaultSortFunc)) {
|
||||
$defaultSortFunc = function($a, $b) {
|
||||
if ($a->isStatic() !== $isStatic = $b->isStatic()) {
|
||||
return $isStatic ? 1 : -1;
|
||||
}
|
||||
|
||||
if (($aV = $a->getVisibility()) !== $bV = $b->getVisibility()) {
|
||||
$aV = 'public' === $aV ? 3 : ('protected' === $aV ? 2 : 1);
|
||||
$bV = 'public' === $bV ? 3 : ('protected' === $bV ? 2 : 1);
|
||||
|
||||
return $aV > $bV ? -1 : 1;
|
||||
}
|
||||
|
||||
$rs = strcasecmp($a->getName(), $b->getName());
|
||||
if (0 === $rs) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return $rs > 0 ? -1 : 1;
|
||||
};
|
||||
}
|
||||
|
||||
return $defaultSortFunc;
|
||||
}
|
||||
|
||||
private function getPropertySortFunc()
|
||||
{
|
||||
if (null !== $this->propertySortFunc) {
|
||||
return $this->propertySortFunc;
|
||||
}
|
||||
|
||||
static $defaultSortFunc;
|
||||
if (empty($defaultSortFunc)) {
|
||||
$defaultSortFunc = function($a, $b) {
|
||||
if (($aV = $a->getVisibility()) !== $bV = $b->getVisibility()) {
|
||||
$aV = 'public' === $aV ? 3 : ('protected' === $aV ? 2 : 1);
|
||||
$bV = 'public' === $bV ? 3 : ('protected' === $bV ? 2 : 1);
|
||||
|
||||
return $aV > $bV ? -1 : 1;
|
||||
}
|
||||
|
||||
$rs = strcasecmp($a->getName(), $b->getName());
|
||||
if (0 === $rs) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return $rs > 0 ? -1 : 1;
|
||||
};
|
||||
}
|
||||
|
||||
return $defaultSortFunc;
|
||||
}
|
||||
}
|
237
vendor/jms/cg/src/CG/Generator/DefaultVisitor.php
vendored
Normal file
237
vendor/jms/cg/src/CG/Generator/DefaultVisitor.php
vendored
Normal file
@@ -0,0 +1,237 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
namespace CG\Generator;
|
||||
|
||||
/**
|
||||
* The default code generation visitor.
|
||||
*
|
||||
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*/
|
||||
class DefaultVisitor implements DefaultVisitorInterface
|
||||
{
|
||||
private $writer;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->writer = new Writer();
|
||||
}
|
||||
|
||||
public function reset()
|
||||
{
|
||||
$this->writer->reset();
|
||||
}
|
||||
|
||||
public function startVisitingClass(PhpClass $class)
|
||||
{
|
||||
if ($namespace = $class->getNamespace()) {
|
||||
$this->writer->write('namespace '.$namespace.';'."\n\n");
|
||||
}
|
||||
|
||||
if ($files = $class->getRequiredFiles()) {
|
||||
foreach ($files as $file) {
|
||||
$this->writer->writeln('require_once '.var_export($file, true).';');
|
||||
}
|
||||
|
||||
$this->writer->write("\n");
|
||||
}
|
||||
|
||||
if ($useStatements = $class->getUseStatements()) {
|
||||
foreach ($useStatements as $alias => $namespace) {
|
||||
$this->writer->write('use '.$namespace);
|
||||
|
||||
if (substr($namespace, strrpos($namespace, '\\') + 1) !== $alias) {
|
||||
$this->writer->write(' as '.$alias);
|
||||
}
|
||||
|
||||
$this->writer->write(";\n");
|
||||
}
|
||||
|
||||
$this->writer->write("\n");
|
||||
}
|
||||
|
||||
if ($docblock = $class->getDocblock()) {
|
||||
$this->writer->write($docblock);
|
||||
}
|
||||
|
||||
$this->writer->write('class '.$class->getShortName());
|
||||
|
||||
if ($parentClassName = $class->getParentClassName()) {
|
||||
$this->writer->write(' extends '.('\\' === $parentClassName[0] ? $parentClassName : '\\'.$parentClassName));
|
||||
}
|
||||
|
||||
$interfaceNames = $class->getInterfaceNames();
|
||||
if (!empty($interfaceNames)) {
|
||||
$interfaceNames = array_unique($interfaceNames);
|
||||
|
||||
$interfaceNames = array_map(function($name) {
|
||||
if ('\\' === $name[0]) {
|
||||
return $name;
|
||||
}
|
||||
|
||||
return '\\'.$name;
|
||||
}, $interfaceNames);
|
||||
|
||||
$this->writer->write(' implements '.implode(', ', $interfaceNames));
|
||||
}
|
||||
|
||||
$this->writer
|
||||
->write("\n{\n")
|
||||
->indent()
|
||||
;
|
||||
}
|
||||
|
||||
public function startVisitingConstants()
|
||||
{
|
||||
}
|
||||
|
||||
public function visitConstant($name, $value)
|
||||
{
|
||||
$this->writer->writeln('const '.$name.' = '.var_export($value, true).';');
|
||||
}
|
||||
|
||||
public function endVisitingConstants()
|
||||
{
|
||||
$this->writer->write("\n");
|
||||
}
|
||||
|
||||
public function startVisitingProperties()
|
||||
{
|
||||
}
|
||||
|
||||
public function visitProperty(PhpProperty $property)
|
||||
{
|
||||
$this->writer->write($property->getVisibility().' '.($property->isStatic()? 'static ' : '').'$'.$property->getName());
|
||||
|
||||
if ($property->hasDefaultValue()) {
|
||||
$this->writer->write(' = '.var_export($property->getDefaultValue(), true));
|
||||
}
|
||||
|
||||
$this->writer->writeln(';');
|
||||
}
|
||||
|
||||
public function endVisitingProperties()
|
||||
{
|
||||
$this->writer->write("\n");
|
||||
}
|
||||
|
||||
public function startVisitingMethods()
|
||||
{
|
||||
}
|
||||
|
||||
public function visitMethod(PhpMethod $method)
|
||||
{
|
||||
if ($docblock = $method->getDocblock()) {
|
||||
$this->writer->writeln($docblock)->rtrim();
|
||||
}
|
||||
|
||||
if ($method->isAbstract()) {
|
||||
$this->writer->write('abstract ');
|
||||
}
|
||||
|
||||
$this->writer->write($method->getVisibility().' ');
|
||||
|
||||
if ($method->isStatic()) {
|
||||
$this->writer->write('static ');
|
||||
}
|
||||
|
||||
$this->writer->write('function '.$method->getName().'(');
|
||||
|
||||
$this->writeParameters($method->getParameters());
|
||||
|
||||
if ($method->isAbstract()) {
|
||||
$this->writer->write(");\n\n");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->writer
|
||||
->writeln(")")
|
||||
->writeln('{')
|
||||
->indent()
|
||||
->writeln($method->getBody())
|
||||
->outdent()
|
||||
->rtrim()
|
||||
->write("}\n\n")
|
||||
;
|
||||
}
|
||||
|
||||
public function endVisitingMethods()
|
||||
{
|
||||
}
|
||||
|
||||
public function endVisitingClass(PhpClass $class)
|
||||
{
|
||||
$this->writer
|
||||
->outdent()
|
||||
->rtrim()
|
||||
->write('}')
|
||||
;
|
||||
}
|
||||
|
||||
public function visitFunction(PhpFunction $function)
|
||||
{
|
||||
if ($namespace = $function->getNamespace()) {
|
||||
$this->writer->write("namespace $namespace;\n\n");
|
||||
}
|
||||
|
||||
$this->writer->write("function {$function->getName()}(");
|
||||
$this->writeParameters($function->getParameters());
|
||||
$this->writer
|
||||
->write(")\n{\n")
|
||||
->indent()
|
||||
->writeln($function->getBody())
|
||||
->outdent()
|
||||
->rtrim()
|
||||
->write('}')
|
||||
;
|
||||
}
|
||||
|
||||
public function getContent()
|
||||
{
|
||||
return $this->writer->getContent();
|
||||
}
|
||||
|
||||
private function writeParameters(array $parameters)
|
||||
{
|
||||
$first = true;
|
||||
foreach ($parameters as $parameter) {
|
||||
if (!$first) {
|
||||
$this->writer->write(', ');
|
||||
}
|
||||
$first = false;
|
||||
|
||||
if ($type = $parameter->getType()) {
|
||||
$this->writer->write(
|
||||
('array' === $type ? 'array' : ('\\' === $type[0] ? $type : '\\'. $type))
|
||||
.' '
|
||||
);
|
||||
}
|
||||
|
||||
if ($parameter->isPassedByReference()) {
|
||||
$this->writer->write('&');
|
||||
}
|
||||
|
||||
$this->writer->write('$'.$parameter->getName());
|
||||
|
||||
if ($parameter->hasDefaultValue()) {
|
||||
$this->writer->write(' = '.var_export($parameter->getDefaultValue(), true));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
47
vendor/jms/cg/src/CG/Generator/DefaultVisitorInterface.php
vendored
Normal file
47
vendor/jms/cg/src/CG/Generator/DefaultVisitorInterface.php
vendored
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
namespace CG\Generator;
|
||||
|
||||
/**
|
||||
* The visitor interface required by the DefaultNavigator.
|
||||
*
|
||||
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*/
|
||||
interface DefaultVisitorInterface
|
||||
{
|
||||
/**
|
||||
* Resets the visitors internal state to allow re-using the same instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function reset();
|
||||
|
||||
function startVisitingClass(PhpClass $class);
|
||||
function startVisitingConstants();
|
||||
function visitConstant($name, $value);
|
||||
function endVisitingConstants();
|
||||
function startVisitingProperties();
|
||||
function visitProperty(PhpProperty $property);
|
||||
function endVisitingProperties();
|
||||
function startVisitingMethods();
|
||||
function visitMethod(PhpMethod $method);
|
||||
function endVisitingMethods();
|
||||
function endVisitingClass(PhpClass $class);
|
||||
function visitFunction(PhpFunction $function);
|
||||
}
|
38
vendor/jms/cg/src/CG/Generator/GeneratorUtils.php
vendored
Normal file
38
vendor/jms/cg/src/CG/Generator/GeneratorUtils.php
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
namespace CG\Generator;
|
||||
|
||||
/**
|
||||
* Some Generator utils.
|
||||
*
|
||||
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*/
|
||||
abstract class GeneratorUtils
|
||||
{
|
||||
private final function __construct() {}
|
||||
|
||||
public static function callMethod(\ReflectionMethod $method, array $params = null)
|
||||
{
|
||||
if (null === $params) {
|
||||
$params = array_map(function($p) { return '$'.$p->name; }, $method->getParameters());
|
||||
}
|
||||
|
||||
return '\\'.$method->getDeclaringClass()->name.'::'.$method->name.'('.implode(', ', $params).')';
|
||||
}
|
||||
}
|
360
vendor/jms/cg/src/CG/Generator/PhpClass.php
vendored
Normal file
360
vendor/jms/cg/src/CG/Generator/PhpClass.php
vendored
Normal file
@@ -0,0 +1,360 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
namespace CG\Generator;
|
||||
|
||||
use Doctrine\Common\Annotations\PhpParser;
|
||||
|
||||
use CG\Core\ReflectionUtils;
|
||||
|
||||
/**
|
||||
* Represents a PHP class.
|
||||
*
|
||||
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*/
|
||||
class PhpClass
|
||||
{
|
||||
private static $phpParser;
|
||||
|
||||
private $name;
|
||||
private $parentClassName;
|
||||
private $interfaceNames = array();
|
||||
private $useStatements = array();
|
||||
private $constants = array();
|
||||
private $properties = array();
|
||||
private $requiredFiles = array();
|
||||
private $methods = array();
|
||||
private $abstract = false;
|
||||
private $final = false;
|
||||
private $docblock;
|
||||
|
||||
public static function create($name = null)
|
||||
{
|
||||
return new self($name);
|
||||
}
|
||||
|
||||
public static function fromReflection(\ReflectionClass $ref)
|
||||
{
|
||||
$class = new static();
|
||||
$class
|
||||
->setName($ref->name)
|
||||
->setAbstract($ref->isAbstract())
|
||||
->setFinal($ref->isFinal())
|
||||
->setConstants($ref->getConstants())
|
||||
;
|
||||
|
||||
if (null === self::$phpParser) {
|
||||
if (!class_exists('Doctrine\Common\Annotations\PhpParser')) {
|
||||
self::$phpParser = false;
|
||||
} else {
|
||||
self::$phpParser = new PhpParser();
|
||||
}
|
||||
}
|
||||
|
||||
if (false !== self::$phpParser) {
|
||||
$class->setUseStatements(self::$phpParser->parseClass($ref));
|
||||
}
|
||||
|
||||
if ($docComment = $ref->getDocComment()) {
|
||||
$class->setDocblock(ReflectionUtils::getUnindentedDocComment($docComment));
|
||||
}
|
||||
|
||||
foreach ($ref->getMethods() as $method) {
|
||||
$class->setMethod(static::createMethod($method));
|
||||
}
|
||||
|
||||
foreach ($ref->getProperties() as $property) {
|
||||
$class->setProperty(static::createProperty($property));
|
||||
}
|
||||
|
||||
return $class;
|
||||
}
|
||||
|
||||
protected static function createMethod(\ReflectionMethod $method)
|
||||
{
|
||||
return PhpMethod::fromReflection($method);
|
||||
}
|
||||
|
||||
protected static function createProperty(\ReflectionProperty $property)
|
||||
{
|
||||
return PhpProperty::fromReflection($property);
|
||||
}
|
||||
|
||||
public function __construct($name = null)
|
||||
{
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
public function setName($name)
|
||||
{
|
||||
$this->name = $name;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setParentClassName($name)
|
||||
{
|
||||
$this->parentClassName = $name;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setInterfaceNames(array $names)
|
||||
{
|
||||
$this->interfaceNames = $names;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addInterfaceName($name)
|
||||
{
|
||||
$this->interfaceNames[] = $name;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setRequiredFiles(array $files)
|
||||
{
|
||||
$this->requiredFiles = $files;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addRequiredFile($file)
|
||||
{
|
||||
$this->requiredFiles[] = $file;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setUseStatements(array $useStatements)
|
||||
{
|
||||
$this->useStatements = $useStatements;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addUseStatement($namespace, $alias = null)
|
||||
{
|
||||
if (null === $alias) {
|
||||
$alias = substr($namespace, strrpos($namespace, '\\') + 1);
|
||||
}
|
||||
|
||||
$this->useStatements[$alias] = $namespace;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setConstants(array $constants)
|
||||
{
|
||||
$this->constants = $constants;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setConstant($name, $value)
|
||||
{
|
||||
$this->constants[$name] = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function hasConstant($name)
|
||||
{
|
||||
return array_key_exists($this->constants, $name);
|
||||
}
|
||||
|
||||
public function removeConstant($name)
|
||||
{
|
||||
if (!array_key_exists($name, $this->constants)) {
|
||||
throw new \InvalidArgumentException(sprintf('The constant "%s" does not exist.', $name));
|
||||
}
|
||||
|
||||
unset($this->constants[$name]);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setProperties(array $properties)
|
||||
{
|
||||
$this->properties = $properties;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setProperty(PhpProperty $property)
|
||||
{
|
||||
$this->properties[$property->getName()] = $property;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function hasProperty($property)
|
||||
{
|
||||
if ($property instanceof PhpProperty) {
|
||||
$property = $property->getName();
|
||||
}
|
||||
|
||||
return isset($this->properties[$property]);
|
||||
}
|
||||
|
||||
public function removeProperty($property)
|
||||
{
|
||||
if ($property instanceof PhpProperty) {
|
||||
$property = $property->getName();
|
||||
}
|
||||
|
||||
if (!array_key_exists($property, $this->properties)) {
|
||||
throw new \InvalidArgumentException(sprintf('The property "%s" does not exist.', $property));
|
||||
}
|
||||
unset($this->properties[$property]);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setMethods(array $methods)
|
||||
{
|
||||
$this->methods = $methods;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setMethod(PhpMethod $method)
|
||||
{
|
||||
$this->methods[$method->getName()] = $method;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function hasMethod($method)
|
||||
{
|
||||
if ($method instanceof PhpMethod) {
|
||||
$method = $method->getName();
|
||||
}
|
||||
|
||||
return isset($this->methods[$method]);
|
||||
}
|
||||
|
||||
public function removeMethod($method)
|
||||
{
|
||||
if ($method instanceof PhpMethod) {
|
||||
$method = $method->getName();
|
||||
}
|
||||
|
||||
if (!array_key_exists($method, $this->methods)) {
|
||||
throw new \InvalidArgumentException(sprintf('The method "%s" does not exist.', $method));
|
||||
}
|
||||
unset($this->methods[$method]);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setAbstract($bool)
|
||||
{
|
||||
$this->abstract = (Boolean) $bool;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setFinal($bool)
|
||||
{
|
||||
$this->final = (Boolean) $bool;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setDocblock($block)
|
||||
{
|
||||
$this->docblock = $block;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function getParentClassName()
|
||||
{
|
||||
return $this->parentClassName;
|
||||
}
|
||||
|
||||
public function getInterfaceNames()
|
||||
{
|
||||
return $this->interfaceNames;
|
||||
}
|
||||
|
||||
public function getRequiredFiles()
|
||||
{
|
||||
return $this->requiredFiles;
|
||||
}
|
||||
|
||||
public function getUseStatements()
|
||||
{
|
||||
return $this->useStatements;
|
||||
}
|
||||
|
||||
public function getNamespace()
|
||||
{
|
||||
if (false === $pos = strrpos($this->name, '\\')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return substr($this->name, 0, $pos);
|
||||
}
|
||||
|
||||
public function getShortName()
|
||||
{
|
||||
if (false === $pos = strrpos($this->name, '\\')) {
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
return substr($this->name, $pos+1);
|
||||
}
|
||||
|
||||
public function getConstants()
|
||||
{
|
||||
return $this->constants;
|
||||
}
|
||||
|
||||
public function getProperties()
|
||||
{
|
||||
return $this->properties;
|
||||
}
|
||||
|
||||
public function getMethods()
|
||||
{
|
||||
return $this->methods;
|
||||
}
|
||||
|
||||
public function isAbstract()
|
||||
{
|
||||
return $this->abstract;
|
||||
}
|
||||
|
||||
public function isFinal()
|
||||
{
|
||||
return $this->final;
|
||||
}
|
||||
|
||||
public function getDocblock()
|
||||
{
|
||||
return $this->docblock;
|
||||
}
|
||||
}
|
146
vendor/jms/cg/src/CG/Generator/PhpFunction.php
vendored
Normal file
146
vendor/jms/cg/src/CG/Generator/PhpFunction.php
vendored
Normal file
@@ -0,0 +1,146 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
namespace CG\Generator;
|
||||
|
||||
/**
|
||||
* Represents a PHP function.
|
||||
*
|
||||
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*/
|
||||
class PhpFunction
|
||||
{
|
||||
private $name;
|
||||
private $namespace;
|
||||
private $parameters = array();
|
||||
private $body = '';
|
||||
private $referenceReturned = false;
|
||||
private $docblock;
|
||||
|
||||
public static function create($name = null)
|
||||
{
|
||||
return new static($name);
|
||||
}
|
||||
|
||||
public function __construct($name = null)
|
||||
{
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
public function setName($name)
|
||||
{
|
||||
$this->name = $name;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setNamespace($namespace)
|
||||
{
|
||||
$this->namespace = $namespace;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setParameters(array $parameters)
|
||||
{
|
||||
$this->parameters = $parameters;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setReferenceReturned($bool)
|
||||
{
|
||||
$this->referenceReturned = (Boolean) $bool;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function replaceParameter($position, PhpParameter $parameter)
|
||||
{
|
||||
if ($position < 0 || $position > count($this->parameters)) {
|
||||
throw new \InvalidArgumentException(sprintf('$position must be in the range [0, %d].', count($this->parameters)));
|
||||
}
|
||||
|
||||
$this->parameters[$position] = $parameter;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addParameter(PhpParameter $parameter)
|
||||
{
|
||||
$this->parameters[] = $parameter;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function removeParameter($position)
|
||||
{
|
||||
if (!isset($this->parameters[$position])) {
|
||||
throw new \InvalidArgumentException(sprintf('There is not parameter at position %d.', $position));
|
||||
}
|
||||
|
||||
unset($this->parameters[$position]);
|
||||
$this->parameters = array_values($this->parameters);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setBody($body)
|
||||
{
|
||||
$this->body = $body;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setDocblock($docBlock)
|
||||
{
|
||||
$this->docblock = $docBlock;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function getNamespace()
|
||||
{
|
||||
return $this->namespace;
|
||||
}
|
||||
|
||||
public function getParameters()
|
||||
{
|
||||
return $this->parameters;
|
||||
}
|
||||
|
||||
public function getBody()
|
||||
{
|
||||
return $this->body;
|
||||
}
|
||||
|
||||
public function getDocblock()
|
||||
{
|
||||
return $this->docblock;
|
||||
}
|
||||
|
||||
public function isReferenceReturned()
|
||||
{
|
||||
return $this->referenceReturned;
|
||||
}
|
||||
}
|
158
vendor/jms/cg/src/CG/Generator/PhpMethod.php
vendored
Normal file
158
vendor/jms/cg/src/CG/Generator/PhpMethod.php
vendored
Normal file
@@ -0,0 +1,158 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
namespace CG\Generator;
|
||||
|
||||
use CG\Core\ReflectionUtils;
|
||||
|
||||
/**
|
||||
* Represents a PHP method.
|
||||
*
|
||||
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*/
|
||||
class PhpMethod extends AbstractPhpMember
|
||||
{
|
||||
private $final = false;
|
||||
private $abstract = false;
|
||||
private $parameters = array();
|
||||
private $referenceReturned = false;
|
||||
private $body = '';
|
||||
|
||||
public static function create($name = null)
|
||||
{
|
||||
return new static($name);
|
||||
}
|
||||
|
||||
public static function fromReflection(\ReflectionMethod $ref)
|
||||
{
|
||||
$method = new static();
|
||||
$method
|
||||
->setFinal($ref->isFinal())
|
||||
->setAbstract($ref->isAbstract())
|
||||
->setStatic($ref->isStatic())
|
||||
->setVisibility($ref->isPublic() ? self::VISIBILITY_PUBLIC : ($ref->isProtected() ? self::VISIBILITY_PROTECTED : self::VISIBILITY_PRIVATE))
|
||||
->setReferenceReturned($ref->returnsReference())
|
||||
->setName($ref->name)
|
||||
;
|
||||
|
||||
if ($docComment = $ref->getDocComment()) {
|
||||
$method->setDocblock(ReflectionUtils::getUnindentedDocComment($docComment));
|
||||
}
|
||||
|
||||
foreach ($ref->getParameters() as $param) {
|
||||
$method->addParameter(static::createParameter($param));
|
||||
}
|
||||
|
||||
// FIXME: Extract body?
|
||||
|
||||
return $method;
|
||||
}
|
||||
|
||||
protected static function createParameter(\ReflectionParameter $parameter)
|
||||
{
|
||||
return PhpParameter::fromReflection($parameter);
|
||||
}
|
||||
|
||||
public function setFinal($bool)
|
||||
{
|
||||
$this->final = (Boolean) $bool;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setAbstract($bool)
|
||||
{
|
||||
$this->abstract = $bool;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setReferenceReturned($bool)
|
||||
{
|
||||
$this->referenceReturned = (Boolean) $bool;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setBody($body)
|
||||
{
|
||||
$this->body = $body;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setParameters(array $parameters)
|
||||
{
|
||||
$this->parameters = array_values($parameters);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addParameter(PhpParameter $parameter)
|
||||
{
|
||||
$this->parameters[] = $parameter;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function replaceParameter($position, PhpParameter $parameter)
|
||||
{
|
||||
if ($position < 0 || $position > strlen($this->parameters)) {
|
||||
throw new \InvalidArgumentException(sprintf('The position must be in the range [0, %d].', strlen($this->parameters)));
|
||||
}
|
||||
$this->parameters[$position] = $parameter;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function removeParameter($position)
|
||||
{
|
||||
if (!isset($this->parameters[$position])) {
|
||||
throw new \InvalidArgumentException(sprintf('There is no parameter at position "%d" does not exist.', $position));
|
||||
}
|
||||
unset($this->parameters[$position]);
|
||||
$this->parameters = array_values($this->parameters);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function isFinal()
|
||||
{
|
||||
return $this->final;
|
||||
}
|
||||
|
||||
public function isAbstract()
|
||||
{
|
||||
return $this->abstract;
|
||||
}
|
||||
|
||||
public function isReferenceReturned()
|
||||
{
|
||||
return $this->referenceReturned;
|
||||
}
|
||||
|
||||
public function getBody()
|
||||
{
|
||||
return $this->body;
|
||||
}
|
||||
|
||||
public function getParameters()
|
||||
{
|
||||
return $this->parameters;
|
||||
}
|
||||
}
|
126
vendor/jms/cg/src/CG/Generator/PhpParameter.php
vendored
Normal file
126
vendor/jms/cg/src/CG/Generator/PhpParameter.php
vendored
Normal file
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
namespace CG\Generator;
|
||||
|
||||
/**
|
||||
* Represents a PHP parameter.
|
||||
*
|
||||
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*/
|
||||
class PhpParameter
|
||||
{
|
||||
private $name;
|
||||
private $defaultValue;
|
||||
private $hasDefaultValue = false;
|
||||
private $passedByReference = false;
|
||||
private $type;
|
||||
|
||||
public static function create($name = null)
|
||||
{
|
||||
return new static($name);
|
||||
}
|
||||
|
||||
public static function fromReflection(\ReflectionParameter $ref)
|
||||
{
|
||||
$parameter = new static();
|
||||
$parameter
|
||||
->setName($ref->name)
|
||||
->setPassedByReference($ref->isPassedByReference())
|
||||
;
|
||||
|
||||
if ($ref->isDefaultValueAvailable()) {
|
||||
$parameter->setDefaultValue($ref->getDefaultValue());
|
||||
}
|
||||
|
||||
if ($ref->isArray()) {
|
||||
$parameter->setType('array');
|
||||
} else if ($class = $ref->getClass()) {
|
||||
$parameter->setType($class->name);
|
||||
}
|
||||
|
||||
return $parameter;
|
||||
}
|
||||
|
||||
public function __construct($name = null)
|
||||
{
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
public function setName($name)
|
||||
{
|
||||
$this->name = $name;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setDefaultValue($value)
|
||||
{
|
||||
$this->defaultValue = $value;
|
||||
$this->hasDefaultValue = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function unsetDefaultValue()
|
||||
{
|
||||
$this->defaultValue = null;
|
||||
$this->hasDefaultValue = false;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setPassedByReference($bool)
|
||||
{
|
||||
$this->passedByReference = (Boolean) $bool;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setType($type)
|
||||
{
|
||||
$this->type = $type;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function getDefaultValue()
|
||||
{
|
||||
return $this->defaultValue;
|
||||
}
|
||||
|
||||
public function hasDefaultValue()
|
||||
{
|
||||
return $this->hasDefaultValue;
|
||||
}
|
||||
|
||||
public function isPassedByReference()
|
||||
{
|
||||
return $this->passedByReference;
|
||||
}
|
||||
|
||||
public function getType()
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
}
|
84
vendor/jms/cg/src/CG/Generator/PhpProperty.php
vendored
Normal file
84
vendor/jms/cg/src/CG/Generator/PhpProperty.php
vendored
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
namespace CG\Generator;
|
||||
|
||||
use CG\Core\ReflectionUtils;
|
||||
|
||||
/**
|
||||
* Represents a PHP property.
|
||||
*
|
||||
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*/
|
||||
class PhpProperty extends AbstractPhpMember
|
||||
{
|
||||
private $hasDefaultValue = false;
|
||||
private $defaultValue;
|
||||
|
||||
public static function create($name = null)
|
||||
{
|
||||
return new static($name);
|
||||
}
|
||||
|
||||
public static function fromReflection(\ReflectionProperty $ref)
|
||||
{
|
||||
$property = new static();
|
||||
$property
|
||||
->setName($ref->name)
|
||||
->setStatic($ref->isStatic())
|
||||
->setVisibility($ref->isPublic() ? self::VISIBILITY_PUBLIC : ($ref->isProtected() ? self::VISIBILITY_PROTECTED : self::VISIBILITY_PRIVATE))
|
||||
;
|
||||
|
||||
if ($docComment = $ref->getDocComment()) {
|
||||
$property->setDocblock(ReflectionUtils::getUnindentedDocComment($docComment));
|
||||
}
|
||||
|
||||
$defaultProperties = $ref->getDeclaringClass()->getDefaultProperties();
|
||||
if (isset($defaultProperties[$ref->name])) {
|
||||
$property->setDefaultValue($defaultProperties[$ref->name]);
|
||||
}
|
||||
|
||||
return $property;
|
||||
}
|
||||
|
||||
public function setDefaultValue($value)
|
||||
{
|
||||
$this->defaultValue = $value;
|
||||
$this->hasDefaultValue = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function unsetDefaultValue()
|
||||
{
|
||||
$this->hasDefaultValue = false;
|
||||
$this->defaultValue = null;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function hasDefaultValue()
|
||||
{
|
||||
return $this->hasDefaultValue;
|
||||
}
|
||||
|
||||
public function getDefaultValue()
|
||||
{
|
||||
return $this->defaultValue;
|
||||
}
|
||||
}
|
103
vendor/jms/cg/src/CG/Generator/Writer.php
vendored
Normal file
103
vendor/jms/cg/src/CG/Generator/Writer.php
vendored
Normal file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
namespace CG\Generator;
|
||||
|
||||
/**
|
||||
* A writer implementation.
|
||||
*
|
||||
* This may be used to simplify writing well-formatted code.
|
||||
*
|
||||
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*/
|
||||
class Writer
|
||||
{
|
||||
private $content = '';
|
||||
private $indentationSpaces = 4;
|
||||
private $indentationLevel = 0;
|
||||
|
||||
public function indent()
|
||||
{
|
||||
$this->indentationLevel += 1;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function outdent()
|
||||
{
|
||||
$this->indentationLevel -= 1;
|
||||
|
||||
if ($this->indentationLevel < 0) {
|
||||
throw new \RuntimeException('The identation level cannot be less than zero.');
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function writeln($content)
|
||||
{
|
||||
$this->write($content."\n");
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function write($content)
|
||||
{
|
||||
$lines = explode("\n", $content);
|
||||
for ($i=0,$c=count($lines); $i<$c; $i++) {
|
||||
if ($this->indentationLevel > 0
|
||||
&& !empty($lines[$i])
|
||||
&& (empty($this->content) || "\n" === substr($this->content, -1))) {
|
||||
$this->content .= str_repeat(' ', $this->indentationLevel * $this->indentationSpaces);
|
||||
}
|
||||
|
||||
$this->content .= $lines[$i];
|
||||
|
||||
if ($i+1 < $c) {
|
||||
$this->content .= "\n";
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function rtrim()
|
||||
{
|
||||
$addNl = "\n" === substr($this->content, -1);
|
||||
$this->content = rtrim($this->content);
|
||||
|
||||
if ($addNl) {
|
||||
$this->content .= "\n";
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function reset()
|
||||
{
|
||||
$this->content = '';
|
||||
$this->indentationLevel = 0;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getContent()
|
||||
{
|
||||
return $this->content;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user