Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[DI] Add ContainerBuilder::fileExists() for checking/tracking resource existence#21408

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged

Conversation

chalasr
Copy link
Member

@chalasrchalasr commentedJan 25, 2017
edited
Loading

QA
Branch?master
Bug fix?no
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed tickets#20189,#20189 (comment)
LicenseMIT
Doc PRn/a

Finishes#20189 Adds a convenientContainerBuilder::fileExists() method as suggested by Nicolas and use it to track resources in the FrameworkExtension, adding some missing ones.

ogizanagi reacted with thumbs up emoji
@chalasrchalasr changed the titleMissing existence resource[Config][FrameworkBundle] Add missing class/directory/file existence resourceJan 25, 2017
@chalasrchalasrforce-pushed themissing-existence-resource branch 2 times, most recently from772970b to9328b37CompareJanuary 25, 2017 18:02
@ogizanagi
Copy link
Contributor

According to#20189 (comment), it seems theDirectoryExistenceResource implementation should allow to detect new files in the directory. Not only check if the dir exists (which already worked with theFileExistenceResource but here you ensure it's a dir).

@@ -900,6 +915,7 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
// Register translation resources
if ($dirs) {
foreach ($dirs as $dir) {
$container->addResource(new DirectoryExistenceResource($dir));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

this is wrong. It must be done earlier, because the directories are not added in$dirs if they don't exist, and this is precisely the case where we need the DirectoryExistenceResource

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Indeed

$files['yml'][] = $file;
$container->addResource(new FileResource($file));
}

if (is_file($file = $dirname.'/Resources/config/validation.xml')) {
$container->addResource(new FileExistenceResource($file = $dirname.'/Resources/config/validation.xml'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

We could optimize things by adding it only when the file does not exist (as we already have a FileResource when it exists, and removing the file will also invalidate the FileResource).
Or we should remove the FileResource if the content of the file does not impact the container itself (which may well be the case for this particular case)

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

These paths being all passed to a definition through a method call, don't they impact it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

well, if the paths are passed, the content of the file does not impact the container definition (it cannot impact it if you never read the content during the container building). So a FileResource is useless. Only FileExistenceResource is needed.

And it avoids invalidating the container cache when adding a new validation constraint while the container does not care about them and so would rebuild exactly the same one

chalasr reacted with thumbs up emoji
Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Thanks for the explanation.

@stof
Copy link
Member

@ogizanagi no. If you need to detect the addition of new files inside a directory, you must use DirectoryResource. This is exactly what it does.

And IIRC, we used FileExistenceResource to account for cases checking for directory existence in the past (the only case where it would fail is if you replace a file with a directory with the same name, but this is weird)

@ogizanagi
Copy link
Contributor

ogizanagi commentedJan 25, 2017
edited
Loading

@stof : You're right. Thanks for the head up. I misinterpreted@fabpot comment and didn't even realized I was talking about the existingDirectoryResource implementation... -.-'

@chalasr
Copy link
MemberAuthor

chalasr commentedJan 25, 2017
edited
Loading

And IIRC, we used FileExistenceResource to account for cases checking for directory existence in the past (the only case where it would fail is if you replace a file with a directory with the same name, but this is weird)

@stof Sure, FileExistenceResource would work except for the edge case you mentioned. IMHO the distinction is not bad to have, but I'll revert if you think it's not worth it.

Btw, I updated the places you're talking about for make them useDirectoryExistenceResource instead.

@chalasrchalasrforce-pushed themissing-existence-resource branch 2 times, most recently fromcfc5524 tof901285CompareJanuary 25, 2017 20:40
@chalasrchalasr changed the title[Config][FrameworkBundle] Add missing class/directory/file existence resource[FrameworkBundle] Add missing class/directory/file existence resourceJan 25, 2017
@chalasr
Copy link
MemberAuthor

Comments addressed

@@ -105,15 +114,16 @@ public function load(array $configs, ContainerBuilder $container)
// default in the Form and Validator component). If disabled, an identity
// translator will be used and everything will still work as expected.
if ($this->isConfigEnabled($container, $config['translator']) || $this->isConfigEnabled($container, $config['form']) || $this->isConfigEnabled($container, $config['validation'])) {
if (!class_exists('Symfony\Component\Translation\Translator') && $this->isConfigEnabled($container, $config['translator'])) {
$container->addResource(new ClassExistenceResource(Translator::class));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Why should we track classes for throwing cases? Isn't the containernot dumped when an exception is thrown? Thus there is nothing to track? (same below)

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I think you're right. Removed

if (is_dir($dir)) {
$dirs[] = $dir;
} else {
throw new \UnexpectedValueException(sprintf('%s defined in translator.paths does not exist or is not a directory', $dir));
}
}

if (is_dir($dir = $rootDir.'/Resources/translations')) {
$container->addResource(new FileExistenceResource($dir = $rootDir.'/Resources/translations'));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

this is already tracked a few lines below

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

maybe not when the dir is not found in fact - then should be added in an "else" case of the following "if", isn't it?

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Indeed, moved in a else

}

if (is_file($file = $dirname.'/Resources/config/validation.xml')) {
$container->addResource(new FileExistenceResource($file = $dirname.'/Resources/config/validation.xml'));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

FileExistence is not enoug when the file is found - FileResource is needed then

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Please see#21408 (comment) from@stof

nicolas-grekas reacted with thumbs up emoji
@chalasrchalasrforce-pushed themissing-existence-resource branch 7 times, most recently fromad9b26f to0026c67CompareJanuary 29, 2017 14:59
@@ -105,15 +112,15 @@ public function load(array $configs, ContainerBuilder $container)
// default in the Form and Validator component). If disabled, an identity
// translator will be used and everything will still work as expected.
if ($this->isConfigEnabled($container, $config['translator']) || $this->isConfigEnabled($container, $config['form']) || $this->isConfigEnabled($container, $config['validation'])) {
if (!class_exists('Symfony\Component\Translation\Translator') && $this->isConfigEnabled($container, $config['translator'])) {
if (!class_exists(Translator::class) && $this->isConfigEnabled($container, $config['translator'])) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

as usual, all these should be reverted: they only create merge conflicts with no practical benefit

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Oops, done

@chalasrchalasrforce-pushed themissing-existence-resource branch from0026c67 tof6122eaCompareJanuary 29, 2017 15:10
if ($this->isConfigEnabled($container, $config['form'])) {
$this->formConfigEnabled = true;
$this->registerFormConfiguration($config, $container, $loader);
$config['validation']['enabled'] = true;

if (!class_exists('Symfony\Component\Validator\Validation')) {
if (!class_exists(Validation::class)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

all 'Foo\Bar' => Bar::classchanges in this PR should be reverted :)

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

There was 2 chances :) I'm ok to revert, but fabpot started like this (seehttps://github.com/symfony/symfony/pull/20189/files) and IMHO it's a good opportunity to make the code more readable

@nicolas-grekas
Copy link
Member

nicolas-grekas commentedJan 29, 2017
edited
Loading

Note that this PR might be superseded by#21452
So maybe better to submit a PR on my fork if you see anything missing there?
Or keep here only the parts that are not already covered in#21452?

@chalasr
Copy link
MemberAuthor

Status: needs work

@chalasrchalasrforce-pushed themissing-existence-resource branch 2 times, most recently fromd7ee7d6 to5877987CompareJanuary 30, 2017 12:45
@chalasr
Copy link
MemberAuthor

@nicolas-grekas Updated TwigExtension and DoctrineValidationPass, other places do not seem relevant (either they are aboutRouteCollection::addResource() or do not look for the resource existence).

Status: needs review

@chalasrchalasrforce-pushed themissing-existence-resource branch 2 times, most recently from9b39dca tocfe850bCompareJanuary 30, 2017 12:55
if ($trackContents) {
if (is_file($path)) {
$this->addResource(new FileResource($path));
} elseif (is_dir($path)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

what but just an "else" here? otherwise, it looks strange to has a potential case were we don't do anything (the implicit else here)

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

agreed, done

@chalasrchalasrforce-pushed themissing-existence-resource branch 4 times, most recently from2e4ced3 tocece6d4CompareJanuary 30, 2017 19:58
@chalasrchalasrforce-pushed themissing-existence-resource branch from076dec5 to6b556b8CompareFebruary 2, 2017 10:26
Copy link
Member

@nicolas-grekasnicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

👍

@@ -1006,19 +1001,16 @@ private function getValidatorMappingFiles(ContainerBuilder $container, array &$f
foreach ($container->getParameter('kernel.bundles_metadata') as $bundle) {
$dirname = $bundle['path'];

if (is_file($file = $dirname.'/Resources/config/validation.yml')) {
if ($container->fileExists($file = $dirname.'/Resources/config/validation.yml', false)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Why did you passfalse here (same for similar cases below)?

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

These files do not impact the container (see#21408 (comment))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Ah yeah, just found the hidden discussion too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Maybe we should open a PR for older branches to make use of theFileExistenceResource there.

Copy link
Member

@xabbuhxabbuh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

👍

@nicolas-grekas
Copy link
Member

Thank you@chalasr.

@nicolas-grekasnicolas-grekas merged commit6b556b8 intosymfony:masterFeb 2, 2017
nicolas-grekas added a commit that referenced this pull requestFeb 2, 2017
…racking resource existence (chalasr)This PR was merged into the 3.3-dev branch.Discussion----------[DI] Add ContainerBuilder::fileExists() for checking/tracking resource existence| Q             | A| ------------- | ---| Branch?       | master| Bug fix?      | no| New feature?  | yes| BC breaks?    | no| Deprecations? | no| Tests pass?   | yes| Fixed tickets |#20189,#20189 (comment)| License       | MIT| Doc PR        | n/a~~Finishes#20189 Adds a convenient `ContainerBuilder::fileExists()` method as suggested by Nicolas and use it to track resources in the FrameworkExtension, adding some missing ones.Commits-------6b556b8 [DI] Add ContainerBuilder::fileExists()
@chalasrchalasr deleted the missing-existence-resource branchFebruary 2, 2017 13:16
*
* @final
*/
public function fileExists($path, $trackContents = true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I know I'm late to the party, but this name doesn't suggest any side effects. The method actually adds resources and could be better named to reflect this fact.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

@jakzal This follows up#21452 (comment) which gives the arguments against the need for reflecting the tracking effect in the method name.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@jakzaljakzaljakzal left review comments

@nicolas-grekasnicolas-grekasnicolas-grekas approved these changes

@stofstofstof approved these changes

@xabbuhxabbuhxabbuh approved these changes

Assignees
No one assigned
Projects
None yet
Milestone
3.3
Development

Successfully merging this pull request may close these issues.

8 participants
@chalasr@ogizanagi@stof@nicolas-grekas@jakzal@xabbuh@javiereguiluz@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp