-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Class existence resource #20121
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
Class existence resource #20121
Conversation
fabpot
commented
Oct 1, 2016
Q | A |
---|---|
Branch? | master |
Bug fix? | no |
New feature? | yes |
BC breaks? | no |
Deprecations? | no |
Tests pass? | yes |
Fixed tickets | see #20094 |
License | MIT |
Doc PR | n/a |
9232ba4
to
8d0ff06
Compare
namespace Symfony\Component\Config\Resource; | ||
|
||
/** | ||
* ClassExistenceResource represents a class availability. |
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.
Let's replace availability
with existence
, to be consistent with the class name and the class_exists
function name.
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.
done
EOF | ||
); | ||
|
||
$this->assertFalse($res->isFresh(time())); |
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.
Afaik, this should be assertTrue
. In table form (horizontal: current state, vertical: stored state)
Class exists | Class does not exists | |
---|---|---|
Class exists | Not fresh | Fresh |
Class does not exists | Fresh | Not fresh |
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.
The freshness is all about the state change. The resource is fresh if there is no change in class existence, so no need to refresh the cache. Not fresh means that the cache must be recalculated, so when the state changes. So, the table should be:
Class exists | Class does not exists | |
---|---|---|
Class exists | Fresh | Not fresh |
Class does not exists | Not fresh | fresh |
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.
Oh, sorry. Completely messed up the definition of "fresh"...
@@ -72,15 +76,18 @@ public function process(ContainerBuilder $container) | |||
$container->getDefinition('twig.extension.assets')->addTag('twig.extension'); | |||
} | |||
|
|||
if (class_exists('Symfony\Component\Yaml\Parser')) { | |||
$container->addResource(new ClassExistenceResource(YamlParser::class)); | |||
if (class_exists(YamlParser::class)) { |
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.
Related to this comment, just asking if we could replace this:
if (class_exists(YamlParser::class)) {
$container->getDefinition('twig.extension.yaml')->addTag('twig.extension');
}
by this?
if (!class_exists(YamlParser::class)) {
$container->removeDefinition('twig.extension.yaml');
}
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.
The difference is that if one uses the twig.extension.yaml
directly, the proposed version will break.
I think the current approach is a more open thus a better practice, even if in this very case using the extension directly is so unlikely.
8d0ff06
to
222b56d
Compare
This PR was merged into the 3.2-dev branch. Discussion ---------- Class existence resource | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | see #20094 | License | MIT | Doc PR | n/a Commits ------- 222b56d [TwigBundle] added support for ClassExistenceResource when relevant d98eb7b [Config] added ClassExistenceResource
This PR was merged into the 4.0.x-dev branch. Discussion ---------- added support for ClassExistenceResource depends on symfony/symfony#20121 Commits ------- a1f987d added support for ClassExistenceResource
public function __construct($resource) | ||
{ | ||
$this->resource = $resource; | ||
$this->exists = class_exists($resource); |
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.
we should use class_exists() || interface_exists() || trait_exists()
to make it usable for interfaces and traits too