Skip to content

Commit

Permalink
Merge pull request #9 from Ocramius/feature/php-parser-visitors
Browse files Browse the repository at this point in the history
WIP - PHP-Parser visitors based code generation
  • Loading branch information
Ocramius committed Jul 20, 2013
2 parents 5ef0dcf + b1372a8 commit 2dffa70
Show file tree
Hide file tree
Showing 71 changed files with 3,507 additions and 931 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"require": {
"php": ">=5.4",
"zendframework/zend-stdlib": "2.*",
"ocramius/proxy-manager": "0.4.*",
"nikic/php-parser": "0.9.*"
},
"require-dev": {
Expand All @@ -33,7 +32,8 @@
},
"autoload": {
"psr-0": {
"GeneratedHydrator\\": "src"
"GeneratedHydrator\\": "src",
"CodeGenerationUtils\\": "src"
}
},
"extra": {
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
>
<testsuite name="GeneratedHydrator tests">
<directory>./tests/GeneratedHydratorTest</directory>
<directory>./tests/CodeGenerationUtilsTest</directory>
</testsuite>
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
Expand Down
69 changes: 69 additions & 0 deletions src/CodeGenerationUtils/Autoloader/Autoloader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/

namespace CodeGenerationUtils\Autoloader;

use CodeGenerationUtils\FileLocator\FileLocatorInterface;
use CodeGenerationUtils\Inflector\ClassNameInflectorInterface;

/**
* {@inheritDoc}
*
* @author Marco Pivetta <ocramius@gmail.com>
* @license MIT
*/
class Autoloader implements AutoloaderInterface
{
/**
* @var \CodeGenerationUtils\FileLocator\FileLocatorInterface
*/
protected $fileLocator;

/**
* @var \CodeGenerationUtils\Inflector\ClassNameInflectorInterface
*/
protected $classNameInflector;

/**
* @param \CodeGenerationUtils\FileLocator\FileLocatorInterface $fileLocator
* @param \CodeGenerationUtils\Inflector\ClassNameInflectorInterface $classNameInflector
*/
public function __construct(FileLocatorInterface $fileLocator, ClassNameInflectorInterface $classNameInflector)
{
$this->fileLocator = $fileLocator;
$this->classNameInflector = $classNameInflector;
}

/**
* {@inheritDoc}
*/
public function __invoke($className)
{
if (class_exists($className, false) || ! $this->classNameInflector->isProxyClassName($className)) {
return false;
}

$file = $this->fileLocator->getProxyFileName($className);

if (! file_exists($file)) {
return false;
}

return (bool) require_once $file;
}
}
37 changes: 37 additions & 0 deletions src/CodeGenerationUtils/Autoloader/AutoloaderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/

namespace CodeGenerationUtils\Autoloader;

/**
* Basic autoloader utilities required to work with generated classes
*
* @author Marco Pivetta <ocramius@gmail.com>
* @license MIT
*/
interface AutoloaderInterface
{
/**
* Callback to allow the object to be handled as autoloader - tries to autoload the given class name
*
* @param string $className
*
* @return bool
*/
public function __invoke($className);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,27 @@
* and is licensed under the MIT license.
*/

namespace GeneratedHydrator\ClassGenerator\Hydrator\MethodGenerator;
namespace CodeGenerationUtils\Exception;

use GeneratedHydrator\Exception\DisabledMethodException;
use ProxyManager\Generator\MethodGenerator;
use BadMethodCallException;

/**
* Method generator for forcefully disabled methods
* Exception for forcefully disabled methods
*
* @author Marco Pivetta <ocramius@gmail.com>
* @license MIT
*/
class DisabledMethod extends MethodGenerator
class DisabledMethodException extends BadMethodCallException implements ExceptionInterface
{
const NAME = __CLASS__;

/**
* {@inheritDoc}
* @param string $method
*
* @return self
*/
public function generate()
public static function disabledMethod($method)
{
$this->setBody('throw \\' . DisabledMethodException::NAME . '::disabledMethod(__METHOD__);');
$this->setDocblock(
"{@inheritDoc}\n\n@internal disabled since this object is not a real proxy\n\n"
. "@throws \\GeneratedHydrator\\Exception\\DisabledMethodException"
);

return parent::generate();
return new self(sprintf('Method "%s" is forcefully disabled', (string) $method));
}
}
29 changes: 29 additions & 0 deletions src/CodeGenerationUtils/Exception/ExceptionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/

namespace CodeGenerationUtils\Exception;

/**
* Base exception class for the proxy manager
*
* @author Marco Pivetta <ocramius@gmail.com>
* @license MIT
*/
interface ExceptionInterface
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/

namespace CodeGenerationUtils\Exception;

use InvalidArgumentException;

/**
* Exception for invalid directories
*
* @author Marco Pivetta <ocramius@gmail.com>
* @license MIT
*/
class InvalidProxyDirectoryException extends InvalidArgumentException implements ExceptionInterface
{
/**
* @param string $directory
*
* @return self
*/
public static function proxyDirectoryNotFound($directory)
{
return new self(sprintf('Provided directory "%s" does not exist', (string) $directory));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,42 @@
* and is licensed under the MIT license.
*/

namespace GeneratedHydratorTest\ClassGenerator\Hydrator\MethodGenerator;
namespace CodeGenerationUtils\FileLocator;

use PHPUnit_Framework_TestCase;
use GeneratedHydrator\ClassGenerator\Hydrator\MethodGenerator\DisabledMethod;
use CodeGenerationUtils\Exception\InvalidProxyDirectoryException;

/**
* Tests for {@see \GeneratedHydrator\ClassGenerator\Hydrator\MethodGenerator\DisabledMethod}
* {@inheritDoc}
*
* @author Marco Pivetta <ocramius@gmail.com>
* @license MIT
*
* @covers \GeneratedHydrator\ClassGenerator\Hydrator\MethodGenerator\DisabledMethod
*/
class DisabledMethodTest extends PHPUnit_Framework_TestCase
class FileLocator implements FileLocatorInterface
{
/**
* @covers \GeneratedHydrator\ClassGenerator\Hydrator\MethodGenerator\DisabledMethod::generate
* @var string
*/
public function testGeneratedStructure()
protected $proxiesDirectory;

/**
* @param string $proxiesDirectory
*
* @throws \CodeGenerationUtils\Exception\InvalidProxyDirectoryException
*/
public function __construct($proxiesDirectory)
{
$disabledMethod = new DisabledMethod('foo');
$this->proxiesDirectory = realpath($proxiesDirectory);

$this->assertStringMatchesFormat(
'%athrow \\GeneratedHydrator\\Exception\\DisabledMethodException::disabledMethod(__METHOD__);%a',
$disabledMethod->generate()
);
if (false === $this->proxiesDirectory) {
throw InvalidProxyDirectoryException::proxyDirectoryNotFound($proxiesDirectory);
}
}

/**
* {@inheritDoc}
*/
public function getProxyFileName($className)
{
return $this->proxiesDirectory . DIRECTORY_SEPARATOR . str_replace('\\', '', $className) . '.php';
}
}
37 changes: 37 additions & 0 deletions src/CodeGenerationUtils/FileLocator/FileLocatorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/

namespace CodeGenerationUtils\FileLocator;

/**
* File locator - enables discovering files where generated classes are located
*
* @author Marco Pivetta <ocramius@gmail.com>
* @license MIT
*/
interface FileLocatorInterface
{
/**
* Retrieves the file name for the given proxy
*
* @param $className
*
* @return mixed
*/
public function getProxyFileName($className);
}
Loading

0 comments on commit 2dffa70

Please sign in to comment.