Skip to content

Commit

Permalink
Merge pull request #29 from juriansluiman/feature/view-helpers-new
Browse files Browse the repository at this point in the history
Create view helpers hooking into ASSEMBLE event
  • Loading branch information
Jurian Sluiman committed Sep 17, 2013
2 parents fedee0f + 0eb805d commit a9f3fad
Show file tree
Hide file tree
Showing 9 changed files with 642 additions and 5 deletions.
11 changes: 11 additions & 0 deletions config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,15 @@
'SlmLocale\Locale\Detector' => 'SlmLocale\Service\DetectorFactory',
),
),

'view_helpers' => array(
'aliases' => array(
'localeUrl' => 'SlmLocale\View\Helper\LocaleUrl',
'localeMenu' => 'SlmLocale\View\Helper\LocaleMenu',
),
'factories' => array(
'SlmLocale\View\Helper\LocaleUrl' => 'SlmLocale\Service\LocaleUrlViewHelperFactory',
'SlmLocale\View\Helper\LocaleMenu' => 'SlmLocale\Service\LocaleMenuViewHelperFactory',
),
),
);
25 changes: 25 additions & 0 deletions src/SlmLocale/Locale/Detector.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
use Zend\EventManager\EventManagerInterface;
use Zend\Stdlib\RequestInterface;
use Zend\Stdlib\ResponseInterface;
use Zend\Uri\Uri;

class Detector implements EventManagerAwareInterface
{
Expand Down Expand Up @@ -169,4 +170,28 @@ public function detect(RequestInterface $request, ResponseInterface $response =

return $locale;
}

public function assemble($locale, $uri)
{
$event = new LocaleEvent(LocaleEvent::EVENT_ASSEMBLE, $this);
$event->setLocale($locale);

if ($this->hasSupported()) {
$event->setSupported($this->getSupported());
}

if (!$uri instanceof Uri) {
$uri = new Uri($uri);
}
$event->setUri($uri);

$events = $this->getEventManager();
$results = $events->trigger($event);

if (!$results->stopped()) {
return $uri;
}

return $results->last();
}
}
30 changes: 28 additions & 2 deletions src/SlmLocale/LocaleEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,19 @@
use Zend\EventManager\Event;
use Zend\Stdlib\RequestInterface;
use Zend\Stdlib\ResponseInterface;
use Zend\Uri\Uri;

class LocaleEvent extends Event
{
const EVENT_DETECT = 'detect';
const EVENT_FOUND = 'found';
const EVENT_DETECT = 'detect';
const EVENT_FOUND = 'found';
const EVENT_ASSEMBLE = 'assemble';

protected $request;
protected $response;
protected $supported;
protected $locale;
protected $uri;

public function getRequest()
{
Expand Down Expand Up @@ -106,4 +109,27 @@ public function setLocale($locale)
$this->locale = $locale;
return $this;
}

/**
* Get uri for assemble event
*
* @return Uri
*/
public function getUri()
{
return $this->uri;
}

/**
* Set uri for assemble event
*
* @param Uri $uri
* @return self
*/
public function setUri(Uri $uri)
{
$this->setParam('uri', $uri);
$this->uri = $uri;
return $this;
}
}
62 changes: 62 additions & 0 deletions src/SlmLocale/Service/LocaleMenuViewHelperFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* Copyright (c) 2012-2013 Jurian Sluiman.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the names of the copyright holders nor the names of the
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @author Jurian Sluiman <jurian@juriansluiman.nl>
* @copyright 2012-2013 Jurian Sluiman.
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://juriansluiman.nl
*/

namespace SlmLocale\Service;

use SlmLocale\View\Helper\LocaleMenu;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class LocaleMenuViewHelperFactory implements FactoryInterface
{
/**
* @param ServiceLocatorInterface $serviceLocator
* @return LocaleMenu
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
$detector = $serviceLocator->getServiceLocator()->get('SlmLocale\Locale\Detector');

$helper = new LocaleMenu;
$helper->setDetector($detector);

return $helper;
}
}
65 changes: 65 additions & 0 deletions src/SlmLocale/Service/LocaleUrlViewHelperFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/**
* Copyright (c) 2012-2013 Jurian Sluiman.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the names of the copyright holders nor the names of the
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @author Jurian Sluiman <jurian@juriansluiman.nl>
* @copyright 2012-2013 Jurian Sluiman.
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://juriansluiman.nl
*/

namespace SlmLocale\Service;

use SlmLocale\View\Helper\LocaleUrl;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class LocaleUrlViewHelperFactory implements FactoryInterface
{
/**
* @param ServiceLocatorInterface $serviceLocator
* @return LocaleUrl
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
$sl = $serviceLocator->getServiceLocator();

$detector = $sl->get('SlmLocale\Locale\Detector');
$request = $sl->get('Request');
$app = $sl->get('Application');

$match = $app->getMvcEvent()->getRouteMatch();
$helper = new LocaleUrl($detector, $request, $match);
return $helper;
}
}
11 changes: 8 additions & 3 deletions src/SlmLocale/Strategy/AbstractStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,16 @@ abstract class AbstractStrategy implements StrategyInterface
protected $listeners = array();

/**
* Attach "detect" and "found" listeners
* Attach "detect", "found" and "assemble" listeners
*
* @param EventManagerInterface $events
* @param int $priority
*/
public function attach(EventManagerInterface $events, $priority = 1)
{
$this->listeners[] = $events->attach(LocaleEvent::EVENT_DETECT, array($this, 'detect'), $priority);
$this->listeners[] = $events->attach(LocaleEvent::EVENT_FOUND, array($this, 'found'), $priority);
$this->listeners[] = $events->attach(LocaleEvent::EVENT_DETECT, array($this, 'detect'), $priority);
$this->listeners[] = $events->attach(LocaleEvent::EVENT_FOUND, array($this, 'found'), $priority);
$this->listeners[] = $events->attach(LocaleEvent::EVENT_ASSEMBLE, array($this, 'assemble'), $priority);
}

/**
Expand All @@ -88,6 +89,10 @@ public function found(LocaleEvent $event)
{
}

public function assemble(LocaleEvent $event)
{
}

protected function isHttpRequest(RequestInterface $request)
{
return $request instanceof HttpRequest;
Expand Down
14 changes: 14 additions & 0 deletions src/SlmLocale/Strategy/UriPathStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

namespace SlmLocale\Strategy;

use Locale;
use SlmLocale\LocaleEvent;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
Expand Down Expand Up @@ -188,6 +189,19 @@ public function found(LocaleEvent $event)
return $response;
}

public function assemble(LocaleEvent $event)
{
$current = Locale::getDefault();
$locale = $event->getLocale();
$uri = $event->getUri();
$path = $uri->getPath();

$path = str_replace($current, $locale, $path);
$uri->setPath($path);

return $uri;
}

protected function getFirstSegmentInPath(RequestInterface $request, $base = null)
{
$path = $request->getUri()->getPath();
Expand Down
Loading

0 comments on commit a9f3fad

Please sign in to comment.