Commit ddbcf351 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Nouveau service UrlTheseService ; nouvelle aide de vue UrlThese ;...

Nouveau service UrlTheseService ; nouvelle aide de vue UrlThese ; refactorisation du plugin UrlThese.
parent da5fffac
Loading
Loading
Loading
Loading
+16 −7
Changes for module/Application/config/others/these.config.php: 16 added lines, 7 removed lines.
Original line number Diff line number Diff line
<?php

use Application\Assertion\AssertionAbstractFactory;
use Application\Assertion\TheseAssertion;
use Application\Controller\Factory\TheseControllerFactory;
use Application\Controller\Factory\TheseObserverControllerFactory;
use Application\Controller\TheseController;
use Application\Controller\TheseObserverController;
use Application\Controller\Plugin\Url\UrlThesePluginFactory;
use Application\Entity\Db\Diffusion;
use Application\Entity\Db\NatureFichier;
use Application\Entity\Db\VersionFichier;
use Application\Entity\Db\WfEtape;
use Application\Form\Factory\AttestationHydratorFactory;
use Application\Form\Factory\AttestationTheseFormFactory;
@@ -21,6 +18,10 @@ use Application\Service\Message\DiffusionMessages;
use Application\Service\ServiceAwareInitializer;
use Application\Service\These\TheseObserverService;
use Application\Service\These\TheseService;
use Application\Service\Url\UrlServiceFactory;
use Application\Service\Url\UrlTheseService;
use Application\Service\Url\UrlTheseServiceFactory;
use Application\View\Helper\Url\UrlTheseHelperFactory;
use UnicaenAuth\Guard\PrivilegeController;
use UnicaenAuth\Provider\Rule\PrivilegeRuleProvider;

@@ -868,13 +869,14 @@ return [
        ],
        'abstract_factories' => [
            AssertionAbstractFactory::class,
            UrlServiceFactory::class, // construit: 'urlTheseService'
        ],
    ],
    'controllers'     => [
        'invokables' => [
            'Application\Controller\These' => TheseController::class,
        ],
        'factories' => [
            'Application\Controller\These' => TheseControllerFactory::class,
            'Application\Controller\TheseObserver' => TheseObserverControllerFactory::class,
        ],
        'aliases' => [
@@ -883,7 +885,14 @@ return [
    ],
    'controller_plugins' => [
        'invokables' => [
            'urlThese'              => 'Application\Controller\Plugin\UrlThese',
        ],
        'factories' => [
            'urlThese' => UrlThesePluginFactory::class,
        ],
    ],
    'view_helpers' => [
        'factories' => [
            'urlThese' => UrlTheseHelperFactory::class,
        ],
    ],

+89
Changes for module/Application/src/Application/Controller/Plugin/Url/UrlThesePlugin.php: 89 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Application\Controller\Plugin\Url;

use Application\Entity\Db\These;
use Application\Service\Url\UrlTheseService;
use Zend\Mvc\Controller\Plugin\AbstractPlugin;

/**
 * Aide de vue de génération d'URL.
 *
 * @method depotFichiers(These $these, $nature, $version = null, $retraite = false, array $queryParams = [])
 * @method identiteThese(These $these)
 * @method depotThese(These $these)
 * @method archivageThese(These $these, $version)
 * @method testArchivabilite(These $these, $version)
 * @method creerVersionRetraitee(These $these, $version)
 * @method archivabiliteThese(These $these, $version, $retraite = false)
 * @method conformiteTheseRetraitee(These $these, $version)
 * @method validationFichierRetraiteUrl(These $these)
 * @method attestationThese(These $these, $version)
 * @method diffusionThese(These $these, $version)
 * @method modifierMetadonneesUrl(These $these)
 * @method certifierConformiteTheseRetraiteUrl(These $these, $version)
 * @method modifierAttestationUrl(These $these)
 * @method modifierDiffusionUrl(These $these)
 * @method exporterConventionMiseEnLigneUrl(These $these)
 * @method modifierRdvBuUrl(These $these)
 * @method validerRdvBuUrl(These $these)
 * @method devaliderRdvBuUrl(These $these)
 * @method validationDepotTheseCorrigeeUrl(These $these)
 * @method validationCorrectionTheseUrl(These $these)
 * @method validerDepotTheseCorrigeeUrl(These $these)
 * @method devaliderDepotTheseCorrigeeUrl(These $these)
 * @method validerCorrectionTheseUrl(These $these)
 * @method devaliderCorrectionTheseUrl(These $these)
 *
 * @see UrlTheseService
 *
 * @author Unicaen
 */
class UrlThesePlugin extends AbstractPlugin
{
    /**
     * @var UrlTheseService
     */
    protected $urlTheseService;

    /**
     * @param UrlTheseService $urlTheseService
     * @return self
     */
    public function setUrlTheseService(UrlTheseService $urlTheseService)
    {
        $this->urlTheseService = $urlTheseService;

        return $this;
    }

    /**
     * @param array $options
     * @return self
     */
    public function setOptions(array $options = [])
    {
        $this->urlTheseService->setOptions($options);

        return $this;
    }

    /**
     * @param bool $forceCanonical
     * @return self
     */
    public function setForceCanonical($forceCanonical = true)
    {
        return $this->setOptions(['force_canonical' => $forceCanonical]);
    }

    /**
     * @param $name
     * @param $arguments
     * @return string
     */
    public function __call($name, $arguments)
    {
        return call_user_func_array([$this->urlTheseService, $name], $arguments);
    }
}
 No newline at end of file
+20
Changes for module/Application/src/Application/Controller/Plugin/Url/UrlThesePluginFactory.php: 20 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Application\Controller\Plugin\Url;

use Application\Service\Url\UrlTheseService;
use Zend\Mvc\Controller\PluginManager;

class UrlThesePluginFactory
{
    public function __invoke(PluginManager $helperPluginManager)
    {
        /** @var UrlTheseService $urlService */
        $urlService = $helperPluginManager->getServiceLocator()->get(UrlTheseService::class);

        $service = new UrlThesePlugin();
        $service->setUrlTheseService($urlService);

        return $service;
    }
}
+131
Changes for module/Application/src/Application/Service/Url/UrlService.php: 131 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Application\Service\Url;

use Application\Filter\IdifyFilterAwareTrait;
use Application\RouteMatch;
use Traversable;
use UnicaenApp\Exception\RuntimeException;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\Router\RouteStackInterface;

abstract class UrlService
{
    use IdifyFilterAwareTrait;

    /**
     * @var array
     */
    protected $options = [];

    /**
     * @param array $options
     * @return self
     */
    public function setOptions(array $options = [])
    {
        $this->options = $options;
        return $this;
    }

    /**
     * RouteStackInterface instance.
     *
     * @var RouteStackInterface
     */
    protected $router;

    /**
     * RouteInterface match returned by the router.
     *
     * @var RouteMatch
     */
    protected $routeMatch;

    /**
     * Generates a url given the name of a route.
     *
     * @param  string               $name               Name of the route
     * @param  array                $params             Parameters for the link
     * @param  array|Traversable    $options            Options for the route
     * @param  bool                 $reuseMatchedParams Whether to reuse matched parameters
     * @return string Url                         For the link href attribute
     */
    public function fromRoute($name = null, $params = array(), $options = array(), $reuseMatchedParams = false)
    {
        $options = array_merge($this->options, $options);

        if (null === $this->router) {
            throw new RuntimeException('No RouteStackInterface instance provided');
        }

        if (3 == func_num_args() && is_bool($options)) {
            $reuseMatchedParams = $options;
            $options = array();
        }

        if ($name === null) {
            if ($this->routeMatch === null) {
                throw new RuntimeException('No RouteMatch instance provided');
            }

            $name = $this->routeMatch->getMatchedRouteName();

            if ($name === null) {
                throw new RuntimeException('RouteMatch does not contain a matched route name');
            }
        }

        if (!is_array($params)) {
            if (!$params instanceof Traversable) {
                throw new \InvalidArgumentException(
                    'Params is expected to be an array or a Traversable object'
                );
            }
            $params = iterator_to_array($params);
        }

        if ($reuseMatchedParams && $this->routeMatch !== null) {
            $routeMatchParams = $this->routeMatch->getParams();

            if (isset($routeMatchParams[ModuleRouteListener::ORIGINAL_CONTROLLER])) {
                $routeMatchParams['controller'] = $routeMatchParams[ModuleRouteListener::ORIGINAL_CONTROLLER];
                unset($routeMatchParams[ModuleRouteListener::ORIGINAL_CONTROLLER]);
            }

            if (isset($routeMatchParams[ModuleRouteListener::MODULE_NAMESPACE])) {
                unset($routeMatchParams[ModuleRouteListener::MODULE_NAMESPACE]);
            }

            $params = array_merge($routeMatchParams, $params);
        }

        $options['name'] = $name;

        return $this->router->assemble($params, $options);
    }

    /**
     * Set the router to use for assembling.
     *
     * @param RouteStackInterface $router
     * @return self
     */
    public function setRouter(RouteStackInterface $router)
    {
        $this->router = $router;
        return $this;
    }

    /**
     * Set route match returned by the router.
     *
     * @param  RouteMatch $routeMatch
     * @return self
     */
    public function setRouteMatch(RouteMatch $routeMatch)
    {
        $this->routeMatch = $routeMatch;
        return $this;
    }
}
 No newline at end of file
+54
Changes for module/Application/src/Application/Service/Url/UrlServiceFactory.php: 54 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Application\Service\Url;

use Application\RouteMatch;
use Zend\Console\Console;
use Zend\Mvc\Router\RouteStackInterface;
use Zend\ServiceManager\AbstractFactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class UrlServiceFactory implements AbstractFactoryInterface
{
    /**
     * Determine if we can create a service with name
     *
     * @param ServiceLocatorInterface $serviceLocator
     * @param                         $name
     * @param                         $requestedName
     * @return bool
     */
    public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
    {
        $requestedClass = $requestedName;
        $isInNamespace = substr($requestedClass, 0, strlen(__NAMESPACE__)) === __NAMESPACE__;

        return $isInNamespace;
    }

    /**
     * Create service with name
     *
     * @param ServiceLocatorInterface $serviceLocator
     * @param                         $name
     * @param                         $requestedName
     * @return UrlService
     */
    public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
    {
        $class = $requestedName;

        /** @var UrlService $service */
        $service = new $class();

        /** @var RouteStackInterface $router */
        $router = $serviceLocator->get(Console::isConsole() ? 'HttpRouter' : 'Router');
        $match = $serviceLocator->get('application')->getMvcEvent()->getRouteMatch();
        if ($match instanceof RouteMatch) {
            $service->setRouteMatch($match);
        }
        $service->setRouter($router);

        return $service;
    }
}
 No newline at end of file
Loading