-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,8 @@ | |
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; | ||
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface; | ||
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag; | ||
use Symfony\Component\Config\Resource\DirectoryResource; | ||
use Symfony\Component\Config\Resource\FileExistenceResource; | ||
use Symfony\Component\Config\Resource\FileResource; | ||
use Symfony\Component\Config\Resource\ResourceInterface; | ||
use Symfony\Component\DependencyInjection\LazyProxy\Instantiator\InstantiatorInterface; | ||
|
@@ -1321,6 +1323,42 @@ public static function getServiceConditionals($value) | |
return $services; | ||
} | ||
|
||
/** | ||
* Checks whether the requested file or directory exists and registers the result for resource tracking. | ||
* | ||
* @param string $path The file or directory path for which to check the existence | ||
* @param bool|string $trackContents Whether to track contents of the given resource. If a string is passed, | ||
* it will be used as pattern for tracking contents of the requested directory | ||
* | ||
* @return bool | ||
* | ||
* @final | ||
*/ | ||
public function fileExists($path, $trackContents = 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. 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. 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. @jakzal This follows up #21452 (comment) which gives the arguments against the need for reflecting the tracking effect in the method name. |
||
{ | ||
$exists = file_exists($path); | ||
|
||
if (!$this->trackResources) { | ||
return $exists; | ||
} | ||
|
||
if (!$exists) { | ||
$this->addResource(new FileExistenceResource($path)); | ||
|
||
return $exists; | ||
} | ||
|
||
if ($trackContents) { | ||
if (is_file($path)) { | ||
$this->addResource(new FileResource($path)); | ||
} else { | ||
$this->addResource(new DirectoryResource($path, is_string($trackContents) ? $trackContents : null)); | ||
} | ||
} | ||
|
||
return $exists; | ||
} | ||
|
||
/** | ||
* Retrieves the currently set proxy instantiator or instantiates one. | ||
* | ||
|
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.
Why did you pass
false
here (same for similar cases below)?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.
These files do not impact the container (see #21408 (comment))
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.
Ah yeah, just found the hidden discussion 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.
Maybe we should open a PR for older branches to make use of the
FileExistenceResource
there.