The Gitlab instance will be restarted on Monday April 28th at 2AM. There will be a short interruption of service.

Skip to content
Snippets Groups Projects
Commit ce5b9523 authored by Demian Katz's avatar Demian Katz
Browse files

Refactored route generation to a class.

parent ab3e697e
No related merge requests found
...@@ -695,13 +695,6 @@ $recordRoutes = [ ...@@ -695,13 +695,6 @@ $recordRoutes = [
'summonrecord' => 'SummonRecord', 'summonrecord' => 'SummonRecord',
'worldcatrecord' => 'WorldcatRecord' 'worldcatrecord' => 'WorldcatRecord'
]; ];
// Record sub-routes are generally used to access tab plug-ins, but a few
// URLs are hard-coded to specific actions; this array lists those actions.
$nonTabRecordActions = [
'AddComment', 'DeleteComment', 'AddTag', 'Save', 'Email', 'SMS', 'Cite',
'Export', 'RDF', 'Hold', 'BlockedHold', 'Home', 'StorageRetrievalRequest', 'AjaxTab',
'BlockedStorageRetrievalRequest', 'ILLRequest', 'BlockedILLRequest', 'PDF',
];
// Define list-related routes -- route name => MyResearch action // Define list-related routes -- route name => MyResearch action
$listRoutes = ['userList' => 'MyList', 'editList' => 'EditList']; $listRoutes = ['userList' => 'MyList', 'editList' => 'EditList'];
...@@ -750,75 +743,10 @@ $staticRoutes = [ ...@@ -750,75 +743,10 @@ $staticRoutes = [
'Worldcat/Advanced', 'Worldcat/Home', 'Worldcat/Search' 'Worldcat/Advanced', 'Worldcat/Home', 'Worldcat/Search'
]; ];
// Build record routes $routeGenerator = new \VuFind\Route\RouteGenerator();
foreach ($recordRoutes as $routeBase => $controller) { $routeGenerator->addRecordRoutes($config, $recordRoutes);
// catch-all "tab" route: $routeGenerator->addListRoutes($config, $listRoutes);
$config['router']['routes'][$routeBase] = [ $routeGenerator->addStaticRoutes($config, $staticRoutes);
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => [
'route' => '/' . $controller . '/[:id[/:tab]]',
'constraints' => [
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
],
'defaults' => [
'controller' => $controller,
'action' => 'Home',
]
]
];
// special non-tab actions that each need their own route:
foreach ($nonTabRecordActions as $action) {
$config['router']['routes'][$routeBase . '-' . strtolower($action)] = [
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => [
'route' => '/' . $controller . '/[:id]/' . $action,
'constraints' => [
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
],
'defaults' => [
'controller' => $controller,
'action' => $action,
]
]
];
}
}
// Build list routes
foreach ($listRoutes as $routeName => $action) {
$config['router']['routes'][$routeName] = [
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => [
'route' => '/MyResearch/' . $action . '/[:id]',
'constraints' => [
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
],
'defaults' => [
'controller' => 'MyResearch',
'action' => $action,
]
]
];
}
// Build static routes
foreach ($staticRoutes as $route) {
list($controller, $action) = explode('/', $route);
$routeName = str_replace('/', '-', strtolower($route));
$config['router']['routes'][$routeName] = [
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => [
'route' => '/' . $route,
'defaults' => [
'controller' => $controller,
'action' => $action,
]
]
];
}
// Add the home route last // Add the home route last
$config['router']['routes']['home'] = [ $config['router']['routes']['home'] = [
......
<?php
/**
* Route Generator Class
*
* PHP version 5
*
* Copyright (C) Villanova University 2010.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* @category VuFind2
* @package Route
* @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://vufind.org/wiki/vufind2:developer_manual Wiki
*/
namespace VuFind\Route;
/**
* Route Generator Class
*
* The data model object representing a user's book cart.
*
* @category VuFind2
* @package Route
* @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://vufind.org/wiki/vufind2:developer_manual Wiki
*/
class RouteGenerator
{
/**
* Record sub-routes are generally used to access tab plug-ins, but a few
* URLs are hard-coded to specific actions; this array lists those actions.
*
* @var array
*/
protected $nonTabRecordActions;
/**
* Constructor
*
* @param array $nonTabRecordActions List of non-tab record actions (null
* for default).
*/
public function __construct(array $nonTabRecordActions = null)
{
if (null === $nonTabRecordActions) {
$this->nonTabRecordActions = [
'AddComment', 'DeleteComment', 'AddTag', 'Save', 'Email', 'SMS',
'Cite', 'Export', 'RDF', 'Hold', 'BlockedHold', 'Home',
'StorageRetrievalRequest', 'AjaxTab',
'BlockedStorageRetrievalRequest', 'ILLRequest', 'BlockedILLRequest',
'PDF',
];
} else {
$this->nonTabRecordActions = $nonTabRecordActions;
}
}
/**
* Add a list route to the configuration.
*
* @param array $config Configuration array to update
* @param string $routeName Name of route to generate
* @param string $action MyResearch action
*
* @return void
*/
public function addListRoute(& $config, $routeName, $action)
{
$config['router']['routes'][$routeName] = [
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => [
'route' => '/MyResearch/' . $action . '/[:id]',
'constraints' => [
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
],
'defaults' => [
'controller' => 'MyResearch',
'action' => $action,
]
]
];
}
/**
* Add list routes to the configuration.
*
* @param array $config Configuration array to update
* @param array $routes Associative array (route name => action) of routes
* to add.
*
* @return void
*/
public function addListRoutes(& $config, $routes)
{
// Build list routes
foreach ($routes as $routeName => $action) {
$this->addListRoute($config, $routeName, $action);
}
}
/**
* Add record route to the configuration.
*
* @param array $config Configuration array to update
* @param string $routeBase Base name to use for routes
* @param string $controller Controller to point routes toward
*
* @return void
*/
public function addRecordRoute(& $config, $routeBase, $controller)
{
// catch-all "tab" route:
$config['router']['routes'][$routeBase] = [
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => [
'route' => '/' . $controller . '/[:id[/:tab]]',
'constraints' => [
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
],
'defaults' => [
'controller' => $controller,
'action' => 'Home',
]
]
];
// special non-tab actions that each need their own route:
foreach ($this->nonTabRecordActions as $action) {
$config['router']['routes'][$routeBase . '-' . strtolower($action)] = [
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => [
'route' => '/' . $controller . '/[:id]/' . $action,
'constraints' => [
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
],
'defaults' => [
'controller' => $controller,
'action' => $action,
]
]
];
}
}
/**
* Add record routes to the configuration.
*
* @param array $config Configuration array to update
* @param array $routes Associative array (route base name => controller) of
* routes to add.
*
* @return void
*/
public function addRecordRoutes(& $config, $routes)
{
foreach ($routes as $routeBase => $controller) {
$this->addRecordRoute($config, $routeBase, $controller);
}
}
/**
* Add a simple static route to the configuration.
*
* @param array $config Configuration array to update
* @param string $route Controller/Action string representing route
*
* @return void
*/
public function addStaticRoute(& $config, $route)
{
list($controller, $action) = explode('/', $route);
$routeName = str_replace('/', '-', strtolower($route));
$config['router']['routes'][$routeName] = [
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => [
'route' => '/' . $route,
'defaults' => [
'controller' => $controller,
'action' => $action,
]
]
];
}
/**
* Add simple static routes to the configuration.
*
* @param array $config Configuration array to update
* @param array $routes Array of Controller/Action strings representing routes
*
* @return void
*/
public function addStaticRoutes(& $config, $routes)
{
foreach ($routes as $route) {
$this->addStaticRoute($config, $route);
}
}
}
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment