Skip to content

Commit

Permalink
Merge pull request #189 from malukenho/bugfix/extends-private-magic-m…
Browse files Browse the repository at this point in the history
…ethods

Bugfix on inherit private magic methods
  • Loading branch information
Ocramius committed Oct 23, 2014
2 parents 10a871f + 05fd973 commit dd6153d
Show file tree
Hide file tree
Showing 11 changed files with 442 additions and 162 deletions.
53 changes: 53 additions & 0 deletions src/ProxyManager/Generator/Util/ClassGeneratorUtils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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\Generator\Util;

use ReflectionClass;
use Zend\Code\Generator\MethodGenerator;
use Zend\Code\Generator\ClassGenerator as GeneratorClass;

/**
* Util class to help to generate code
*
* @author Jefersson Nathan <[email protected]>
* @license MIT
*/
final class ClassGeneratorUtils
{
/**
* @param ReflectionClass $originalClass
* @param GeneratorClass $classGenerator
* @param MethodGenerator $generatedMethod
*
* @return void|false
*/
public static function addMethodIfNotFinal(
ReflectionClass $originalClass,
GeneratorClass $classGenerator,
MethodGenerator $generatedMethod
) {
$methodName = $generatedMethod->getName();

if ($originalClass->hasMethod($methodName) && $originalClass->getMethod($methodName)->isFinal()) {
return false;
}

$classGenerator->addMethodFromGenerator($generatedMethod);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
namespace ProxyManager\ProxyGenerator;

use ProxyManager\Exception\InvalidProxiedClassException;
use ProxyManager\Generator\Util\ClassGeneratorUtils;
use ProxyManager\ProxyGenerator\AccessInterceptor\MethodGenerator\SetMethodPrefixInterceptor;
use ProxyManager\ProxyGenerator\AccessInterceptor\MethodGenerator\SetMethodSuffixInterceptor;
use ProxyManager\ProxyGenerator\AccessInterceptor\PropertyGenerator\MethodPrefixInterceptors;
Expand All @@ -32,7 +33,9 @@
use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\MagicUnset;
use ProxyManager\ProxyGenerator\Util\ProxiedMethodsFilter;
use ReflectionClass;
use ReflectionMethod;
use Zend\Code\Generator\ClassGenerator;
use Zend\Code\Generator\MethodGenerator;
use Zend\Code\Reflection\MethodReflection;

/**
Expand Down Expand Up @@ -60,43 +63,36 @@ public function generate(ReflectionClass $originalClass, ClassGenerator $classGe
$classGenerator->addPropertyFromGenerator($prefixInterceptors = new MethodPrefixInterceptors());
$classGenerator->addPropertyFromGenerator($suffixInterceptors = new MethodPrefixInterceptors());

$methods = ProxiedMethodsFilter::getProxiedMethods(
$originalClass,
array('__get', '__set', '__isset', '__unset', '__clone', '__sleep')
);

foreach ($methods as $method) {
$classGenerator->addMethodFromGenerator(
InterceptedMethod::generateMethod(
new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()),
$prefixInterceptors,
$suffixInterceptors
array_map(
function (MethodGenerator $generatedMethod) use ($originalClass, $classGenerator) {
ClassGeneratorUtils::addMethodIfNotFinal($originalClass, $classGenerator, $generatedMethod);
},
array_merge(
array_map(
function (ReflectionMethod $method) use ($prefixInterceptors, $suffixInterceptors) {
return InterceptedMethod::generateMethod(
new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()),
$prefixInterceptors,
$suffixInterceptors
);
},
ProxiedMethodsFilter::getProxiedMethods(
$originalClass,
array('__get', '__set', '__isset', '__unset', '__clone', '__sleep')
)
),
array(
new Constructor($originalClass, $prefixInterceptors, $suffixInterceptors),
new SetMethodPrefixInterceptor($prefixInterceptors),
new SetMethodSuffixInterceptor($suffixInterceptors),
new MagicGet($originalClass, $prefixInterceptors, $suffixInterceptors),
new MagicSet($originalClass, $prefixInterceptors, $suffixInterceptors),
new MagicIsset($originalClass, $prefixInterceptors, $suffixInterceptors),
new MagicUnset($originalClass, $prefixInterceptors, $suffixInterceptors),
new MagicSleep($originalClass, $prefixInterceptors, $suffixInterceptors),
new MagicClone($originalClass, $prefixInterceptors, $suffixInterceptors),
)
);
}

$classGenerator->addMethodFromGenerator(
new Constructor($originalClass, $prefixInterceptors, $suffixInterceptors)
);
$classGenerator->addMethodFromGenerator(new SetMethodPrefixInterceptor($prefixInterceptors));
$classGenerator->addMethodFromGenerator(new SetMethodSuffixInterceptor($suffixInterceptors));
$classGenerator->addMethodFromGenerator(
new MagicGet($originalClass, $prefixInterceptors, $suffixInterceptors)
);
$classGenerator->addMethodFromGenerator(
new MagicSet($originalClass, $prefixInterceptors, $suffixInterceptors)
);
$classGenerator->addMethodFromGenerator(
new MagicIsset($originalClass, $prefixInterceptors, $suffixInterceptors)
);
$classGenerator->addMethodFromGenerator(
new MagicUnset($originalClass, $prefixInterceptors, $suffixInterceptors)
);
$classGenerator->addMethodFromGenerator(
new MagicSleep($originalClass, $prefixInterceptors, $suffixInterceptors)
);
$classGenerator->addMethodFromGenerator(
new MagicClone($originalClass, $prefixInterceptors, $suffixInterceptors)
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

namespace ProxyManager\ProxyGenerator;

use ProxyManager\Generator\Util\ClassGeneratorUtils;
use ProxyManager\ProxyGenerator\AccessInterceptor\MethodGenerator\MagicWakeup;
use ProxyManager\ProxyGenerator\AccessInterceptor\MethodGenerator\SetMethodPrefixInterceptor;
use ProxyManager\ProxyGenerator\AccessInterceptor\MethodGenerator\SetMethodSuffixInterceptor;
Expand All @@ -36,7 +37,9 @@
use ProxyManager\ProxyGenerator\ValueHolder\MethodGenerator\GetWrappedValueHolderValue;
use ProxyManager\ProxyGenerator\ValueHolder\MethodGenerator\MagicSleep;
use ReflectionClass;
use ReflectionMethod;
use Zend\Code\Generator\ClassGenerator;
use Zend\Code\Generator\MethodGenerator;
use Zend\Code\Reflection\MethodReflection;

/**
Expand Down Expand Up @@ -73,54 +76,60 @@ public function generate(ReflectionClass $originalClass, ClassGenerator $classGe
$classGenerator->addPropertyFromGenerator($suffixInterceptors = new MethodSuffixInterceptors());
$classGenerator->addPropertyFromGenerator($publicProperties);

foreach (ProxiedMethodsFilter::getProxiedMethods($originalClass) as $method) {
$classGenerator->addMethodFromGenerator(
InterceptedMethod::generateMethod(
new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()),
$valueHolder,
$prefixInterceptors,
$suffixInterceptors
array_map(
function (MethodGenerator $generatedMethod) use ($originalClass, $classGenerator, $valueHolder) {
ClassGeneratorUtils::addMethodIfNotFinal($originalClass, $classGenerator, $generatedMethod);
},
array_merge(
array_map(
function (ReflectionMethod $method) use ($prefixInterceptors, $suffixInterceptors, $valueHolder) {
return InterceptedMethod::generateMethod(
new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()),
$valueHolder,
$prefixInterceptors,
$suffixInterceptors
);
},
ProxiedMethodsFilter::getProxiedMethods($originalClass)
),
array(
new Constructor($originalClass, $valueHolder, $prefixInterceptors, $suffixInterceptors),
new GetWrappedValueHolderValue($valueHolder),
new SetMethodPrefixInterceptor($prefixInterceptors),
new SetMethodSuffixInterceptor($suffixInterceptors),
new MagicGet(
$originalClass,
$valueHolder,
$prefixInterceptors,
$suffixInterceptors,
$publicProperties
),
new MagicSet(
$originalClass,
$valueHolder,
$prefixInterceptors,
$suffixInterceptors,
$publicProperties
),
new MagicIsset(
$originalClass,
$valueHolder,
$prefixInterceptors,
$suffixInterceptors,
$publicProperties
),
new MagicUnset(
$originalClass,
$valueHolder,
$prefixInterceptors,
$suffixInterceptors,
$publicProperties
),
new MagicClone($originalClass, $valueHolder, $prefixInterceptors, $suffixInterceptors),
new MagicSleep($originalClass, $valueHolder),
new MagicWakeup($originalClass, $valueHolder),
)
);
}

$classGenerator->addMethodFromGenerator(
new Constructor($originalClass, $valueHolder, $prefixInterceptors, $suffixInterceptors)
);
$classGenerator->addMethodFromGenerator(
new GetWrappedValueHolderValue($valueHolder)
);
$classGenerator->addMethodFromGenerator(
new SetMethodPrefixInterceptor($prefixInterceptors)
);
$classGenerator->addMethodFromGenerator(
new SetMethodSuffixInterceptor($suffixInterceptors)
);

$classGenerator->addMethodFromGenerator(
new MagicGet($originalClass, $valueHolder, $prefixInterceptors, $suffixInterceptors, $publicProperties)
);

$classGenerator->addMethodFromGenerator(
new MagicSet($originalClass, $valueHolder, $prefixInterceptors, $suffixInterceptors, $publicProperties)
);

$classGenerator->addMethodFromGenerator(
new MagicIsset($originalClass, $valueHolder, $prefixInterceptors, $suffixInterceptors, $publicProperties)
);

$classGenerator->addMethodFromGenerator(
new MagicUnset($originalClass, $valueHolder, $prefixInterceptors, $suffixInterceptors, $publicProperties)
);

$classGenerator->addMethodFromGenerator(
new MagicClone($originalClass, $valueHolder, $prefixInterceptors, $suffixInterceptors)
);
$classGenerator->addMethodFromGenerator(
new MagicSleep($originalClass, $valueHolder)
);
$classGenerator->addMethodFromGenerator(
new MagicWakeup($originalClass, $valueHolder)
)
);
}
}
57 changes: 33 additions & 24 deletions src/ProxyManager/ProxyGenerator/LazyLoadingGhostGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

namespace ProxyManager\ProxyGenerator;

use ProxyManager\Generator\Util\ClassGeneratorUtils;
use ProxyManager\ProxyGenerator\LazyLoading\MethodGenerator\Constructor;
use ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\CallInitializer;
use ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\GetProxyInitializer;
Expand All @@ -37,7 +38,9 @@
use ProxyManager\ProxyGenerator\PropertyGenerator\PublicPropertiesMap;
use ProxyManager\ProxyGenerator\Util\ProxiedMethodsFilter;
use ReflectionClass;
use ReflectionMethod;
use Zend\Code\Generator\ClassGenerator;
use Zend\Code\Generator\MethodGenerator;
use Zend\Code\Reflection\MethodReflection;

/**
Expand Down Expand Up @@ -73,30 +76,36 @@ public function generate(ReflectionClass $originalClass, ClassGenerator $classGe

$init = new CallInitializer($initializer, $publicPropsDefaults, $initializationTracker);

$classGenerator->addMethodFromGenerator($init);

foreach (ProxiedMethodsFilter::getProxiedMethods($originalClass) as $method) {
$classGenerator->addMethodFromGenerator(
LazyLoadingMethodInterceptor::generateMethod(
new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()),
$initializer,
$init
array_map(
function (MethodGenerator $generatedMethod) use ($originalClass, $classGenerator) {
ClassGeneratorUtils::addMethodIfNotFinal($originalClass, $classGenerator, $generatedMethod);
},
array_merge(
array_map(
function (ReflectionMethod $method) use ($initializer, $init) {
return LazyLoadingMethodInterceptor::generateMethod(
new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()),
$initializer,
$init
);
},
ProxiedMethodsFilter::getProxiedMethods($originalClass)
),
array(
$init,
new Constructor($originalClass, $initializer),
new MagicGet($originalClass, $initializer, $init, $publicProperties),
new MagicSet($originalClass, $initializer, $init, $publicProperties),
new MagicIsset($originalClass, $initializer, $init, $publicProperties),
new MagicUnset($originalClass, $initializer, $init, $publicProperties),
new MagicClone($originalClass, $initializer, $init, $publicProperties),
new MagicSleep($originalClass, $initializer, $init, $publicProperties),
new SetProxyInitializer($initializer),
new GetProxyInitializer($initializer),
new InitializeProxy($initializer, $init),
new IsProxyInitialized($initializer),
)
);
}

$classGenerator->addMethodFromGenerator(new Constructor($originalClass, $initializer));

$classGenerator->addMethodFromGenerator(new MagicGet($originalClass, $initializer, $init, $publicProperties));
$classGenerator->addMethodFromGenerator(new MagicSet($originalClass, $initializer, $init, $publicProperties));
$classGenerator->addMethodFromGenerator(new MagicIsset($originalClass, $initializer, $init, $publicProperties));
$classGenerator->addMethodFromGenerator(new MagicUnset($originalClass, $initializer, $init, $publicProperties));
$classGenerator->addMethodFromGenerator(new MagicClone($originalClass, $initializer, $init));
$classGenerator->addMethodFromGenerator(new MagicSleep($originalClass, $initializer, $init));

$classGenerator->addMethodFromGenerator(new SetProxyInitializer($initializer));
$classGenerator->addMethodFromGenerator(new GetProxyInitializer($initializer));
$classGenerator->addMethodFromGenerator(new InitializeProxy($initializer, $init));
$classGenerator->addMethodFromGenerator(new IsProxyInitialized($initializer));
)
);
}
}
Loading

0 comments on commit dd6153d

Please sign in to comment.