Skip to content

Commit

Permalink
Merge pull request #42 from bolt/hotfix/moar-efficient
Browse files Browse the repository at this point in the history
Two performance improvements for fetching labels and 'current language'.
  • Loading branch information
GwendolenLynch authored Feb 16, 2017
2 parents 957a481 + 200cb93 commit 2c18a11
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
10 changes: 7 additions & 3 deletions src/Labels.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class Labels
private $filesystemManager;
/** @var string */
private $extBasePath;
/** @var array */
private $loadedlabels = [];

/**
* Constructor.
Expand All @@ -42,7 +44,9 @@ public function __construct(Config $config, SessionInterface $session, Filesyste
*/
public function getLabels()
{
$labels = [];
if (!empty($this->loadedlabels)) {
return $this->loadedlabels;
}

// Check that the user's JSON file exists, else copy in the default
if (!$this->filesystemManager->has('config://extensions/labels.json')) {
Expand Down Expand Up @@ -81,7 +85,7 @@ public function getLabels()

/** @var \Bolt\Filesystem\Handler\File $file */
foreach ($finder->files() as $file) {
$labels = json_decode($file->read(), true);
$this->loadedlabels = json_decode($file->read(), true);
continue;
}
} catch (\Exception $e) {
Expand All @@ -91,7 +95,7 @@ public function getLabels()
);
}

return $labels;
return $this->loadedlabels;
}

/**
Expand Down
21 changes: 16 additions & 5 deletions src/LabelsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
class LabelsExtension extends SimpleExtension
{
/** @var string */
private $currentlanguage = null;

protected function registerServices(Application $app)
{
$app['labels'] = $app->share(
Expand Down Expand Up @@ -259,6 +262,7 @@ private function isValidLanguage($lang)
private function setCurrentLanguage($lang)
{
if ($this->isValidLanguage($lang)) {
$this->currentlanguage = $lang;
$app = $this->getContainer();
$app['twig']->addGlobal('lang', $lang);
}
Expand All @@ -271,12 +275,19 @@ private function setCurrentLanguage($lang)
*/
private function getCurrentLanguage()
{
$app = $this->getContainer();
$twigGlobals = $app['twig']->getGlobals();
if (isset($twigGlobals['lang'])) {
return $twigGlobals['lang'];
if (!empty($this->currentlanguage)) {
$lang = $this->currentlanguage;
} else {
return null;
$app = $this->getContainer();
$twigGlobals = $app['twig']->getGlobals();
if (isset($twigGlobals['lang'])) {
$lang = $twigGlobals['lang'];
} else {
$lang = null;
}
$this->currentlanguage = $lang;
}

return $lang;
}
}

0 comments on commit 2c18a11

Please sign in to comment.