Implemented the usage of multiple viewMethods.
- This change allows the developer to sort methods with the usual Priority. The first valid method found shall be used. - Dependency for implementing methods in a REST API.
This commit is contained in:
parent
ea0cf92d74
commit
ed90b3badb
|
@ -38,6 +38,7 @@ namespace FuzeWorks\Event;
|
||||||
|
|
||||||
use FuzeWorks\Controller;
|
use FuzeWorks\Controller;
|
||||||
use FuzeWorks\Event;
|
use FuzeWorks\Event;
|
||||||
|
use FuzeWorks\Priority;
|
||||||
use FuzeWorks\View;
|
use FuzeWorks\View;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -58,9 +59,9 @@ class RouterCallViewEvent extends Event
|
||||||
/**
|
/**
|
||||||
* The function that will be loaded in the view
|
* The function that will be loaded in the view
|
||||||
*
|
*
|
||||||
* @var string
|
* @var array
|
||||||
*/
|
*/
|
||||||
public $viewMethod;
|
public $viewMethods;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The parameters that will be provided to the function in the view
|
* The parameters that will be provided to the function in the view
|
||||||
|
@ -90,12 +91,27 @@ class RouterCallViewEvent extends Event
|
||||||
*/
|
*/
|
||||||
public $controller;
|
public $controller;
|
||||||
|
|
||||||
public function init(View $view, Controller $controller, string $viewMethod, string $viewParameters, string $route)
|
public function init(View $view, Controller $controller, array $viewMethods, string $viewParameters, string $route)
|
||||||
{
|
{
|
||||||
$this->view = $view;
|
$this->view = $view;
|
||||||
$this->controller = $controller;
|
$this->controller = $controller;
|
||||||
$this->viewMethod = $viewMethod;
|
$this->viewMethods = $viewMethods;
|
||||||
$this->viewParameters = $viewParameters;
|
$this->viewParameters = $viewParameters;
|
||||||
$this->route = $route;
|
$this->route = $route;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a method which should be tried upon calling the view
|
||||||
|
*
|
||||||
|
* @param string $method
|
||||||
|
* @param int $priority
|
||||||
|
*/
|
||||||
|
public function addMethod(string $method, int $priority = Priority::NORMAL)
|
||||||
|
{
|
||||||
|
if (!isset($this->viewMethods[$priority]))
|
||||||
|
$this->viewMethods[$priority] = [];
|
||||||
|
|
||||||
|
if (!isset($this->viewMethods[$priority][$method]))
|
||||||
|
$this->viewMethods[$priority][] = $method;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -37,6 +37,7 @@
|
||||||
namespace FuzeWorks\Event;
|
namespace FuzeWorks\Event;
|
||||||
use FuzeWorks\Controller;
|
use FuzeWorks\Controller;
|
||||||
use FuzeWorks\Event;
|
use FuzeWorks\Event;
|
||||||
|
use FuzeWorks\Priority;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Event that gets fired when a view and controller are loaded.
|
* Event that gets fired when a view and controller are loaded.
|
||||||
|
@ -67,9 +68,9 @@ class RouterLoadViewAndControllerEvent extends Event
|
||||||
/**
|
/**
|
||||||
* The function that will be loaded in the view
|
* The function that will be loaded in the view
|
||||||
*
|
*
|
||||||
* @var string
|
* @var array
|
||||||
*/
|
*/
|
||||||
public $viewMethod;
|
public $viewMethods;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The parameters that will be provided to the function in the view
|
* The parameters that will be provided to the function in the view
|
||||||
|
@ -92,15 +93,30 @@ class RouterLoadViewAndControllerEvent extends Event
|
||||||
*/
|
*/
|
||||||
public $controller;
|
public $controller;
|
||||||
|
|
||||||
public function init(string $viewName, string $viewType, string $viewMethod, string $viewParameters, string $route)
|
public function init(string $viewName, string $viewType, array $viewMethods, string $viewParameters, string $route)
|
||||||
{
|
{
|
||||||
$this->viewName = $viewName;
|
$this->viewName = $viewName;
|
||||||
$this->viewType = $viewType;
|
$this->viewType = $viewType;
|
||||||
$this->viewMethod = $viewMethod;
|
$this->viewMethods = $viewMethods;
|
||||||
$this->viewParameters = $viewParameters;
|
$this->viewParameters = $viewParameters;
|
||||||
$this->route = $route;
|
$this->route = $route;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a method which should be tried upon calling the view
|
||||||
|
*
|
||||||
|
* @param string $method
|
||||||
|
* @param int $priority
|
||||||
|
*/
|
||||||
|
public function addMethod(string $method, int $priority = Priority::NORMAL)
|
||||||
|
{
|
||||||
|
if (!isset($this->viewMethods[$priority]))
|
||||||
|
$this->viewMethods[$priority] = [];
|
||||||
|
|
||||||
|
if (!isset($this->viewMethods[$priority][$method]))
|
||||||
|
$this->viewMethods[$priority][] = $method;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Override the controller to be provided to the view.
|
* Override the controller to be provided to the view.
|
||||||
*
|
*
|
||||||
|
|
|
@ -325,7 +325,8 @@ class Router
|
||||||
$event = Events::fireEvent('routerLoadViewAndControllerEvent',
|
$event = Events::fireEvent('routerLoadViewAndControllerEvent',
|
||||||
$viewName,
|
$viewName,
|
||||||
$viewType,
|
$viewType,
|
||||||
$viewMethod,
|
// ViewMethod is provided as a Priority::NORMAL method
|
||||||
|
[3 => [$viewMethod]],
|
||||||
$viewParameters,
|
$viewParameters,
|
||||||
$route
|
$route
|
||||||
);
|
);
|
||||||
|
@ -363,7 +364,7 @@ class Router
|
||||||
$event = Events::fireEvent('routerCallViewEvent',
|
$event = Events::fireEvent('routerCallViewEvent',
|
||||||
$this->view,
|
$this->view,
|
||||||
$this->controller,
|
$this->controller,
|
||||||
$event->viewMethod,
|
$event->viewMethods,
|
||||||
$event->viewParameters,
|
$event->viewParameters,
|
||||||
$event->route
|
$event->route
|
||||||
);
|
);
|
||||||
|
@ -383,21 +384,27 @@ class Router
|
||||||
if (isset($this->view->halt))
|
if (isset($this->view->halt))
|
||||||
throw new HaltException("Will not load view. Cancelled by 'halt' attribute in view.");
|
throw new HaltException("Will not load view. Cancelled by 'halt' attribute in view.");
|
||||||
|
|
||||||
// Check if requested function or magic method exists in view
|
// Cycle over every viewMethod until a valid one is found
|
||||||
if (method_exists($this->view, $event->viewMethod) || method_exists($this->view, '__call'))
|
for ($i=Priority::getHighestPriority(); $i<=Priority::getLowestPriority(); $i++) {
|
||||||
{
|
if (!isset($event->viewMethods[$i]))
|
||||||
// Execute the function on the view
|
continue;
|
||||||
Logger::newLevel("Calling method '{$event->viewMethod}' on " . get_class($this->view) . ' with ' . get_class($this->controller));
|
|
||||||
$output = $this->view->{$event->viewMethod}($event->viewParameters);
|
foreach ($event->viewMethods[$i] as $method) {
|
||||||
Logger::stopLevel();
|
if (method_exists($this->view, $method))
|
||||||
return $output;
|
{
|
||||||
}
|
// Execute this method on the view
|
||||||
else
|
Logger::newLevel("Calling method '{$method}' on " . get_class($this->view) . ' with ' . get_class($this->controller));
|
||||||
{
|
$output = $this->view->{$method}($event->viewParameters);
|
||||||
Logger::logError("Could not load view. View does not have method '".$event->viewMethod."'");
|
Logger::stopLevel();
|
||||||
|
return $output;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// View could not be found
|
// Otherwise log an error
|
||||||
|
Logger::logError("Could not load view. View does not have any of the provided methods.");
|
||||||
|
|
||||||
|
// View could not be found.
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -331,7 +331,7 @@ class RouterTest extends MVCRTestAbstract
|
||||||
$this->assertInstanceOf('\FuzeWorks\Event\RouterLoadViewAndControllerEvent', $event);
|
$this->assertInstanceOf('\FuzeWorks\Event\RouterLoadViewAndControllerEvent', $event);
|
||||||
$this->assertEquals('TestDefaultCallable', $event->viewName);
|
$this->assertEquals('TestDefaultCallable', $event->viewName);
|
||||||
$this->assertEquals('test', $event->viewType);
|
$this->assertEquals('test', $event->viewType);
|
||||||
$this->assertEquals('missing', $event->viewMethod);
|
$this->assertEquals([3=>['missing']], $event->viewMethods);
|
||||||
$event->setCancelled(true);
|
$event->setCancelled(true);
|
||||||
}, 'routerLoadViewAndControllerEvent');
|
}, 'routerLoadViewAndControllerEvent');
|
||||||
|
|
||||||
|
@ -365,6 +365,32 @@ class RouterTest extends MVCRTestAbstract
|
||||||
$this->assertEquals($mockController, $this->router->getCurrentController());
|
$this->assertEquals($mockController, $this->router->getCurrentController());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @depends testDefaultCallableReplaceController
|
||||||
|
* @covers ::defaultCallable
|
||||||
|
* @covers \FuzeWorks\Event\RouterLoadViewAndControllerEvent::addMethod
|
||||||
|
*/
|
||||||
|
public function testDefaultCallableAddMethod()
|
||||||
|
{
|
||||||
|
$matches = [
|
||||||
|
'viewName' => 'TestDefaultCallableChangeMethod',
|
||||||
|
'viewType' => 'test',
|
||||||
|
'viewMethod' => 'index'
|
||||||
|
];
|
||||||
|
|
||||||
|
$this->assertNull($this->router->getCurrentController());
|
||||||
|
$this->assertNull($this->router->getCurrentView());
|
||||||
|
|
||||||
|
$mockController = $this->getMockBuilder('\FuzeWorks\Controller')->getMock();
|
||||||
|
// Create listener
|
||||||
|
Events::addListener(function($event, $mockController){
|
||||||
|
$event->overrideController($mockController);
|
||||||
|
$event->addMethod('altered', Priority::HIGH);
|
||||||
|
}, 'routerLoadViewAndControllerEvent', Priority::NORMAL, $mockController);
|
||||||
|
|
||||||
|
$this->assertEquals('Altered!', $this->router->defaultCallable($matches, '.*$'));
|
||||||
|
}
|
||||||
|
|
||||||
/* route() ------------------------------------------------------------ */
|
/* route() ------------------------------------------------------------ */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -0,0 +1,52 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* FuzeWorks CLIComponent.
|
||||||
|
*
|
||||||
|
* The FuzeWorks PHP FrameWork
|
||||||
|
*
|
||||||
|
* Copyright (C) 2013-2019 TechFuze
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*
|
||||||
|
* @author TechFuze
|
||||||
|
* @copyright Copyright (c) 2013 - 2019, TechFuze. (http://techfuze.net)
|
||||||
|
* @license https://opensource.org/licenses/MIT MIT License
|
||||||
|
*
|
||||||
|
* @link http://techfuze.net/fuzeworks
|
||||||
|
* @since Version 1.2.0
|
||||||
|
*
|
||||||
|
* @version Version 1.2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Application\View;
|
||||||
|
use FuzeWorks\View;
|
||||||
|
|
||||||
|
class TestDefaultCallableChangeMethodTestView extends View
|
||||||
|
{
|
||||||
|
|
||||||
|
public function index()
|
||||||
|
{
|
||||||
|
return "Not altered!";
|
||||||
|
}
|
||||||
|
|
||||||
|
public function altered()
|
||||||
|
{
|
||||||
|
return "Altered!";
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue