-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DependencyInjection] Forbid container cloning #15290
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
Conversation
jakzal
commented
Jul 16, 2015
Q | A |
---|---|
Bug fix? | no |
New feature? | no |
BC breaks? | no |
Deprecations? | no |
Tests pass? | yes |
Fixed tickets | #15261 |
License | MIT |
Doc PR | - |
df5f4ec
to
85c8451
Compare
@@ -90,6 +90,11 @@ public function __construct(ParameterBagInterface $parameterBag = null) | |||
$this->parameterBag = $parameterBag ?: new ParameterBag(); | |||
} | |||
|
|||
public function __clone() |
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 suggest using a private (final) __clone
method. This way, PHP itself will figure out that the class cannot be cloned (ReflectionClass::isCloneable
will return false
)
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.
Great suggestion. Applied.
85c8451
to
68fb163
Compare
{ | ||
$class = new \ReflectionClass('Symfony\Component\DependencyInjection\Container'); | ||
$clone = $class->getMethod('__clone'); | ||
$this->assertTrue($clone->isPrivate()); |
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 would assert that $class->isCloneable()
returns false too
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.
Sure. This check would actually be sufficient. Added.
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.
Actually, isCloneable() is available for PHP >= 5.4.
68fb163
to
8fcc4bd
Compare
8fcc4bd
to
ba12904
Compare
👍 |
Let's wait for a green build. Previous one failed on PHP 5.3 ;) |
What about merging this one on 2.3? Cloning a container does not work anyway, so it should be safe. |
I'm fine with merging to 2.3. I just checked and there should be no issues in merging this PR to the 2.3 branch. We can't get a green build, as tests from the tty group are blocking phpunit process on travis. Once 2.7 is merged to 2.8, there should be no blocking, but tests will still be failing. I'll work on fixing them as soon as 2.8 is updated. |
After more thought, I'm going to merge it into 2.8. |
Thank you @jakzal. |
This PR was merged into the 2.8 branch. Discussion ---------- [DependencyInjection] Forbid container cloning | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #15261 | License | MIT | Doc PR | - Commits ------- ba12904 [DependencyInjection] Forbid container cloning
@jakzal I've just merged 2.7 into 2.8. |
@fabpot cool. Looks like the fix is already in 2.8. Tests should be passing now. |
👍 |
The |
@dosten cloning a ContainerBuilder is a huge pain (see my comment in the issue) |