Initial commit with Symfony 2.1+Vendors

Signed-off-by: Gergely POLONKAI (W00d5t0ck) <polesz@w00d5t0ck.info>
This commit is contained in:
Polonkai Gergely
2012-07-01 09:52:20 +02:00
commit 082a0130c2
5381 changed files with 416709 additions and 0 deletions

201
vendor/jms/cg/LICENSE vendored Normal file
View File

@@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
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.

19
vendor/jms/cg/composer.json vendored Normal file
View File

@@ -0,0 +1,19 @@
{
"name": "jms/cg",
"description": "Toolset for generating PHP code",
"keywords": ["code generation"],
"type": "library",
"license": "Apache",
"authors": [
{
"name": "Johannes M. Schmitt",
"email": "schmittjoh@gmail.com"
}
],
"require": {
"php": ">=5.3.0"
},
"autoload": {
"psr-0": { "CG\\": "src/" }
}
}

View File

@@ -0,0 +1,60 @@
<?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\Core;
use CG\Generator\PhpClass;
/**
* Abstract base class for all class generators.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
abstract class AbstractClassGenerator implements ClassGeneratorInterface
{
private $namingStrategy;
private $generatorStrategy;
public function setNamingStrategy(NamingStrategyInterface $namingStrategy = null)
{
$this->namingStrategy = $namingStrategy;
}
public function setGeneratorStrategy(GeneratorStrategyInterface $generatorStrategy = null)
{
$this->generatorStrategy = $generatorStrategy;
}
public function getClassName(\ReflectionClass $class)
{
if (null === $this->namingStrategy) {
$this->namingStrategy = new DefaultNamingStrategy();
}
return $this->namingStrategy->getClassName($class);
}
protected function generateCode(PhpClass $class)
{
if (null === $this->generatorStrategy) {
$this->generatorStrategy = new DefaultGeneratorStrategy();
}
return $this->generatorStrategy->generate($class);
}
}

View File

@@ -0,0 +1,36 @@
<?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\Core;
use CG\Generator\PhpClass;
/**
* Interface for class generators.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
interface ClassGeneratorInterface
{
/**
* Generates the PHP class.
*
* @return string
*/
function generateClass();
}

View File

@@ -0,0 +1,35 @@
<?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\Core;
use CG\Proxy\Enhancer;
abstract class ClassUtils
{
public static function getUserClass($className)
{
if (false === $pos = strrpos($className, '\\'.NamingStrategyInterface::SEPARATOR.'\\')) {
return $className;
}
return substr($className, $pos + NamingStrategyInterface::SEPARATOR_LENGTH + 2);
}
private final function __construct() {}
}

View File

@@ -0,0 +1,67 @@
<?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\Core;
use CG\Generator\DefaultVisitorInterface;
use CG\Generator\PhpClass;
use CG\Generator\DefaultVisitor;
use CG\Generator\DefaultNavigator;
/**
* The default generator strategy.
*
* This strategy allows to change the order in which methods, properties and
* constants are sorted.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class DefaultGeneratorStrategy implements GeneratorStrategyInterface
{
private $navigator;
private $visitor;
public function __construct(DefaultVisitorInterface $visitor = null)
{
$this->navigator = new DefaultNavigator();
$this->visitor = $visitor ?: new DefaultVisitor();
}
public function setConstantSortFunc(\Closure $func = null)
{
$this->navigator->setConstantSortFunc($func);
}
public function setMethodSortFunc(\Closure $func = null)
{
$this->navigator->setMethodSortFunc($func);
}
public function setPropertySortFunc(\Closure $func = null)
{
$this->navigator->setPropertySortFunc($func);
}
public function generate(PhpClass $class)
{
$this->visitor->reset();
$this->navigator->accept($this->visitor, $class);
return $this->visitor->getContent();
}
}

View File

@@ -0,0 +1,41 @@
<?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\Core;
/**
* The default naming strategy.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class DefaultNamingStrategy implements NamingStrategyInterface
{
private $prefix;
public function __construct($prefix = 'EnhancedProxy')
{
$this->prefix = $prefix;
}
public function getClassName(\ReflectionClass $class)
{
$userClass = ClassUtils::getUserClass($class->name);
return $this->prefix.'_'.sha1($class->name).'\\'.self::SEPARATOR.'\\'.$userClass;
}
}

View File

@@ -0,0 +1,34 @@
<?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\Core;
use CG\Generator\PhpClass;
/**
* Generator Strategy Interface.
*
* Implementing classes are responsible for generating PHP code from the given
* PhpClass instance.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
interface GeneratorStrategyInterface
{
function generate(PhpClass $class);
}

View File

@@ -0,0 +1,50 @@
<?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\Core;
/**
* The naming strategy interface.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
interface NamingStrategyInterface
{
const SEPARATOR = '__CG__';
const SEPARATOR_LENGTH = 6;
/**
* Returns the class name for the proxy class.
*
* The generated class name MUST be the concatenation of a nonempty prefix,
* the namespace separator __CG__, and the original class name.
*
* Examples:
*
* +----------------------------+------------------------------+
* | Original Name | Generated Name |
* +============================+==============================+
* | Foo\Bar | dred332\__CG__\Foo\Bar |
* | Bar\Baz | Foo\Doo\__CG__\Bar\Baz |
* +----------------------------+------------------------------+
*
* @param \ReflectionClass $class
* @return string the class name for the generated class
*/
function getClassName(\ReflectionClass $class);
}

View File

@@ -0,0 +1,57 @@
<?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\Core;
abstract class ReflectionUtils
{
public static function getOverrideableMethods(\ReflectionClass $class, $publicOnly = false)
{
$filter = \ReflectionMethod::IS_PUBLIC;
if (!$publicOnly) {
$filter |= \ReflectionMethod::IS_PROTECTED;
}
return array_filter(
$class->getMethods($filter),
function($method) { return !$method->isFinal() && !$method->isStatic(); }
);
}
public static function getUnindentedDocComment($docComment)
{
$lines = explode("\n", $docComment);
for ($i=0,$c=count($lines); $i<$c; $i++) {
if (0 === $i) {
$docBlock = $lines[0]."\n";
continue;
}
$docBlock .= ' '.ltrim($lines[$i]);
if ($i+1 < $c) {
$docBlock .= "\n";
}
}
return $docBlock;
}
private final function __construct() { }
}

View 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;
}
}

View 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;
}
}

View 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));
}
}
}
}

View 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);
}

View 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).')';
}
}

View 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;
}
}

View 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;
}
}

View 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;
}
}

View 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;
}
}

View 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;
}
}

View 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;
}
}

158
vendor/jms/cg/src/CG/Proxy/Enhancer.php vendored Normal file
View 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\Proxy;
use CG\Core\NamingStrategyInterface;
use CG\Generator\Writer;
use CG\Generator\PhpMethod;
use CG\Generator\PhpDocblock;
use CG\Generator\PhpClass;
use CG\Core\AbstractClassGenerator;
/**
* Class enhancing generator implementation.
*
* This class enhances existing classes by generating a proxy and leveraging
* different generator implementation.
*
* There are several built-in generator such as lazy-initializing objects, or
* a generator for creating AOP joinpoints.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class Enhancer extends AbstractClassGenerator
{
private $generatedClass;
private $class;
private $interfaces;
private $generators;
public function __construct(\ReflectionClass $class, array $interfaces = array(), array $generators = array())
{
if (empty($generators) && empty($interfaces)) {
throw new \RuntimeException('Either generators, or interfaces must be given.');
}
$this->class = $class;
$this->interfaces = $interfaces;
$this->generators = $generators;
}
/**
* Creates a new instance of the enhanced class.
*
* @param array $args
* @return object
*/
public function createInstance(array $args = array())
{
$generatedClass = $this->getClassName($this->class);
if (!class_exists($generatedClass, false)) {
eval($this->generateClass());
}
$ref = new \ReflectionClass($generatedClass);
return $ref->newInstanceArgs($args);
}
public function writeClass($filename)
{
if (!is_dir($dir = dirname($filename))) {
if (false === @mkdir($dir, 0777, true)) {
throw new \RuntimeException(sprintf('Could not create directory "%s".', $dir));
}
}
if (!is_writable($dir)) {
throw new \RuntimeException(sprintf('The directory "%s" is not writable.', $dir));
}
file_put_contents($filename, "<?php\n\n".$this->generateClass());
}
/**
* Creates a new enhanced class
*
* @return string
*/
public final function generateClass()
{
static $docBlock;
if (empty($docBlock)) {
$writer = new Writer();
$writer
->writeln('/**')
->writeln(' * CG library enhanced proxy class.')
->writeln(' *')
->writeln(' * This code was generated automatically by the CG library, manual changes to it')
->writeln(' * will be lost upon next generation.')
->writeln(' */')
;
$docBlock = $writer->getContent();
}
$this->generatedClass = PhpClass::create()
->setDocblock($docBlock)
->setParentClassName($this->class->name)
;
$proxyClassName = $this->getClassName($this->class);
if (false === strpos($proxyClassName, NamingStrategyInterface::SEPARATOR)) {
throw new \RuntimeException(sprintf('The proxy class name must be suffixed with "%s" and an optional string, but got "%s".', NamingStrategyInterface::SEPARATOR, $proxyClassName));
}
$this->generatedClass->setName($proxyClassName);
if (!empty($this->interfaces)) {
$this->generatedClass->setInterfaceNames(array_map(function($v) { return '\\'.$v; }, $this->interfaces));
foreach ($this->getInterfaceMethods() as $method) {
$method = PhpMethod::fromReflection($method);
$method->setAbstract(false);
$this->generatedClass->setMethod($method);
}
}
if (!empty($this->generators)) {
foreach ($this->generators as $generator) {
$generator->generate($this->class, $this->generatedClass);
}
}
return $this->generateCode($this->generatedClass);
}
/**
* Adds stub methods for the interfaces that have been implemented.
*/
protected function getInterfaceMethods()
{
$methods = array();
foreach ($this->interfaces as $interface) {
$ref = new \ReflectionClass($interface);
$methods = array_merge($methods, $ref->getMethods());
}
return $methods;
}
}

View 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\Proxy;
use CG\Generator\PhpClass;
/**
* Interface for enhancing generators.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
interface GeneratorInterface
{
/**
* Generates the necessary changes in the class.
*
* @param \ReflectionClass $originalClass
* @param PhpClass $generatedClass The generated class
* @return void
*/
function generate(\ReflectionClass $originalClass, PhpClass $generatedClass);
}

View File

@@ -0,0 +1,117 @@
<?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\Proxy;
use CG\Core\ClassUtils;
use CG\Core\ReflectionUtils;
use CG\Generator\PhpParameter;
use CG\Generator\PhpProperty;
use CG\Generator\PhpMethod;
use CG\Generator\PhpClass;
/**
* Interception Generator.
*
* This generator creates joinpoints to allow for AOP advices. Right now, it only
* supports the most powerful around advice.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class InterceptionGenerator implements GeneratorInterface
{
private $prefix = '__CGInterception__';
private $filter;
private $requiredFile;
public function setRequiredFile($file)
{
$this->requiredFile = $file;
}
public function setPrefix($prefix)
{
$this->prefix = $prefix;
}
public function setFilter(\Closure $filter)
{
$this->filter = $filter;
}
public function generate(\ReflectionClass $originalClass, PhpClass $genClass)
{
$methods = ReflectionUtils::getOverrideableMethods($originalClass);
if (null !== $this->filter) {
$methods = array_filter($methods, $this->filter);
}
if (empty($methods)) {
return;
}
if (!empty($this->requiredFile)) {
$genClass->addRequiredFile($this->requiredFile);
}
$interceptorLoader = new PhpProperty();
$interceptorLoader
->setName($this->prefix.'loader')
->setVisibility(PhpProperty::VISIBILITY_PRIVATE)
;
$genClass->setProperty($interceptorLoader);
$loaderSetter = new PhpMethod();
$loaderSetter
->setName($this->prefix.'setLoader')
->setVisibility(PhpMethod::VISIBILITY_PUBLIC)
->setBody('$this->'.$this->prefix.'loader = $loader;')
;
$genClass->setMethod($loaderSetter);
$loaderParam = new PhpParameter();
$loaderParam
->setName('loader')
->setType('CG\Proxy\InterceptorLoaderInterface')
;
$loaderSetter->addParameter($loaderParam);
$interceptorCode =
'$ref = new \ReflectionMethod(%s, %s);'."\n"
.'$interceptors = $this->'.$this->prefix.'loader->loadInterceptors($ref, $this, array(%s));'."\n"
.'$invocation = new \CG\Proxy\MethodInvocation($ref, $this, array(%s), $interceptors);'."\n\n"
.'return $invocation->proceed();'
;
foreach ($methods as $method) {
$params = array();
foreach ($method->getParameters() as $param) {
$params[] = '$'.$param->name;
}
$params = implode(', ', $params);
$genMethod = PhpMethod::fromReflection($method)
->setBody(sprintf($interceptorCode, var_export(ClassUtils::getUserClass($method->class), true), var_export($method->name, true), $params, $params))
->setDocblock(null)
;
$genClass->setMethod($genMethod);
}
}
}

View 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\Proxy;
/**
* Interception Loader.
*
* Implementations of this interface are responsible for loading the interceptors
* for a certain method.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
interface InterceptorLoaderInterface
{
/**
* Loads interceptors.
*
* @param \ReflectionMethod $method
* @return array<MethodInterceptorInterface>
*/
function loadInterceptors(\ReflectionMethod $method);
}

View File

@@ -0,0 +1,153 @@
<?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\Proxy;
use CG\Generator\Writer;
use CG\Core\ReflectionUtils;
use CG\Generator\GeneratorUtils;
use CG\Generator\PhpParameter;
use CG\Generator\PhpMethod;
use CG\Generator\PhpProperty;
use CG\Generator\PhpClass;
/**
* Generator for creating lazy-initializing instances.
*
* This generator enhances concrete classes to allow for them to be lazily
* initialized upon first access.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class LazyInitializerGenerator implements GeneratorInterface
{
private $writer;
private $prefix = '__CG__';
private $markerInterface;
public function __construct()
{
$this->writer = new Writer();
}
public function setPrefix($prefix)
{
$this->prefix = $prefix;
}
/**
* Sets the marker interface which should be implemented by the
* generated classes.
*
* @param string $interface The FQCN of the interface
*/
public function setMarkerInterface($interface)
{
$this->markerInterface = $interface;
}
/**
* Generates the necessary methods in the class.
*
* @param \ReflectionClass $originalClass
* @param PhpClass $class
* @return void
*/
public function generate(\ReflectionClass $originalClass, PhpClass $class)
{
$methods = ReflectionUtils::getOverrideableMethods($originalClass, true);
// no public, non final methods
if (empty($methods)) {
return;
}
if (null !== $this->markerInterface) {
$class->setImplementedInterfaces(array_merge(
$class->getImplementedInterfaces(),
array($this->markerInterface)
));
}
$initializer = new PhpProperty();
$initializer->setName($this->prefix.'lazyInitializer');
$initializer->setVisibility(PhpProperty::VISIBILITY_PRIVATE);
$class->setProperty($initializer);
$initialized = new PhpProperty();
$initialized->setName($this->prefix.'initialized');
$initialized->setDefaultValue(false);
$initialized->setVisibility(PhpProperty::VISIBILITY_PRIVATE);
$class->setProperty($initialized);
$initializerSetter = new PhpMethod();
$initializerSetter->setName($this->prefix.'setLazyInitializer');
$initializerSetter->setBody('$this->'.$this->prefix.'lazyInitializer = $initializer;');
$parameter = new PhpParameter();
$parameter->setName('initializer');
$parameter->setType('\CG\Proxy\LazyInitializerInterface');
$initializerSetter->addParameter($parameter);
$class->setMethod($initializerSetter);
$this->addMethods($class, $methods);
$initializingMethod = new PhpMethod();
$initializingMethod->setName($this->prefix.'initialize');
$initializingMethod->setVisibility(PhpMethod::VISIBILITY_PRIVATE);
$initializingMethod->setBody(
$this->writer
->reset()
->writeln('if (null === $this->'.$this->prefix.'lazyInitializer) {')
->indent()
->writeln('throw new \RuntimeException("'.$this->prefix.'setLazyInitializer() must be called prior to any other public method on this object.");')
->outdent()
->write("}\n\n")
->writeln('$this->'.$this->prefix.'lazyInitializer->initializeObject($this);')
->writeln('$this->'.$this->prefix.'initialized = true;')
->getContent()
);
$class->setMethod($initializingMethod);
}
private function addMethods(PhpClass $class, array $methods)
{
foreach ($methods as $method) {
$initializingCode = 'if (false === $this->'.$this->prefix.'initialized) {'."\n"
.' $this->'.$this->prefix.'initialize();'."\n"
.'}';
if ($class->hasMethod($method->name)) {
$genMethod = $class->getMethod($method->name);
$genMethod->setBody(
$initializingCode."\n"
.$genMethod->getBody()
);
continue;
}
$genMethod = PhpMethod::fromReflection($method);
$genMethod->setBody(
$initializingCode."\n\n"
.'return '.GeneratorUtils::callMethod($method).';'
);
$class->setMethod($genMethod);
}
}
}

View 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\Proxy;
/**
* Lazy Initializer.
*
* Implementations of this interface are responsible for lazily initializing
* object instances.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
interface LazyInitializerInterface
{
/**
* Initializes the passed object.
*
* @param object $object
* @return void
*/
function initializeObject($object);
}

View File

@@ -0,0 +1,42 @@
<?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\Proxy;
/**
* Interface for Method Interceptors.
*
* Implementations of this interface can execute custom code before, and after the
* invocation of the actual method. In addition, they can also catch, or throw
* exceptions, modify the return value, or modify the arguments.
*
* This is also known as around advice in AOP terminology.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
interface MethodInterceptorInterface
{
/**
* Called when intercepting a method call.
*
* @param MethodInvocation $invocation
* @return mixed the return value for the method invocation
* @throws \Exception may throw any exception
*/
function intercept(MethodInvocation $invocation);
}

View File

@@ -0,0 +1,77 @@
<?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\Proxy;
/**
* Represents a method invocation.
*
* This object contains information for the method invocation, such as the object
* on which the method is invoked, and the arguments that are passed to the method.
*
* Before the actual method is called, first all the interceptors must call the
* proceed() method on this class.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class MethodInvocation
{
public $reflection;
public $object;
public $arguments;
private $interceptors;
private $pointer;
public function __construct(\ReflectionMethod $reflection, $object, array $arguments, array $interceptors)
{
$this->reflection = $reflection;
$this->object = $object;
$this->arguments = $arguments;
$this->interceptors = $interceptors;
$this->pointer = 0;
}
/**
* Proceeds down the call-chain and eventually calls the original method.
*
* @return mixed
*/
public function proceed()
{
if (isset($this->interceptors[$this->pointer])) {
return $this->interceptors[$this->pointer++]->intercept($this);
}
$this->reflection->setAccessible(true);
return $this->reflection->invokeArgs($this->object, $this->arguments);
}
/**
* Returns a string representation of the method.
*
* This is intended for debugging purposes only.
*
* @return string
*/
public function __toString()
{
return sprintf('%s::%s', $this->reflection->class, $this->reflection->name);
}
}

View File

@@ -0,0 +1,48 @@
<?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\Proxy;
class RegexInterceptionLoader implements InterceptorLoaderInterface
{
private $interceptors;
public function __construct(array $interceptors = array())
{
$this->interceptors = $interceptors;
}
public function loadInterceptors(\ReflectionMethod $method)
{
$signature = $method->class.'::'.$method->name;
$matchingInterceptors = array();
foreach ($this->interceptors as $pattern => $interceptor) {
if (preg_match('#'.$pattern.'#', $signature)) {
$matchingInterceptors[] = $this->initializeInterceptor($interceptor);
}
}
return $matchingInterceptors;
}
protected function initializeInterceptor($interceptor)
{
return $interceptor;
}
}

8
vendor/jms/cg/src/CG/Version.php vendored Normal file
View File

@@ -0,0 +1,8 @@
<?php
namespace CG;
class Version
{
const VERSION = '1.0.0';
}