Skip to content

Commit

Permalink
Fix #6 #7 #9 - Add callbacks feature
Browse files Browse the repository at this point in the history
  • Loading branch information
vluzrmos committed Dec 30, 2015
1 parent 3d1ae59 commit 2f2014a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/Drivers/AbstractDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function setLanguages(array $languages)
*/
public function setDefaultSegment($segment = 0)
{
$this->segment = $segment;
$this->segment = (int) $segment;
}

/**
Expand Down
27 changes: 14 additions & 13 deletions src/LanguageDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ class LanguageDetector implements LanguageDetectorInterface
protected $driver;

/**
* @var Closure
* @var array
*/
protected $applyLocaleCallback;
protected $callbacks = [];

/**
* @param Translator $translator
Expand Down Expand Up @@ -95,26 +95,27 @@ public function apply($locale)
{
$this->translator->setLocale($locale);

call_user_func($this->getApplyLocaleCallback(), $locale, $this);
$this->applyCallbacks($locale);
}

/**
* Get the Closure to be called after locale was applied.
*
* @return Closure
* Add a callback to call after applying the detected locale.
* @param Closure $callback
*/
public function getApplyLocaleCallback()
{
return is_callable($this->applyLocaleCallback) ? $this->applyLocaleCallback : function () {};
public function addCallback(Closure $callback){
$this->callbacks[] = $callback;
}

/**
* Set the Closure to be called after locale was applied.
* @param Closure $callback
* Call all registered callbacks.
*
* @param $language
*/
public function setApplyLocaleCallback(Closure $callback)
protected function applyCallbacks($language)
{
$this->applyLocaleCallback = $callback;
foreach($this->callbacks as $callback) {
call_user_func($callback, $language, $this);
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Providers/LanguageDetectorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ function () use ($driver) {
);

if (method_exists($this->app, 'setLocale')) {
$detector->setApplyLocaleCallback(function ($locale, $driver) {
$detector->addCallback(function ($locale) {
$this->app->setLocale($locale);
});
}
Expand Down

0 comments on commit 2f2014a

Please sign in to comment.