-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #97 from basz/koseduhemak-master
Added New AssetStrategy
- Loading branch information
Showing
7 changed files
with
274 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
namespace SlmLocale\Strategy; | ||
|
||
use SlmLocale\LocaleEvent; | ||
|
||
/** | ||
* This class checks whether the requested uri should deliver an asset and should therefore not be redirected. | ||
* If it is an asset, we return false to stop further processing of other strategies in SlmLocale\Locale\Detector. | ||
* | ||
* Example config: | ||
* 'slm_locale' => [ | ||
* 'default' => 'de_DE', | ||
* 'supported' => ['en_GB', 'de_DE'], | ||
* 'strategies' => [ | ||
* [ | ||
* 'name' => SlmLocale\Strategy\AssetStrategy::class, | ||
* 'options' => [ | ||
* 'file_extensions' => [ | ||
* 'css', 'js' | ||
* ] | ||
* ] | ||
* ], | ||
* 'query', | ||
* 'cookie', | ||
* 'acceptlanguage' | ||
* ], | ||
* 'mappings' => [ | ||
* 'en' => 'en_GB', | ||
* 'de' => 'de_DE', | ||
* ] | ||
* ], | ||
* | ||
* This example config would ignore the file_extensions ".css", ".CSS", ".js", ".JS". | ||
* | ||
* Class AssetStrategy | ||
* @package SlmLocale\Strategy | ||
*/ | ||
final class AssetStrategy extends AbstractStrategy | ||
{ | ||
/** @var array */ | ||
private $file_extensions = []; | ||
|
||
public function detect(LocaleEvent $event) | ||
{ | ||
$this->stopPropagationIfAsset($event); | ||
} | ||
|
||
public function found(LocaleEvent $event) | ||
{ | ||
$this->stopPropagationIfAsset($event); | ||
} | ||
|
||
private function stopPropagationIfAsset(LocaleEvent $event) | ||
{ | ||
if (! $this->isHttpRequest($event->getRequest())) { | ||
return; | ||
} | ||
|
||
$path = $event->getRequest()->getUri(); | ||
$path = parse_url($path, PHP_URL_PATH); | ||
$extension = strtolower(pathinfo($path, PATHINFO_EXTENSION)); | ||
|
||
// if the file extension of the uri is found within the configured file_extensions, we do not rewrite and skip further processing | ||
if (in_array($extension, $this->file_extensions)) { | ||
$event->stopPropagation(); | ||
} | ||
} | ||
|
||
public function setOptions(array $options = []) | ||
{ | ||
if (array_key_exists('file_extensions', $options)) { | ||
$this->file_extensions = (array) $options['file_extensions']; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<?php | ||
|
||
namespace SlmLocaleTest\Strategy; | ||
|
||
use PHPUnit_Framework_TestCase as TestCase; | ||
use SlmLocale\Locale\Detector; | ||
use SlmLocale\LocaleEvent; | ||
use SlmLocale\Strategy\AssetStrategy; | ||
use Zend\Console\Request as ConsoleRequest; | ||
use Zend\Console\Response as ConsoleResponse; | ||
use Zend\EventManager\EventManager; | ||
use Zend\Http\PhpEnvironment\Request as HttpRequest; | ||
use Zend\Http\PhpEnvironment\Response as HttpResponse; | ||
|
||
class AssetStrategyTest extends TestCase | ||
{ | ||
/** @var AssetStrategy */ | ||
private $strategy; | ||
|
||
/** @var LocaleEvent */ | ||
private $event; | ||
|
||
public function setup() | ||
{ | ||
$this->strategy = new AssetStrategy(); | ||
$this->strategy->setOptions(['file_extensions' => ['css', 'js']]); | ||
|
||
$this->event = new LocaleEvent(); | ||
$this->event->setSupported(['nl', 'de', 'en']); | ||
} | ||
|
||
public function testDetectReturnsNullByDefault() | ||
{ | ||
$this->event->setRequest(new HttpRequest()); | ||
$this->event->setResponse(new HttpResponse()); | ||
|
||
$locale = $this->strategy->detect($this->event); | ||
$this->assertNull($locale); | ||
} | ||
|
||
public function testDetectWithConsoleRequestReturnsNull() | ||
{ | ||
$this->event->setRequest(new ConsoleRequest()); | ||
$this->event->setResponse(new ConsoleResponse()); | ||
|
||
$locale = $this->strategy->detect($this->event); | ||
$this->assertNull($locale); | ||
} | ||
|
||
/** | ||
* @dataProvider uriProvider | ||
*/ | ||
public function testDetectShouldStopPropagationIfFileExtensionWasConfigured($uri, $isAsset) | ||
{ | ||
$request = new HttpRequest(); | ||
$request->setUri($uri); | ||
|
||
$this->event->setRequest($request); | ||
|
||
// should stop event propagation if extension was configured to ignore. | ||
$result = $this->strategy->detect($this->event); | ||
$this->assertNull($result); | ||
$this->assertEquals($isAsset, $this->event->propagationIsStopped()); | ||
} | ||
|
||
/** | ||
* @dataProvider uriProvider | ||
*/ | ||
public function testFoundShouldStopPropagationIfFileExtensionWasConfigured($uri, $isAsset) | ||
{ | ||
$request = new HttpRequest(); | ||
$request->setUri($uri); | ||
|
||
$this->event->setRequest($request); | ||
|
||
// should stop event propagation if extension was configured to ignore. | ||
$result = $this->strategy->found($this->event); | ||
$this->assertNull($result); | ||
$this->assertEquals($isAsset, $this->event->propagationIsStopped()); | ||
} | ||
|
||
public function uriProvider() | ||
{ | ||
return [ | ||
['http://example.com/css/style.css', true], | ||
['http://example.com/css/style.css?ver=123456', true], | ||
['http://example.com/css/script.js', true], | ||
['http://example.com/image/image.jpg', false], | ||
['http://example.com/article/new-asset-strategy', false], | ||
]; | ||
} | ||
|
||
public function testAssetStrategyCanPreventOtherStrategiesExecution() | ||
{ | ||
$request = new HttpRequest(); | ||
$request->setUri('http://example.com/css/style.css'); | ||
$query = $request->getQuery(); | ||
$query->lang = 'de'; | ||
$request->setQuery($query); | ||
$this->event->setRequest($request); | ||
|
||
$detector = new Detector(); | ||
$detector->setEventManager(new EventManager()); | ||
$detector->setSupported(['nl', 'de', 'en']); | ||
$detector->setDefault('en'); | ||
$detector->addStrategy($this->strategy); | ||
$detector->addStrategy(new \SlmLocale\Strategy\QueryStrategy()); | ||
$response = new HttpResponse(); | ||
|
||
$result = $detector->detect($request, $response); | ||
$this->assertEquals('en', $result); | ||
} | ||
} |