-
-
Notifications
You must be signed in to change notification settings - Fork 190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Avoid constructor override #175
Merged
Merged
Changes from all commits
Commits
Show all changes
99 commits
Select commit
Hold shift + click to select a range
17c8eb1
Implementing a static constructor to avoid overriding an object's `__…
Ocramius f94ebf1
Using the static constructor in the `AccessInterceptorScopeLocalizer`…
Ocramius ec200ac
Implementing new static constructor in the `AccessInterceptorValueHol…
Ocramius 1ffaf9c
Variable collision fix (`$instance` was a parameter and a local varia…
Ocramius 59049d1
Using the new static proxy constructor where implemented
Ocramius 28d73f0
Avoiding usage of `$this` in static context
Ocramius cf0f7cc
Implementing a static constructor for `AccessInterceptorValueHolder` …
Ocramius bc8b850
Fixed static constructor name (not `__construct`!)
Ocramius 217840e
Adding the static constructor to generated code
Ocramius cfd117c
Static proxy constructor is public and static
Ocramius 5fef9ff
Using the new static proxy constructor when available
Ocramius 199cfd9
Built instance should be returned by the static constructor
Ocramius 9d1d2ac
Built instance should be returned by the static constructor
Ocramius b33467c
Missing semicolon in generated code
Ocramius a1bf5ab
Mock should return the built instance when using the static constructor
Ocramius ea93303
Minor docblock fixes
Ocramius 3166a17
Minor docblock fixes (missing `@return`)
Ocramius c3e9145
Typo fix causing massive codegen changes (`,` replaced by concatenati…
Ocramius 31af1b2
Cannot bind a closure that was built in static context to an object, …
Ocramius bb5a152
On-the-fly generated classes don't need to re-implement method `#stat…
Ocramius 4605d63
All tests should rely on the new static constructor of the access int…
Ocramius f06f1bf
Removing usage of the `__construct` override
Ocramius e93a368
Static constructors do not override any methods
Ocramius 016863f
Implementing static proxy constructor for lazy loading objects
Ocramius d08695d
Using the newly implemented `StaticProxyConstructor` in the lazy load…
Ocramius ee046bc
Using the newly implemented `StaticProxyConstructor` in the lazy load…
Ocramius 90f94d7
`staticProxyConstructor` must be static
Ocramius ee763d3
Implementing `staticProxyConstructor` for null objects
Ocramius f353cca
Variable name mismatch fix
Ocramius 93160fa
Proxy test should only interact with non-static methods
Ocramius b57ad03
Using newly implemented static proxy constructor in codegen of null o…
Ocramius 610ade0
Implementing static constructor for remote objects
Ocramius ae54f41
Using newly introduced static proxy constructor for remote objects in…
Ocramius 5262aa0
Test stubs should implement the `staticProxyConstructor`
Ocramius d9b7709
Fixed broken assertion (was using `stdClass` instead of a proxy imple…
Ocramius 894b752
Added missing `get_class()` call, which was causing a reflection exce…
Ocramius f14d2bb
Forgot a return statement in a static proxy constructor
Ocramius a20a06b
Tests should also use the `staticProxyConstructor` where applicable
Ocramius de9bbdc
Minor cleanups in the remote object factory code
Ocramius f46e45f
Using the `staticProxyConstructor` in factories where applicable
Ocramius 0333fb4
Using the `staticProxyConstructor` in the test suite where applicable
Ocramius 9b61125
Removing last leftovers of direct proxy constructor calls
Ocramius 70d2b47
Removing old constructor override
Ocramius d6c00b3
PHP 5.6 supports reflection-based proxy instantiation for internal cl…
Ocramius b727fdd
No need to extend from anything in the remote object factory tests
Ocramius 3234e82
Optimized imports (duplicate imports were causing fatals)
Ocramius 34a7c0a
Using static constructor where available
Ocramius ad747c8
Simplified test code (avoiding unused assignment)
Ocramius ca8bac4
Fixed line length overflow (limit is 120 chars)
Ocramius 7edac82
Fixed line length overflow (limit is 120 chars)
Ocramius dab3e3a
Loosen requirement for PHP versions: 5.4 is enough to have some synta…
Ocramius 3e91886
Skipping PHP 5.3 builds
Ocramius b5c43de
Fixed breakages in the test suite - was using static constructor on n…
Ocramius c4ba608
Skipping tests about internal PHP classes when in PHP < 5.6
Ocramius 8ecc06f
Simplified static proxy constructor (avoiding unused params
Ocramius b67197f
Implemented missing docblock params
Ocramius b5ab074
Removed unused imports
Ocramius 7b7ff4a
Minor performance optimization: avoiding spawning a reflection class …
Ocramius 85b5b19
Minor performance optimization: avoiding spawning a reflection class …
Ocramius f8e4156
Minor performance optimization: avoiding spawning a reflection class …
Ocramius 7ce16a0
Minor performance optimization: avoiding spawning a reflection class …
Ocramius 8bd50f3
Minor performance optimization: avoiding spawning a reflection class …
Ocramius 1031dea
Coverage for the static proxy constructor implementation
Ocramius 11a215d
Correct location for the `StaticProxyConstructorTest`
Ocramius 4d9a980
Coverage for the `BindProxyProperties` implementation
Ocramius 32b8743
Removing unused import
Ocramius a4ad856
Coverage for the static proxy constructor implementation
Ocramius 7aebe26
Coverage for the static proxy constructor implementation
Ocramius c3499f2
Coverage for the static proxy constructor implementation
Ocramius 0a07281
Removed unused import
Ocramius 2fe62b5
Coverage for the static proxy constructor implementation
Ocramius 0cbebf3
Removed unused import
Ocramius 78d2280
Adding test asset to verify that proxy constructors work as expected …
Ocramius 59e1935
Adding test to verify that proxy constructors work like the proxied c…
Ocramius 7f13fdf
More checks on the test asset being used
Ocramius b9c6e9f
Typo fix (counter expectation was incorrect)
Ocramius eb9de78
Adding getter for `amount` (to be proxied)
Ocramius 11802d0
Testing getter logic on the access interceptor scope localizer logic
Ocramius 76c716c
Expecting access interceptor value holder to behave as if the constru…
Ocramius 85741f6
Adding test asset with a PHP4-style constructors
Ocramius 233b2e2
Removing invalid test asset - PHP4 constructors don't work with names…
Ocramius 596c71d
Implementing codegen assertions for the access interceptor value hold…
Ocramius 78a944f
Reducing coverage annotations clutter
Ocramius fdcb2f4
Implementing constructor generator for access interceptor value holde…
Ocramius cdcf9e9
Using the newly implemented access interceptor value holder construct…
Ocramius 86a8bfa
Implementing tests to cover #115 and #175 in lazy loading ghost logic
Ocramius 0eaf405
Implementing tests to cover #115 and #175 in lazy loading value holde…
Ocramius 52f0e75
Restoring location of incorrectly moved files
Ocramius 6366b00
Moved constructor generator into th more generic `ProxyManager\ProxyG…
Ocramius e8ae065
PHP/HHVM compat should be checked via `method_exists()`, not via vers…
Ocramius 05c9b17
CS fix - whitespace removal
Ocramius 3b996ab
Removed unused import
Ocramius 62812b7
CS fix - docblock alignment
Ocramius 57f78db
CS fix - docblock alignment
Ocramius 89d0519
CS fix - docblock alignment
Ocramius 0284096
CS fix - docblock alignment
Ocramius 10d21fa
Importing used classes, optimizing imports
Ocramius cc610f3
Add overriding constructor to the generated value holder proxy in ord…
Ocramius 15fcde7
`ClassGeneratorUtilsTest` is part of coverage tests
Ocramius File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
language: php | ||
|
||
php: | ||
- 5.3.3 | ||
- 5.3 | ||
- 5.4 | ||
- 5.5 | ||
- 5.6 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
...er/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/BindProxyProperties.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?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\AccessInterceptorScopeLocalizer\MethodGenerator; | ||
|
||
use ProxyManager\Exception\UnsupportedProxiedClassException; | ||
use ProxyManager\Generator\MethodGenerator; | ||
use ProxyManager\Generator\ParameterGenerator; | ||
use ReflectionClass; | ||
use Zend\Code\Generator\PropertyGenerator; | ||
|
||
/** | ||
* The `bindProxyProperties` method implementation for access interceptor scope localizers | ||
* | ||
* @author Marco Pivetta <ocramius@gmail.com> | ||
* @license MIT | ||
*/ | ||
class BindProxyProperties extends MethodGenerator | ||
{ | ||
/** | ||
* Constructor | ||
* | ||
* @param ReflectionClass $originalClass | ||
* @param PropertyGenerator $prefixInterceptors | ||
* @param PropertyGenerator $suffixInterceptors | ||
*/ | ||
public function __construct( | ||
ReflectionClass $originalClass, | ||
PropertyGenerator $prefixInterceptors, | ||
PropertyGenerator $suffixInterceptors | ||
) { | ||
parent::__construct('bindProxyProperties', array(), static::FLAG_PRIVATE); | ||
|
||
$localizedObject = new ParameterGenerator('localizedObject'); | ||
$prefix = new ParameterGenerator('prefixInterceptors'); | ||
$suffix = new ParameterGenerator('suffixInterceptors'); | ||
|
||
$localizedObject->setType($originalClass->getName()); | ||
$prefix->setDefaultValue(array()); | ||
$suffix->setDefaultValue(array()); | ||
$prefix->setType('array'); | ||
$suffix->setType('array'); | ||
|
||
$this->setParameter($localizedObject); | ||
$this->setParameter($prefix); | ||
$this->setParameter($suffix); | ||
|
||
$localizedProperties = array(); | ||
|
||
foreach ($originalClass->getProperties() as $originalProperty) { | ||
if ((! method_exists('Closure', 'bind')) && $originalProperty->isPrivate()) { | ||
// @codeCoverageIgnoreStart | ||
throw UnsupportedProxiedClassException::unsupportedLocalizedReflectionProperty($originalProperty); | ||
// @codeCoverageIgnoreEnd | ||
} | ||
|
||
$propertyName = $originalProperty->getName(); | ||
|
||
if ($originalProperty->isPrivate()) { | ||
$localizedProperties[] = "\\Closure::bind(function () use (\$localizedObject) {\n " | ||
. '$this->' . $propertyName . ' = & $localizedObject->' . $propertyName . ";\n" | ||
. '}, $this, ' . var_export($originalProperty->getDeclaringClass()->getName(), true) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, it prevents nasty things when people use class names with weird chars in them |
||
. ')->__invoke();'; | ||
} else { | ||
$localizedProperties[] = '$this->' . $propertyName . ' = & $localizedObject->' . $propertyName . ";"; | ||
} | ||
} | ||
|
||
$this->setDocblock( | ||
"@override constructor to setup interceptors\n\n" | ||
. "@param \\" . $originalClass->getName() . " \$localizedObject\n" | ||
. "@param \\Closure[] \$prefixInterceptors method interceptors to be used before method logic\n" | ||
. "@param \\Closure[] \$suffixInterceptors method interceptors to be used before method logic" | ||
); | ||
$this->setBody( | ||
(empty($localizedProperties) ? '' : implode("\n\n", $localizedProperties) . "\n\n") | ||
. '$this->' . $prefixInterceptors->getName() . " = \$prefixInterceptors;\n" | ||
. '$this->' . $suffixInterceptors->getName() . " = \$suffixInterceptors;" | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/StaticProxyConstructor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?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\AccessInterceptorScopeLocalizer\MethodGenerator; | ||
|
||
use ProxyManager\Generator\MethodGenerator; | ||
use ProxyManager\Generator\ParameterGenerator; | ||
use ReflectionClass; | ||
|
||
/** | ||
* The `staticProxyConstructor` implementation for an access interceptor scope localizer proxy | ||
* | ||
* @author Marco Pivetta <ocramius@gmail.com> | ||
* @license MIT | ||
*/ | ||
class StaticProxyConstructor extends MethodGenerator | ||
{ | ||
/** | ||
* Constructor | ||
* | ||
* @param ReflectionClass $originalClass | ||
*/ | ||
public function __construct(ReflectionClass $originalClass) | ||
{ | ||
parent::__construct('staticProxyConstructor', array(), static::FLAG_PUBLIC | static::FLAG_STATIC); | ||
|
||
$localizedObject = new ParameterGenerator('localizedObject'); | ||
$prefix = new ParameterGenerator('prefixInterceptors'); | ||
$suffix = new ParameterGenerator('suffixInterceptors'); | ||
|
||
$localizedObject->setType($originalClass->getName()); | ||
$prefix->setDefaultValue(array()); | ||
$suffix->setDefaultValue(array()); | ||
$prefix->setType('array'); | ||
$suffix->setType('array'); | ||
|
||
$this->setParameter($localizedObject); | ||
$this->setParameter($prefix); | ||
$this->setParameter($suffix); | ||
|
||
$this->setDocblock( | ||
"Constructor to setup interceptors\n\n" | ||
. "@param \\" . $originalClass->getName() . " \$localizedObject\n" | ||
. "@param \\Closure[] \$prefixInterceptors method interceptors to be used before method logic\n" | ||
. "@param \\Closure[] \$suffixInterceptors method interceptors to be used before method logic\n\n" | ||
. "@return self" | ||
); | ||
$this->setBody( | ||
'static $reflection;' . "\n\n" | ||
. '$reflection = $reflection ?: $reflection = new \ReflectionClass(__CLASS__);' . "\n" | ||
. '$instance = $reflection->newInstanceWithoutConstructor();' . "\n\n" | ||
. '$instance->bindProxyProperties($localizedObject, $prefixInterceptors, $suffixInterceptors);' . "\n\n" | ||
. 'return $instance;' | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use the short syntax.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll leave the short syntax for a subsequent diff: I don't want to clutter this one.