Commit 8fbee579 authored by guilhermeblanco's avatar guilhermeblanco

[2.0] Fixed 4 issues with ProxyGenerator. It was not considering the type hint...

[2.0] Fixed 4 issues with ProxyGenerator. It was not considering the type hint and it was generating an E_STRICT error with incompatible method declaration. Some cosmetic changes in Query
parent cdc102fc
......@@ -46,9 +46,11 @@ class ProxyClassGenerator
public function __construct(EntityManager $em, $cacheDir = null)
{
$this->_em = $em;
if ($cacheDir === null) {
$cacheDir = sys_get_temp_dir();
}
$this->_cacheDir = rtrim($cacheDir, '/') . '/';
}
......@@ -64,9 +66,11 @@ class ProxyClassGenerator
{
$class = $this->_em->getClassMetadata($className);
$proxyClassName = str_replace('\\', '_', $className) . 'RProxy';
if (!class_exists($proxyClassName, false)) {
if ( ! class_exists($proxyClassName, false)) {
$this->_em->getMetadataFactory()->setMetadataFor(self::$_ns . $proxyClassName, $class);
$fileName = $this->_cacheDir . $proxyClassName . '.g.php';
if (file_exists($fileName)) {
require $fileName;
$proxyClassName = '\\' . self::$_ns . $proxyClassName;
......@@ -90,56 +94,76 @@ class ProxyClassGenerator
file_put_contents($fileName, $file);
require $fileName;
$proxyClassName = '\\' . self::$_ns . $proxyClassName;
return $proxyClassName;
}
protected function _generateMethods(ClassMetadata $class)
{
$methods = '';
foreach ($class->reflClass->getMethods() as $method) {
if ($method->getName() == '__construct') {
continue;
}
if ($method->isPublic() && ! $method->isFinal()) {
$methods .= PHP_EOL . 'public function ' . $method->getName() . '(';
$firstParam = true;
$parameterString = '';
$parameterString = $argumentString = '';
foreach ($method->getParameters() as $param) {
if ($firstParam) {
$firstParam = false;
} else {
$parameterString .= ', ';
$argumentString .= ', ';
}
// We need to pick the type hint class too
if (($paramClass = $param->getClass()) !== null) {
$parameterString .= '\\' . $paramClass->getName() . ' ';
}
$parameterString .= '$' . $param->getName();
$argumentString .= '$' . $param->getName();
}
$methods .= $parameterString . ') {' . PHP_EOL;
$methods .= '$this->_load();' . PHP_EOL;
$methods .= 'return parent::' . $method->getName() . '(' . $parameterString . ');';
$methods .= 'return parent::' . $method->getName() . '(' . $argumentString . ');';
$methods .= '}' . PHP_EOL;
}
}
return $methods;
}
public function _generateSleep(ClassMetadata $class)
{
$sleepImpl = '';
if ($class->reflClass->hasMethod('__sleep')) {
$sleepImpl .= 'return parent::__sleep();';
} else {
$sleepImpl .= 'return array(';
$first = true;
foreach ($class->getReflectionProperties() as $name => $prop) {
if ($first) {
$first = false;
} else {
$sleepImpl .= ', ';
}
$sleepImpl .= "'" . $name . "'";
}
$sleepImpl .= ');';
}
return $sleepImpl;
}
......@@ -157,19 +181,23 @@ class ProxyClassGenerator
$file = self::$_assocProxyClassTemplate;
$methods = '';
foreach ($class->reflClass->getMethods() as $method) {
if ($method->isPublic() && ! $method->isFinal()) {
$methods .= PHP_EOL . 'public function ' . $method->getName() . '(';
$firstParam = true;
$parameterString = '';
foreach ($method->getParameters() as $param) {
if ($firstParam) {
$firstParam = false;
} else {
$parameterString .= ', ';
}
$parameterString .= '$' . $param->getName();
}
$methods .= $parameterString . ') {' . PHP_EOL;
$methods .= '$this->_load();' . PHP_EOL;
$methods .= 'return parent::' . $method->getName() . '(' . $parameterString . ');';
......@@ -178,19 +206,23 @@ class ProxyClassGenerator
}
$sleepImpl = '';
if ($class->reflClass->hasMethod('__sleep')) {
$sleepImpl .= 'return parent::__sleep();';
} else {
$sleepImpl .= 'return array(';
$first = true;
foreach ($class->getReflectionProperties() as $name => $prop) {
if ($first) {
$first = false;
} else {
$sleepImpl .= ', ';
}
$sleepImpl .= "'" . $name . "'";
}
$sleepImpl .= ');';
}
......@@ -205,6 +237,7 @@ class ProxyClassGenerator
$file = str_replace($placeholders, $replacements, $file);
file_put_contents($fileName, $file);
return $fileName;
}
......
......@@ -47,7 +47,7 @@ class Parser
'trim' => 'Doctrine\ORM\Query\AST\Functions\TrimFunction',
'lower' => 'Doctrine\ORM\Query\AST\Functions\LowerFunction',
'upper' => 'Doctrine\ORM\Query\AST\Functions\UpperFunction'
);
);
/** Maps registered numeric function names to class names. */
private static $_NUMERIC_FUNCTIONS = array(
......@@ -256,7 +256,7 @@ class Parser
$message .= "'{$this->_lexer->lookahead['value']}'";
}
throw DoctrineException::updateMe($message);
throw QueryException::syntaxError($message);
}
/**
......@@ -280,7 +280,7 @@ class Parser
$message = 'line 0, col ' . (isset($token['position']) ? $token['position'] : '-1')
. " near '" . substr($dql, $token['position'], $length) . "'): Error: " . $message;
throw DoctrineException::updateMe($message);
throw QueryException::semanticalError($message);
}
/**
......@@ -352,6 +352,7 @@ class Parser
default:
$this->syntaxError('SELECT, UPDATE or DELETE');
break;
}
}
......@@ -405,19 +406,19 @@ class Parser
switch ($expr->getType()) {
case AST\PathExpression::TYPE_STATE_FIELD:
$this->_validateStateFieldPathExpression($expr);
break;
break;
case AST\PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION:
$this->_validateSingleValuedAssociationPathExpression($expr);
break;
break;
case AST\PathExpression::TYPE_COLLECTION_VALUED_ASSOCIATION:
$this->_validateCollectionValuedAssociationPathExpression($expr);
break;
break;
default:
$this->semanticalError('Encountered invalid PathExpression.');
break;
break;
}
}
}
......
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
/*
* $Id$
*
* 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 LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\ORM\Query;
......@@ -9,6 +24,23 @@ namespace Doctrine\ORM\Query;
/**
* Description of QueryException
*
* @author robo
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Roman Borschel <roman@code-factory.org>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://www.doctrine-project.org
* @since 2.0
* @version $Revision$
*/
class QueryException extends \Doctrine\Common\DoctrineException {}
\ No newline at end of file
class QueryException extends \Doctrine\Common\DoctrineException
{
public static function syntaxError($message)
{
return new self('[Syntax Error] ' . $message);
}
public static function semanticalError($message)
{
return new self('[Semantical Error] ' . $message);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment