Skip to content

Commit

Permalink
Merge pull request #80 from basz/use-class-notation
Browse files Browse the repository at this point in the history
Use class notation
  • Loading branch information
basz authored Mar 15, 2017
2 parents f543f81 + eb75e46 commit 9f03a42
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 60 deletions.
6 changes: 4 additions & 2 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@

use Locale;

use SlmLocale\Locale\Detector;
use Zend\EventManager\EventInterface;
use Zend\Loader\StandardAutoloader;
use Zend\ModuleManager\Feature;
use Zend\Mvc\MvcEvent;
use Zend\Stdlib\ResponseInterface;
Expand All @@ -55,7 +57,7 @@ class Module implements
public function getAutoloaderConfig()
{
return [
'Zend\Loader\StandardAutoloader' => [
StandardAutoloader::class => [
'namespaces' => [
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
],
Expand All @@ -73,7 +75,7 @@ public function onBootstrap(EventInterface $e)
$app = $e->getApplication();
$sm = $app->getServiceManager();

$detector = $sm->get('SlmLocale\Locale\Detector');
$detector = $sm->get(Detector::class);
$result = $detector->detect($app->getRequest(), $app->getResponse());

if ($result instanceof ResponseInterface) {
Expand Down
22 changes: 16 additions & 6 deletions config/module.config.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
<?php

use SlmLocale\Locale\Detector;
use SlmLocale\Service\DetectorFactory;
use SlmLocale\Service\LocaleMenuViewHelperFactory;
use SlmLocale\Service\LocaleUrlViewHelperFactory;
use SlmLocale\Strategy\Factory\StrategyPluginManagerFactory;
use SlmLocale\Strategy\StrategyPluginManager;
use SlmLocale\View\Helper\LocaleMenu;
use SlmLocale\View\Helper\LocaleUrl;

/**
* Copyright (c) 2012-2013 Jurian Sluiman.
* All rights reserved.
Expand Down Expand Up @@ -45,19 +55,19 @@

'service_manager' => [
'factories' => [
'SlmLocale\Strategy\StrategyPluginManager' => 'SlmLocale\Strategy\Factory\StrategyPluginManagerFactory',
'SlmLocale\Locale\Detector' => 'SlmLocale\Service\DetectorFactory',
StrategyPluginManager::class => StrategyPluginManagerFactory::class,
Detector::class => DetectorFactory::class,
],
],

'view_helpers' => [
'aliases' => [
'localeUrl' => 'SlmLocale\View\Helper\LocaleUrl',
'localeMenu' => 'SlmLocale\View\Helper\LocaleMenu',
'localeUrl' => LocaleUrl::class,
'localeMenu' => LocaleMenu::class,
],
'factories' => [
'SlmLocale\View\Helper\LocaleUrl' => 'SlmLocale\Service\LocaleUrlViewHelperFactory',
'SlmLocale\View\Helper\LocaleMenu' => 'SlmLocale\Service\LocaleMenuViewHelperFactory',
LocaleUrl::class => LocaleUrlViewHelperFactory::class,
LocaleMenu::class => LocaleMenuViewHelperFactory::class,
],
],
];
3 changes: 2 additions & 1 deletion src/SlmLocale/Service/DetectorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

use Interop\Container\ContainerInterface;
use SlmLocale\Locale\Detector;
use SlmLocale\Strategy\StrategyPluginManager;

class DetectorFactory
{
Expand Down Expand Up @@ -74,7 +75,7 @@ public function __invoke(ContainerInterface $container)

protected function addStrategies(Detector $detector, array $strategies, ContainerInterface $container)
{
$plugins = $container->get('SlmLocale\Strategy\StrategyPluginManager');
$plugins = $container->get(StrategyPluginManager::class);

foreach ($strategies as $strategy) {
if (is_string($strategy)) {
Expand Down
3 changes: 2 additions & 1 deletion src/SlmLocale/Service/LocaleMenuViewHelperFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

use Interop\Container\ContainerInterface;
use Interop\Container\Exception\ContainerException;
use SlmLocale\Locale\Detector;
use SlmLocale\View\Helper\LocaleMenu;
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
use Zend\ServiceManager\Exception\ServiceNotFoundException;
Expand All @@ -65,7 +66,7 @@ class LocaleMenuViewHelperFactory implements FactoryInterface
*/
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$detector = $container->get('SlmLocale\Locale\Detector');
$detector = $container->get(Detector::class);

$helper = new LocaleMenu();
$helper->setDetector($detector);
Expand Down
3 changes: 2 additions & 1 deletion src/SlmLocale/Service/LocaleUrlViewHelperFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

use Interop\Container\ContainerInterface;
use Interop\Container\Exception\ContainerException;
use SlmLocale\Locale\Detector;
use SlmLocale\View\Helper\LocaleUrl;
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
use Zend\ServiceManager\Exception\ServiceNotFoundException;
Expand All @@ -65,7 +66,7 @@ class LocaleUrlViewHelperFactory implements FactoryInterface
*/
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$detector = $container->get('SlmLocale\Locale\Detector');
$detector = $container->get(Detector::class);
$request = $container->get('Request');
$app = $container->get('Application');

Expand Down
31 changes: 15 additions & 16 deletions src/SlmLocale/Strategy/StrategyPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,28 +51,27 @@ class StrategyPluginManager extends AbstractPluginManager
* {@inheritdoc}
*/
protected $aliases = [
'cookie' => 'SlmLocale\Strategy\CookieStrategy',
'host' => 'SlmLocale\Strategy\HostStrategy',
'acceptlanguage' => 'SlmLocale\Strategy\HttpAcceptLanguageStrategy',
'query' => 'SlmLocale\Strategy\QueryStrategy',
'uripath' => 'SlmLocale\Strategy\UriPathStrategy',
'cookie' => CookieStrategy::class,
'host' => HostStrategy::class,
'acceptlanguage' => HttpAcceptLanguageStrategy::class,
'query' => QueryStrategy::class,
'uripath' => UriPathStrategy::class,
];

/**
* {@inheritdoc}
*/
protected $factories = [
'SlmLocale\Strategy\CookieStrategy' => InvokableFactory::class,
'SlmLocale\Strategy\HostStrategy' => InvokableFactory::class,
'SlmLocale\Strategy\HttpAcceptLanguageStrategy' => InvokableFactory::class,
'SlmLocale\Strategy\QueryStrategy' => InvokableFactory::class,
'SlmLocale\Strategy\UriPathStrategy' => InvokableFactory::class,
'slmlocalestrategycookiestrategy' => InvokableFactory::class,
'slmlocalestrategyhoststrategy' => InvokableFactory::class,
'slmlocalestrategyhttpacceptlanguagestrategy' => InvokableFactory::class,
'slmlocalestrategyquerystrategy' => InvokableFactory::class,
'slmlocalestrategyuripathstrategy' => InvokableFactory::class,

CookieStrategy::class => InvokableFactory::class,
HostStrategy::class => InvokableFactory::class,
HttpAcceptLanguageStrategy::class => InvokableFactory::class,
QueryStrategy::class => InvokableFactory::class,
UriPathStrategy::class => InvokableFactory::class,
'slmlocalestrategycookiestrategy' => InvokableFactory::class,
'slmlocalestrategyhoststrategy' => InvokableFactory::class,
'slmlocalestrategyhttpacceptlanguagestrategy' => InvokableFactory::class,
'slmlocalestrategyquerystrategy' => InvokableFactory::class,
'slmlocalestrategyuripathstrategy' => InvokableFactory::class,
];

/**
Expand Down
14 changes: 8 additions & 6 deletions tests/SlmLocaleTest/Locale/DetectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
use SlmLocale\Locale\Detector;
use SlmLocale\LocaleEvent;

use SlmLocale\Strategy\AbstractStrategy;
use SlmLocale\Strategy\StrategyInterface;
use Zend\EventManager\EventManager;
use Zend\Stdlib\Request;
use Zend\Stdlib\Response;
Expand All @@ -55,7 +57,7 @@ public function testDetectEventUsesLocaleEventObject()

$self = $this;
$this->setEventManager($detector, LocaleEvent::EVENT_DETECT, function ($e) use ($self) {
$self->assertInstanceOf('SlmLocale\LocaleEvent', $e);
$self->assertInstanceOf(LocaleEvent::class, $e);
});

$detector->detect(new Request(), new Response());
Expand Down Expand Up @@ -169,8 +171,8 @@ public function testEmptySupportedListIndicatesNoSupportedList()
public function testStrategyAttachesToEventManager()
{
$detector = new Detector();
$events = $this->getMock('Zend\EventManager\EventManager');
$strategy = $this->getMock('SlmLocale\Strategy\StrategyInterface');
$events = $this->getMock(EventManager::class);
$strategy = $this->getMock(StrategyInterface::class);
$strategy->expects($this->once())
->method('attach')
->with($events);
Expand All @@ -184,12 +186,12 @@ public function testStrategyWithHighestPriorityWins()
$detector = new Detector();
$this->setEventManager($detector);

$strategy1 = $this->getMock('SlmLocale\Strategy\AbstractStrategy', ['detect']);
$strategy1 = $this->getMock(AbstractStrategy::class, ['detect']);
$strategy1->expects($this->once())
->method('detect')
->will($this->returnValue('Foo'));

$strategy2 = $this->getMock('SlmLocale\Strategy\AbstractStrategy', ['detect']);
$strategy2 = $this->getMock(AbstractStrategy::class, ['detect']);
$strategy2->expects($this->never())
->method('detect');

Expand All @@ -206,7 +208,7 @@ public function testFoundEventUsesLocaleEventObject()

$self = $this;
$this->setEventManager($detector, LocaleEvent::EVENT_FOUND, function ($e) use ($self) {
$self->assertInstanceOf('SlmLocale\LocaleEvent', $e);
$self->assertInstanceOf(LocaleEvent::class, $e);
});

$detector->detect(new Request(), new Response());
Expand Down
53 changes: 29 additions & 24 deletions tests/SlmLocaleTest/Service/DetectFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@

use PHPUnit_Framework_TestCase as TestCase;

use SlmLocale\Locale\Detector;
use SlmLocale\Service\DetectorFactory;
use SlmLocale\Strategy\Factory\StrategyPluginManagerFactory;
use SlmLocale\Strategy\StrategyInterface;
use SlmLocale\Strategy\StrategyPluginManager;
use Zend\EventManager\EventManager;
use Zend\ServiceManager\ServiceManager;

Expand All @@ -49,15 +54,15 @@ class DetectFactoryTest extends TestCase
public function testFactoryInstantiatesDetector()
{
$sl = $this->getServiceLocator();
$detector = $sl->get('SlmLocale\Locale\Detector');
$detector = $sl->get(Detector::class);

$this->assertInstanceOf('SlmLocale\Locale\Detector', $detector);
$this->assertInstanceOf(Detector::class, $detector);
}

public function testDefaultLocaleIsOptional()
{
$sl = $this->getServiceLocator();
$detector = $sl->get('SlmLocale\Locale\Detector');
$detector = $sl->get(Detector::class);

$this->assertNull($detector->getDefault());
}
Expand All @@ -67,15 +72,15 @@ public function testDefaultLocaleIsSet()
$sl = $this->getServiceLocator([
'default' => 'Foo',
]);
$detector = $sl->get('SlmLocale\Locale\Detector');
$detector = $sl->get(Detector::class);

$this->assertEquals('Foo', $detector->getDefault());
}

public function testSupportedLocalesAreOptional()
{
$sl = $this->getServiceLocator();
$detector = $sl->get('SlmLocale\Locale\Detector');
$detector = $sl->get(Detector::class);

$this->assertNull($detector->getSupported());
}
Expand All @@ -85,7 +90,7 @@ public function testSupportedLocalesAreSet()
$sl = $this->getServiceLocator([
'supported' => ['Foo', 'Bar'],
]);
$detector = $sl->get('SlmLocale\Locale\Detector');
$detector = $sl->get(Detector::class);

$this->assertEquals(['Foo', 'Bar'], $detector->getSupported());
}
Expand All @@ -98,14 +103,14 @@ public function testUseServiceLocatorToInstantiateStrategy()

$self = $this;
$called = false;
$plugins = $sl->get('SlmLocale\Strategy\StrategyPluginManager');
$plugins = $sl->get(StrategyPluginManager::class);
$plugins->setFactory('TestStrategy', function () use ($self, &$called) {
$called = true;

return $self->getMock('SlmLocale\Strategy\StrategyInterface');
return $self->getMock(StrategyInterface::class);
});

$detector = $sl->get('SlmLocale\Locale\Detector');
$detector = $sl->get(Detector::class);
$this->assertTrue($called);
}

Expand All @@ -117,21 +122,21 @@ public function testConfigurationCanHoldMultipleStrategies()

$self = $this;
$called1 = false;
$plugins = $sl->get('SlmLocale\Strategy\StrategyPluginManager');
$plugins = $sl->get(StrategyPluginManager::class);
$plugins->setFactory('TestStrategy1', function () use ($self, &$called1) {
$called1 = true;

return $self->getMock('SlmLocale\Strategy\StrategyInterface');
return $self->getMock(StrategyInterface::class);
});

$called2 = false;
$plugins->setFactory('TestStrategy2', function () use ($self, &$called2) {
$called2 = true;

return $self->getMock('SlmLocale\Strategy\StrategyInterface');
return $self->getMock(StrategyInterface::class);
});

$detector = $sl->get('SlmLocale\Locale\Detector');
$detector = $sl->get(Detector::class);
$this->assertTrue($called1);
$this->assertTrue($called2);
}
Expand All @@ -146,14 +151,14 @@ public function testStrategyConfigurationCanBeAnArray()

$self = $this;
$called = false;
$plugins = $sl->get('SlmLocale\Strategy\StrategyPluginManager');
$plugins = $sl->get(StrategyPluginManager::class);
$plugins->setFactory('TestStrategy', function () use ($self, &$called) {
$called = true;

return $self->getMock('SlmLocale\Strategy\StrategyInterface');
return $self->getMock(StrategyInterface::class);
});

$detector = $sl->get('SlmLocale\Locale\Detector');
$detector = $sl->get(Detector::class);
$this->assertTrue($called);
}

Expand All @@ -166,14 +171,14 @@ public function testStrategyCanBeAttachedWithPriorities()
]);
$em = $sl->get('EventManager');

$strategy = $this->getMock('SlmLocale\Strategy\StrategyInterface', ['attach', 'detach']);
$strategy = $this->getMock(StrategyInterface::class, ['attach', 'detach']);
$strategy->expects($this->once())
->method('attach')
->with($em, 100);
$plugins = $sl->get('SlmLocale\Strategy\StrategyPluginManager');
$plugins = $sl->get(StrategyPluginManager::class);
$plugins->setService('TestStrategy', $strategy);

$detector = $sl->get('SlmLocale\Locale\Detector');
$detector = $sl->get(Detector::class);
}

public function testStrategyCanBeInstantiatedWithOptions()
Expand All @@ -183,14 +188,14 @@ public function testStrategyCanBeInstantiatedWithOptions()
['name' => 'TestStrategy', 'options' => 'Foo'],
],
]);
$strategy = $this->getMock('SlmLocale\Strategy\StrategyInterface', ['attach', 'detach', 'setOptions']);
$strategy = $this->getMock(StrategyInterface::class, ['attach', 'detach', 'setOptions']);
$strategy->expects($this->once())
->method('setOptions')
->with('Foo');
$plugins = $sl->get('SlmLocale\Strategy\StrategyPluginManager');
$plugins = $sl->get(StrategyPluginManager::class);
$plugins->setService('TestStrategy', $strategy);

$detector = $sl->get('SlmLocale\Locale\Detector');
$detector = $sl->get(Detector::class);
}

public function getServiceLocator(array $config = [])
Expand All @@ -201,8 +206,8 @@ public function getServiceLocator(array $config = [])
],
];
$serviceLocator = new ServiceManager();
$serviceLocator->setFactory('SlmLocale\Locale\Detector', 'SlmLocale\Service\DetectorFactory');
$serviceLocator->setFactory('SlmLocale\Strategy\StrategyPluginManager', 'SlmLocale\Strategy\Factory\StrategyPluginManagerFactory');
$serviceLocator->setFactory(Detector::class, DetectorFactory::class);
$serviceLocator->setFactory(StrategyPluginManager::class, StrategyPluginManagerFactory::class);
$serviceLocator->setService('EventManager', new EventManager());
$serviceLocator->setService('config', $config);

Expand Down
Loading

0 comments on commit 9f03a42

Please sign in to comment.