Skip to content
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

[WIP] Fix localization files loading problem #4444

Closed

Conversation

mlocati
Copy link
Contributor

@mlocati mlocati commented Sep 30, 2016

This is an alternative solution (for v8 now) to #4443

MrKarlDilkington and others added 30 commits August 27, 2016 06:15
* Let's perform tests against PHP 7.1 too

* Let's see how PHP nightly behaves

* Allow failure of PHP 7.1

* Fix allow_failures TravisCI directive
Newer PHPUnit versions show this warning:
Trying to configure method "getCollectionObject" which cannot be configured
because it does not exist, has not been specified, is final, or is static

Let's fix this warning
* remove media query that was breaking the fieldless inputs

* rebuild CSS
* Bootstrap grid elements are breaking fieldless inputs and missing form-group class

* tabs to spaces
* Bootstrap grid elements are breaking fieldless inputs

* tabs to spaces

* translate select options
* Docblocks

* Return type string in __toString method
- also add the Selectize remove button
{
parent::__construct();

$this->cacheName = $cacheName;
$this->setApplication(Application::getFacadeApplication());
Copy link
Member

Choose a reason for hiding this comment

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

If we use $app->make or $app->build to create these classes, you can just implement ApplicationAwareInterface and avoid being coupled to the application facade

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in upcoming commit that I'll add to this PR (#4444)

$this->cacheLifetime = $cacheLifetime;
}

public function setApplication(\Concrete\Core\Application\Application $app)
Copy link
Member

Choose a reason for hiding this comment

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

Looks like we already meet the minimum requirements for that anyway.

if ($this->isLocalizationReady()) {
return $this->app->make($this->finalCacheName);
} else {
return null;
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we need to be explicit with null returns

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe because of many years programming in other languages, as for myself I prefer writing return null instead of just return. I know they lead to the same result, but they are conceptually different...
Think for instance at:

  • a function like echo that never returns anything.
  • a function that parses a string and returns an int if it's valid or null otherwise,

I'd only use return alone in the first case, when functions are not supposed to never return anything.
That's why I suggested this for our .php_cs

protected function isLocalizationReady()
{
if (!$this->localizationReady) {
if ($this->config->get('app.bootstrap.packages_loaded') === true) {
Copy link
Member

Choose a reason for hiding this comment

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

I know why we do this, but just seeing $this->config->get('app.bootstrap.packages_loaded') here makes me sad. We should've made a $status = $app->packageStatus(); or something.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@KorvinSzanto I'm totally with you. This would solve #4253 (see point 4. of the cause).

$item = $cache->getItem('zend/'.$normalizedKey);
if ($item->isMiss()) {
$cache = $this->getCache();
$key = 'zend/'.$normalizedKey;
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't look like this variable gets used

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in upcoming commit that I'll add to this PR (#4444)

if ($result = $item->set($value, $this->cacheLifetime)) {
$item->save();
$cache = $this->getCache();
if ($cache === null) {
Copy link
Member

Choose a reason for hiding this comment

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

With this pattern, I'd just do:

if ($cache = $this->getCache()) {
    $item = ...
    return true;
}

return false;

Also seeing the 'zend/'.$normalizedKey) here makes me thing this might benefit from a protected method like $this->getCacheKey($normalizedKey)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For a cleaner code, I prefer to avoid assignments in if statements.
Indeed, if you read if ($a = 2) { you may think that's wrong, you have to go back to the code and check what the coder wanted.

About getCacheKey: fixed in upcoming commit that I'll add to this PR (#4444)

@@ -87,12 +138,14 @@ protected function internalGetItem(&$normalizedKey, &$success = null, &$casToken
*/
protected function internalSetItem(&$normalizedKey, &$value)
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 part of zend yeah? I wonder why they pass both arguments by reference :S

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know why, but yes, it's because the base ZF abstract class defines the abstract method this way.

$item = $cache->getItem('zend/'.$normalizedKey);
$cache = $this->getCache();
if ($cache === null) {
$result = true;
Copy link
Member

Choose a reason for hiding this comment

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

Returning true for this is a little weird

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It depends on the meaning you give.

  1. If internalRemoveItem means "please be sure that that item does not exist in the cache", we should return true.
  2. If internalRemoveItem means "please remove that item and tell me if you succeeded", we should return false.

I adopted 1, but also 2 makes sense... I'll update my code.

*
* @return bool
*/
public function flush()
{
return Core::make($this->cacheName)->getItem('zend')->clear();
return $this->app->make($this->finalCacheName)->getItem('zend')->clear();
Copy link
Member

Choose a reason for hiding this comment

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

this should use the getCache() method so that we're not duplicating code

Copy link
Contributor Author

Choose a reason for hiding this comment

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

getCache returns the cache only if all the translations files are ready. BTW one may want to clear the cache even before that occurs...

@mlocati
Copy link
Contributor Author

mlocati commented Oct 1, 2016

For a better and more robust handling (solving #4444 too), what about saving the translations state as a property (with getters/setters) of the Application instance?

@mlocati mlocati changed the title Fix localization files loading problem [WIP] Fix localization files loading problem Oct 4, 2016
@mlocati mlocati mentioned this pull request Oct 6, 2016
@KorvinSzanto
Copy link
Member

This pull request needs to be rebased and reopened because we did some history rewriting, if you need help with this head into #dev in our slack channel

#4814

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants