Skip to content

Commit

Permalink
Merge pull request #122 from Ocramius/hotfix/issue-#116-public-proper…
Browse files Browse the repository at this point in the history
…ties-defaults

Adding failing test for #116 - public properties defaults are ignored
  • Loading branch information
Ocramius committed Nov 29, 2013
2 parents 146c677 + b85dc27 commit 3f62454
Show file tree
Hide file tree
Showing 63 changed files with 661 additions and 195 deletions.
10 changes: 5 additions & 5 deletions src/ProxyManager/Factory/AbstractBaseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ abstract class AbstractBaseFactory
* @var string[]
*/
protected $generatedClasses = array();

/**
* @var \ProxyManager\ProxyGenerator\LazyLoadingValueHolderGenerator
*/
Expand All @@ -61,10 +61,10 @@ public function __construct(Configuration $configuration = null)
// localizing some properties for performance
$this->inflector = $this->configuration->getClassNameInflector();
}

/**
* Generate a proxy from a class name
* @param string $className
* @param string $className
* @return string proxy class name
*/
protected function generateProxy($className)
Expand All @@ -86,10 +86,10 @@ protected function generateProxy($className)
$this->configuration->getGeneratorStrategy()->generate($phpClass);
$this->configuration->getProxyAutoloader()->__invoke($proxyClassName);
}

return $proxyClassName;
}

/**
* @return \ProxyManager\ProxyGenerator\ProxyGeneratorInterface
*/
Expand Down
2 changes: 1 addition & 1 deletion src/ProxyManager/Factory/AbstractLazyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ abstract class AbstractLazyFactory extends AbstractBaseFactory
public function createProxy($className, Closure $initializer)
{
$proxyClassName = $this->generateProxy($className);

return new $proxyClassName($initializer);
}
}
4 changes: 2 additions & 2 deletions src/ProxyManager/Factory/NullObjectFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
class NullObjectFactory extends AbstractBaseFactory
{
/**
* @param object $instanceOrClassName the object to be wrapped or interface to transform to null object
* @param object $instanceOrClassName the object to be wrapped or interface to transform to null object
*
* @return \ProxyManager\Proxy\NullobjectInterface
*/
Expand All @@ -40,7 +40,7 @@ public function createProxy($instanceOrClassName)

return new $proxyClassName();
}

/**
* {@inheritDoc}
*/
Expand Down
8 changes: 4 additions & 4 deletions src/ProxyManager/Factory/RemoteObject/Adapter/BaseAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ abstract class BaseAdapter implements AdapterInterface
* @var \Zend\Server\Client
*/
protected $client;

/**
* Service name mapping
*
Expand All @@ -54,18 +54,18 @@ public function __construct(Client $client, array $map = array())
$this->client = $client;
$this->map = $map;
}

/**
* {@inheritDoc}
*/
public function call($wrappedClass, $method, array $params = array())
{
$serviceName = $this->getServiceName($wrappedClass, $method);

if (isset($this->map[$serviceName])) {
$serviceName = $this->map[$serviceName];
}

return $this->client->call($serviceName, $params);
}

Expand Down
4 changes: 2 additions & 2 deletions src/ProxyManager/Factory/RemoteObject/AdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ interface AdapterInterface
{
/**
* Call remote object
*
*
* @param string $wrappedClass
* @param string $method
* @param array $params
* @param array $params
*/
public function call($wrappedClass, $method, array $params = array());
}
6 changes: 3 additions & 3 deletions src/ProxyManager/Factory/RemoteObjectFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ public function __construct(AdapterInterface $adapter, Configuration $configurat

/**
* @param string|object $instanceOrClassName
*
*
* @return \ProxyManager\Proxy\RemoteObjectInterface
*/
public function createProxy($instanceOrClassName)
{
$className = is_object($instanceOrClassName) ? get_class($instanceOrClassName) : $instanceOrClassName;
$proxyClassName = $this->generateProxy($className);

return new $proxyClassName($this->adapter);
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
class MagicGet extends MagicMethodGenerator
{
/**
* @param ReflectionClass $originalClass
* @param PropertyGenerator $prefixInterceptors
* @param PropertyGenerator $suffixInterceptors
* @param ReflectionClass $originalClass
* @param PropertyGenerator $prefixInterceptors
* @param PropertyGenerator $suffixInterceptors
*/
public function __construct(
ReflectionClass $originalClass,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

use ProxyManager\Generator\MagicMethodGenerator;
use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\Util\InterceptorGenerator;
use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\PublicPropertiesMap;
use ProxyManager\ProxyGenerator\Util\PublicScopeSimulator;
use ReflectionClass;
use ProxyManager\Generator\ParameterGenerator;
Expand All @@ -35,9 +34,9 @@
class MagicIsset extends MagicMethodGenerator
{
/**
* @param ReflectionClass $originalClass
* @param PropertyGenerator $prefixInterceptors
* @param PropertyGenerator $suffixInterceptors
* @param ReflectionClass $originalClass
* @param PropertyGenerator $prefixInterceptors
* @param PropertyGenerator $suffixInterceptors
*/
public function __construct(
ReflectionClass $originalClass,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

use ProxyManager\Generator\MagicMethodGenerator;
use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\Util\InterceptorGenerator;
use ProxyManager\ProxyGenerator\PropertyGenerator\PublicPropertiesMap;
use ProxyManager\ProxyGenerator\Util\PublicScopeSimulator;
use ReflectionClass;
use ProxyManager\Generator\ParameterGenerator;
Expand All @@ -35,9 +34,9 @@
class MagicUnset extends MagicMethodGenerator
{
/**
* @param ReflectionClass $originalClass
* @param PropertyGenerator $prefixInterceptors
* @param PropertyGenerator $suffixInterceptors
* @param ReflectionClass $originalClass
* @param PropertyGenerator $prefixInterceptors
* @param PropertyGenerator $suffixInterceptors
*/
public function __construct(
ReflectionClass $originalClass,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\MagicSleep;
use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\MagicUnset;
use ProxyManager\ProxyGenerator\PropertyGenerator\PublicPropertiesMap;

use ProxyManager\ProxyGenerator\Util\ProxiedMethodsFilter;
use ReflectionClass;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@

use ProxyManager\ProxyGenerator\ValueHolder\MethodGenerator\MagicSleep;
use ReflectionClass;
use ReflectionMethod;
use Zend\Code\Generator\ClassGenerator;
use Zend\Code\Reflection\MethodReflection;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?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 ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator;

use ProxyManager\Generator\MethodGenerator;
use ProxyManager\Generator\ParameterGenerator;
use ProxyManager\Generator\Util\UniqueIdentifierGenerator;
use Zend\Code\Generator\PropertyGenerator;

/**
* Implementation for {@see \ProxyManager\Proxy\LazyLoadingInterface::isProxyInitialized}
* for lazy loading value holder objects
*
* @author Marco Pivetta <[email protected]>
* @license MIT
*/
class CallInitializer extends MethodGenerator
{
/**
* Constructor
*/
public function __construct(
PropertyGenerator $initializerProperty,
PropertyGenerator $publicPropertiesDefaults,
PropertyGenerator $initializationTracker
) {
parent::__construct(UniqueIdentifierGenerator::getIdentifier('callInitializer'));
$this->setDocblock("Triggers initialization logic for this ghost object");

$this->setParameters(array(
new ParameterGenerator('methodName'),
new ParameterGenerator('parameters', 'array'),
));

$this->setVisibility(static::VISIBILITY_PRIVATE);

$initializer = $initializerProperty->getName();
$initialization = $initializationTracker->getName();

$this->setBody(
'if ($this->' . $initialization . ' || ! $this->' . $initializer . ') {' . "\n return;\n}\n\n"
. "\$this->" . $initialization . " = true;\n\n"
. "foreach (self::\$" . $publicPropertiesDefaults->getName() . " as \$key => \$default) {\n"
. " \$this->\$key = \$default;\n"
. "}\n\n"
. '$this->' . $initializer . '->__invoke'
. '($this, $methodName, $parameters, $this->' . $initializer . ');' . "\n\n"
. "\$this->" . $initialization . " = false;"
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

use ProxyManager\Generator\MethodGenerator;
use Zend\Code\Generator\PropertyGenerator;
use Zend\Code\Generator\MethodGenerator as ZendMethodGenerator;

/**
* Implementation for {@see \ProxyManager\Proxy\LazyLoadingInterface::initializeProxy}
Expand All @@ -33,16 +34,14 @@ class InitializeProxy extends MethodGenerator
/**
* Constructor
*/
public function __construct(PropertyGenerator $initializerProperty)
public function __construct(PropertyGenerator $initializerProperty, ZendMethodGenerator $callInitializer)
{
parent::__construct('initializeProxy');
$this->setDocblock('{@inheritDoc}');

$initializer = $initializerProperty->getName();

$this->setBody(
'return $this->' . $initializer . ' && $this->' . $initializer
. '->__invoke($this, \'initializeProxy\', array(), $this->' . $initializer . ');'
'return $this->' . $initializerProperty->getName() . ' && $this->' . $callInitializer->getName()
. '(\'initializeProxy\', array());'
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

use ProxyManager\Generator\MethodGenerator;
use Zend\Code\Generator\PropertyGenerator;
use Zend\Code\Generator\MethodGenerator as ZendMethodGenerator;
use Zend\Code\Reflection\MethodReflection;

/**
Expand All @@ -33,14 +34,17 @@ class LazyLoadingMethodInterceptor extends MethodGenerator
/**
* @param \Zend\Code\Reflection\MethodReflection $originalMethod
* @param \Zend\Code\Generator\PropertyGenerator $initializerProperty
* @param \Zend\Code\Generator\MethodGenerator $callInitializer
*
* @return LazyLoadingMethodInterceptor|static
*/
public static function generateMethod(MethodReflection $originalMethod, PropertyGenerator $initializerProperty)
{
public static function generateMethod(
MethodReflection $originalMethod,
PropertyGenerator $initializerProperty,
ZendMethodGenerator $callInitializer
) {
/* @var $method self */
$method = static::fromReflection($originalMethod);
$initializerName = $initializerProperty->getName();
$parameters = $originalMethod->getParameters();
$methodName = $originalMethod->getName();
$initializerParams = array();
Expand All @@ -53,10 +57,10 @@ public static function generateMethod(MethodReflection $originalMethod, Property
}

$method->setBody(
'$this->' . $initializerName
. ' && $this->' . $initializerName
. '->__invoke($this, ' . var_export($methodName, true)
. ', array(' . implode(', ', $initializerParams) . '), $this->' . $initializerName . ");\n\n"
'$this->' . $initializerProperty->getName()
. ' && $this->' . $callInitializer->getName()
. '(' . var_export($methodName, true)
. ', array(' . implode(', ', $initializerParams) . "));\n\n"
. 'return parent::'
. $methodName . '(' . implode(', ', $forwardedParams) . ');'
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

use ProxyManager\Generator\MagicMethodGenerator;
use ReflectionClass;
use Zend\Code\Generator\MethodGenerator;
use Zend\Code\Generator\PropertyGenerator;

/**
Expand All @@ -33,15 +34,16 @@ class MagicClone extends MagicMethodGenerator
/**
* Constructor
*/
public function __construct(ReflectionClass $originalClass, PropertyGenerator $initializerProperty)
{
public function __construct(
ReflectionClass $originalClass,
PropertyGenerator $initializerProperty,
MethodGenerator $callInitializer
) {
parent::__construct($originalClass, '__clone');

$initializer = $initializerProperty->getName();

$this->setBody(
'$this->' . $initializer . ' && $this->' . $initializer
. '->__invoke($this, \'__clone\', array(), $this->' . $initializer . ');'
'$this->' . $initializerProperty->getName() . ' && $this->' . $callInitializer->getName()
. '(\'__clone\', array());'
. ($originalClass->hasMethod('__clone') ? "\n\nparent::__clone();" : '')
);
}
Expand Down
Loading

0 comments on commit 3f62454

Please sign in to comment.