-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Translator] Further postpone adding resource files #27063
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 |
---|---|---|
|
@@ -46,6 +46,8 @@ class Translator extends BaseTranslator implements WarmableInterface | |
*/ | ||
private $resources = array(); | ||
|
||
private $resourceFiles; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
|
@@ -75,7 +77,7 @@ public function __construct(ContainerInterface $container, MessageFormatterInter | |
|
||
$this->options = array_merge($this->options, $options); | ||
$this->resourceLocales = array_keys($this->options['resource_files']); | ||
$this->addResourceFiles($this->options['resource_files']); | ||
$this->resourceFiles = $this->options['resource_files']; | ||
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. This will create a copy of $this->options['resource_files'] array, right? Wouldn't it be better for memory to have a $resourceFilesLoaded boolean attribute instead, and use $this->options['resource_files'] later to iterate at addResourceFiles? 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. This will only copy the array of there is a modification to the array, if we don't touch to the array the copy-on-write mechanism will just keep a second reference to the same array. 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. actually, thanks to copy-on-write, no duplication will ever happen 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. Then, is ok for me also 😀 |
||
|
||
parent::__construct($defaultLocale, $formatter, $this->options['cache_dir'], $this->options['debug']); | ||
} | ||
|
@@ -103,6 +105,9 @@ public function warmUp($cacheDir) | |
|
||
public function addResource($format, $resource, $locale, $domain = null) | ||
{ | ||
if ($this->resourceFiles) { | ||
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. This could be "if (!$this->resourceFilesLoaded) {" |
||
$this->addResourceFiles(); | ||
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. do we need to do it here ? We could ensure we do it only on As 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. one could call the constructor, then addResource on the object, isn't it? 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 confirm, and this is already covered by tests :) ) |
||
} | ||
$this->resources[] = array($format, $resource, $locale, $domain); | ||
} | ||
|
||
|
@@ -117,6 +122,9 @@ protected function initializeCatalogue($locale) | |
|
||
protected function initialize() | ||
{ | ||
if ($this->resourceFiles) { | ||
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. This could also be "if (!$this->resourceFilesLoaded) { " |
||
$this->addResourceFiles(); | ||
} | ||
foreach ($this->resources as $key => $params) { | ||
list($format, $resource, $locale, $domain) = $params; | ||
parent::addResource($format, $resource, $locale, $domain); | ||
|
@@ -130,8 +138,11 @@ protected function initialize() | |
} | ||
} | ||
|
||
private function addResourceFiles($filesByLocale) | ||
private function addResourceFiles() | ||
{ | ||
$filesByLocale = $this->resourceFiles; | ||
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. With this assignment, the resources are cloned again, and now we have 3 times the same content for a moment. We already have $this->options['resource_files'] to iterate in the next line |
||
$this->resourceFiles = array(); | ||
|
||
foreach ($filesByLocale as $locale => $files) { | ||
foreach ($files as $key => $file) { | ||
// filename is domain.locale.format | ||
|
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 think this could be a "private $resourceFilesLoaded" boolean (see next comment).