-
-
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
Conversation
nicolas-grekas
commented
Apr 26, 2018
Q | A |
---|---|
Branch? | 4.1 |
Bug fix? | no |
New feature? | no |
BC breaks? | no |
Deprecations? | no |
Tests pass? | yes |
Fixed tickets | #27010 |
License | MIT |
Doc PR | - |
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.
👍
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.
This is my first review, I don't want to sound aggresive or not polite or something, sorry if that's the case, it's not my intention at all 😃
@@ -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 comment
The 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 comment
The 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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Then, is ok for me also 😀
@@ -46,6 +46,8 @@ class Translator extends BaseTranslator implements WarmableInterface | |||
*/ | |||
private $resources = array(); | |||
|
|||
private $resourceFiles; |
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).
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
This could be "if (!$this->resourceFilesLoaded) {"
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
This could also be "if (!$this->resourceFilesLoaded) { "
{ | ||
$filesByLocale = $this->resourceFiles; |
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.
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
@@ -103,6 +105,9 @@ public function warmUp($cacheDir) | |||
|
|||
public function addResource($format, $resource, $locale, $domain = null) | |||
{ | |||
if ($this->resourceFiles) { | |||
$this->addResourceFiles(); |
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.
do we need to do it here ?
We could ensure we do it only on initialize
by calling parent::addResource
for the resource files instead of $this->addResource
(avoiding to copy them first from resourceFiles
to resources
, to add them in the parent later.
As initialize
will process resourceFiles
before resources
, the other will be preserved.
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
(I confirm, and this is already covered by tests :) )
…olas-grekas) This PR was merged into the 4.1-dev branch. Discussion ---------- [Translator] Further postpone adding resource files | Q | A | ------------- | --- | Branch? | 4.1 | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #27010 | License | MIT | Doc PR | - Commits ------- 2aa62df [Translator] Further postpone adding resource files