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 ...@@ -46,9 +46,11 @@ class ProxyClassGenerator
public function __construct(EntityManager $em, $cacheDir = null) public function __construct(EntityManager $em, $cacheDir = null)
{ {
$this->_em = $em; $this->_em = $em;
if ($cacheDir === null) { if ($cacheDir === null) {
$cacheDir = sys_get_temp_dir(); $cacheDir = sys_get_temp_dir();
} }
$this->_cacheDir = rtrim($cacheDir, '/') . '/'; $this->_cacheDir = rtrim($cacheDir, '/') . '/';
} }
...@@ -64,9 +66,11 @@ class ProxyClassGenerator ...@@ -64,9 +66,11 @@ class ProxyClassGenerator
{ {
$class = $this->_em->getClassMetadata($className); $class = $this->_em->getClassMetadata($className);
$proxyClassName = str_replace('\\', '_', $className) . 'RProxy'; $proxyClassName = str_replace('\\', '_', $className) . 'RProxy';
if (!class_exists($proxyClassName, false)) {
if ( ! class_exists($proxyClassName, false)) {
$this->_em->getMetadataFactory()->setMetadataFor(self::$_ns . $proxyClassName, $class); $this->_em->getMetadataFactory()->setMetadataFor(self::$_ns . $proxyClassName, $class);
$fileName = $this->_cacheDir . $proxyClassName . '.g.php'; $fileName = $this->_cacheDir . $proxyClassName . '.g.php';
if (file_exists($fileName)) { if (file_exists($fileName)) {
require $fileName; require $fileName;
$proxyClassName = '\\' . self::$_ns . $proxyClassName; $proxyClassName = '\\' . self::$_ns . $proxyClassName;
...@@ -90,56 +94,76 @@ class ProxyClassGenerator ...@@ -90,56 +94,76 @@ class ProxyClassGenerator
file_put_contents($fileName, $file); file_put_contents($fileName, $file);
require $fileName; require $fileName;
$proxyClassName = '\\' . self::$_ns . $proxyClassName; $proxyClassName = '\\' . self::$_ns . $proxyClassName;
return $proxyClassName; return $proxyClassName;
} }
protected function _generateMethods(ClassMetadata $class) protected function _generateMethods(ClassMetadata $class)
{ {
$methods = ''; $methods = '';
foreach ($class->reflClass->getMethods() as $method) { foreach ($class->reflClass->getMethods() as $method) {
if ($method->getName() == '__construct') { if ($method->getName() == '__construct') {
continue; continue;
} }
if ($method->isPublic() && ! $method->isFinal()) { if ($method->isPublic() && ! $method->isFinal()) {
$methods .= PHP_EOL . 'public function ' . $method->getName() . '('; $methods .= PHP_EOL . 'public function ' . $method->getName() . '(';
$firstParam = true; $firstParam = true;
$parameterString = ''; $parameterString = $argumentString = '';
foreach ($method->getParameters() as $param) { foreach ($method->getParameters() as $param) {
if ($firstParam) { if ($firstParam) {
$firstParam = false; $firstParam = false;
} else { } else {
$parameterString .= ', '; $parameterString .= ', ';
$argumentString .= ', ';
}
// We need to pick the type hint class too
if (($paramClass = $param->getClass()) !== null) {
$parameterString .= '\\' . $paramClass->getName() . ' ';
} }
$parameterString .= '$' . $param->getName(); $parameterString .= '$' . $param->getName();
$argumentString .= '$' . $param->getName();
} }
$methods .= $parameterString . ') {' . PHP_EOL; $methods .= $parameterString . ') {' . PHP_EOL;
$methods .= '$this->_load();' . PHP_EOL; $methods .= '$this->_load();' . PHP_EOL;
$methods .= 'return parent::' . $method->getName() . '(' . $parameterString . ');'; $methods .= 'return parent::' . $method->getName() . '(' . $argumentString . ');';
$methods .= '}' . PHP_EOL; $methods .= '}' . PHP_EOL;
} }
} }
return $methods; return $methods;
} }
public function _generateSleep(ClassMetadata $class) public function _generateSleep(ClassMetadata $class)
{ {
$sleepImpl = ''; $sleepImpl = '';
if ($class->reflClass->hasMethod('__sleep')) { if ($class->reflClass->hasMethod('__sleep')) {
$sleepImpl .= 'return parent::__sleep();'; $sleepImpl .= 'return parent::__sleep();';
} else { } else {
$sleepImpl .= 'return array('; $sleepImpl .= 'return array(';
$first = true; $first = true;
foreach ($class->getReflectionProperties() as $name => $prop) { foreach ($class->getReflectionProperties() as $name => $prop) {
if ($first) { if ($first) {
$first = false; $first = false;
} else { } else {
$sleepImpl .= ', '; $sleepImpl .= ', ';
} }
$sleepImpl .= "'" . $name . "'"; $sleepImpl .= "'" . $name . "'";
} }
$sleepImpl .= ');'; $sleepImpl .= ');';
} }
return $sleepImpl; return $sleepImpl;
} }
...@@ -157,19 +181,23 @@ class ProxyClassGenerator ...@@ -157,19 +181,23 @@ class ProxyClassGenerator
$file = self::$_assocProxyClassTemplate; $file = self::$_assocProxyClassTemplate;
$methods = ''; $methods = '';
foreach ($class->reflClass->getMethods() as $method) { foreach ($class->reflClass->getMethods() as $method) {
if ($method->isPublic() && ! $method->isFinal()) { if ($method->isPublic() && ! $method->isFinal()) {
$methods .= PHP_EOL . 'public function ' . $method->getName() . '('; $methods .= PHP_EOL . 'public function ' . $method->getName() . '(';
$firstParam = true; $firstParam = true;
$parameterString = ''; $parameterString = '';
foreach ($method->getParameters() as $param) { foreach ($method->getParameters() as $param) {
if ($firstParam) { if ($firstParam) {
$firstParam = false; $firstParam = false;
} else { } else {
$parameterString .= ', '; $parameterString .= ', ';
} }
$parameterString .= '$' . $param->getName(); $parameterString .= '$' . $param->getName();
} }
$methods .= $parameterString . ') {' . PHP_EOL; $methods .= $parameterString . ') {' . PHP_EOL;
$methods .= '$this->_load();' . PHP_EOL; $methods .= '$this->_load();' . PHP_EOL;
$methods .= 'return parent::' . $method->getName() . '(' . $parameterString . ');'; $methods .= 'return parent::' . $method->getName() . '(' . $parameterString . ');';
...@@ -178,19 +206,23 @@ class ProxyClassGenerator ...@@ -178,19 +206,23 @@ class ProxyClassGenerator
} }
$sleepImpl = ''; $sleepImpl = '';
if ($class->reflClass->hasMethod('__sleep')) { if ($class->reflClass->hasMethod('__sleep')) {
$sleepImpl .= 'return parent::__sleep();'; $sleepImpl .= 'return parent::__sleep();';
} else { } else {
$sleepImpl .= 'return array('; $sleepImpl .= 'return array(';
$first = true; $first = true;
foreach ($class->getReflectionProperties() as $name => $prop) { foreach ($class->getReflectionProperties() as $name => $prop) {
if ($first) { if ($first) {
$first = false; $first = false;
} else { } else {
$sleepImpl .= ', '; $sleepImpl .= ', ';
} }
$sleepImpl .= "'" . $name . "'"; $sleepImpl .= "'" . $name . "'";
} }
$sleepImpl .= ');'; $sleepImpl .= ');';
} }
...@@ -205,6 +237,7 @@ class ProxyClassGenerator ...@@ -205,6 +237,7 @@ class ProxyClassGenerator
$file = str_replace($placeholders, $replacements, $file); $file = str_replace($placeholders, $replacements, $file);
file_put_contents($fileName, $file); file_put_contents($fileName, $file);
return $fileName; return $fileName;
} }
......
...@@ -47,7 +47,7 @@ class Parser ...@@ -47,7 +47,7 @@ class Parser
'trim' => 'Doctrine\ORM\Query\AST\Functions\TrimFunction', 'trim' => 'Doctrine\ORM\Query\AST\Functions\TrimFunction',
'lower' => 'Doctrine\ORM\Query\AST\Functions\LowerFunction', 'lower' => 'Doctrine\ORM\Query\AST\Functions\LowerFunction',
'upper' => 'Doctrine\ORM\Query\AST\Functions\UpperFunction' 'upper' => 'Doctrine\ORM\Query\AST\Functions\UpperFunction'
); );
/** Maps registered numeric function names to class names. */ /** Maps registered numeric function names to class names. */
private static $_NUMERIC_FUNCTIONS = array( private static $_NUMERIC_FUNCTIONS = array(
...@@ -256,7 +256,7 @@ class Parser ...@@ -256,7 +256,7 @@ class Parser
$message .= "'{$this->_lexer->lookahead['value']}'"; $message .= "'{$this->_lexer->lookahead['value']}'";
} }
throw DoctrineException::updateMe($message); throw QueryException::syntaxError($message);
} }
/** /**
...@@ -280,7 +280,7 @@ class Parser ...@@ -280,7 +280,7 @@ class Parser
$message = 'line 0, col ' . (isset($token['position']) ? $token['position'] : '-1') $message = 'line 0, col ' . (isset($token['position']) ? $token['position'] : '-1')
. " near '" . substr($dql, $token['position'], $length) . "'): Error: " . $message; . " near '" . substr($dql, $token['position'], $length) . "'): Error: " . $message;
throw DoctrineException::updateMe($message); throw QueryException::semanticalError($message);
} }
/** /**
...@@ -352,6 +352,7 @@ class Parser ...@@ -352,6 +352,7 @@ class Parser
default: default:
$this->syntaxError('SELECT, UPDATE or DELETE'); $this->syntaxError('SELECT, UPDATE or DELETE');
break;
} }
} }
...@@ -405,19 +406,19 @@ class Parser ...@@ -405,19 +406,19 @@ class Parser
switch ($expr->getType()) { switch ($expr->getType()) {
case AST\PathExpression::TYPE_STATE_FIELD: case AST\PathExpression::TYPE_STATE_FIELD:
$this->_validateStateFieldPathExpression($expr); $this->_validateStateFieldPathExpression($expr);
break; break;
case AST\PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION: case AST\PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION:
$this->_validateSingleValuedAssociationPathExpression($expr); $this->_validateSingleValuedAssociationPathExpression($expr);
break; break;
case AST\PathExpression::TYPE_COLLECTION_VALUED_ASSOCIATION: case AST\PathExpression::TYPE_COLLECTION_VALUED_ASSOCIATION:
$this->_validateCollectionValuedAssociationPathExpression($expr); $this->_validateCollectionValuedAssociationPathExpression($expr);
break; break;
default: default:
$this->semanticalError('Encountered invalid PathExpression.'); $this->semanticalError('Encountered invalid PathExpression.');
break; break;
} }
} }
} }
......
<?php <?php
/* /*
* To change this template, choose Tools | Templates * $Id$
* and open the template in the editor. *
* 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; namespace Doctrine\ORM\Query;
...@@ -9,6 +24,23 @@ namespace Doctrine\ORM\Query; ...@@ -9,6 +24,23 @@ namespace Doctrine\ORM\Query;
/** /**
* Description of QueryException * 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 {} class QueryException extends \Doctrine\Common\DoctrineException
\ No newline at end of file {
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