Created MVCR Framework

Implemented MVCR Framework
This commit is contained in:
Abel Hoogeveen 2019-02-01 00:55:49 +01:00
parent 5a184fbf8c
commit 0d867c7371
55 changed files with 4218 additions and 0 deletions

4
.gitattributes vendored Normal file
View File

@ -0,0 +1,4 @@
.gitattributes export-ignore
.gitignore export-ignore
.gitlab-ci.yml export-ignore
test/ export-ignore

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
composer.lock
composer.phar
.idea/
build/
test/temp/
vendor/

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
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.

29
composer.json Normal file
View File

@ -0,0 +1,29 @@
{
"name": "fuzeworks/mvcr",
"description": "FuzeWorks Framework MVC Component",
"license": ["MIT"],
"authors": [
{
"name": "TechFuze",
"homepage": "https://techfuze.net"
},
{
"name": "FuzeWorks Community",
"homepage": "https://techfuze.net/fuzeworks/contributors"
}
],
"require": {
"php": ">=7.1.0"
},
"require-dev": {
"fuzeworks/core": "dev-development",
"phpunit/phpunit": "^7",
"mikey179/vfsStream": "1.6.5"
},
"autoload": {
"psr-4": {
"FuzeWorks\\": "src/FuzeWorks/"
}
}
}

View File

@ -0,0 +1,41 @@
<?php
/**
* FuzeWorks Framework MVCR Component.
*
* 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
*/
/**
* @todo Add documentation
*/
return array(
);

View File

@ -0,0 +1,43 @@
<?php
/**
* FuzeWorks Framework MVCR Component.
*
* 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
*/
return array(
'default_view' => 'index',
'default_viewType' => 'standard',
'default_viewMethod' => 'index',
);

View File

@ -0,0 +1,49 @@
<?php
/**
* FuzeWorks Framework MVCR Component.
*
* 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 FuzeWorks;
/**
* Abstract class Controller.
*
* Extends all controllers to use the Factory.
*
* @author Abel Hoogeveen <abel@techfuze.net>
* @copyright Copyright (c) 2013 - 2019, TechFuze. (http://techfuze.net)
*/
abstract class Controller extends Factory
{
}

View File

@ -0,0 +1,172 @@
<?php
/**
* FuzeWorks Framework MVCR Component.
*
* 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 FuzeWorks;
use FuzeWorks\Event\ControllerGetEvent;
use FuzeWorks\Exception\ControllerException;
use FuzeWorks\Exception\EventException;
use FuzeWorks\Exception\NotFoundException;
/**
* Controllers Class.
*
* Simple loader class for MVC Controllers.
* Typically loads controllers from Application\Controller unless otherwise specified.
*
* @author Abel Hoogeveen <abel@techfuze.net>
* @copyright Copyright (c) 2013 - 2019, TechFuze. (http://techfuze.net)
*/
class Controllers
{
use ComponentPathsTrait;
/**
* Get a controller.
*
* Supply the name and the controller will be loaded from the supplied directory,
* or from one of the controllerPaths (which you can add).
*
* @param string $controllerName Name of the controller
* @param array $controllerPaths Alternative paths to use to load the controller
* @param string $namespace Alternative namespace for the controller. Defaults to \Application\Controller
* @param mixed %arguments,... Arguments to be provided to the constructor [...]
* @return Controller
* @throws ControllerException
* @throws NotFoundException
*/
public function get(string $controllerName, array $controllerPaths = [], string $namespace = '\Application\Controller\\'): Controller
{
if (empty($controllerName))
throw new ControllerException("Could not load controller. No name provided", 1);
// First get the directories where the controller can be located
$controllerPaths = (empty($controllerPaths) ? $this->componentPaths : [3 => $controllerPaths]);
// Get arguments for constructor
if (func_num_args() > 3)
$arguments = array_slice(func_get_args(), 3);
else
$arguments = [];
// Fire a controller load event
/** @var ControllerGetEvent $event */
try {
$event = Events::fireEvent('controllerGetEvent', $controllerName, $controllerPaths, $namespace, $arguments);
} catch (EventException $e) {
throw new ControllerException("Could not load controller. controllerGetEvent threw exception: '".$e->getMessage()."'");
}
// If the event is cancelled, stop loading
if ($event->isCancelled())
throw new ControllerException("Could not load controller. Controller cancelled by controllerGetEvent.");
// And attempt to load the controller
return $this->loadController($event->controllerName, $event->controllerPaths, $event->namespace, $event->arguments);
}
/**
* Load and return a controller.
*
* Supply the name and the controller will be loaded from one of the supplied directories
*
* @param string $controllerName Name of the controller
* @param array $controllerPaths
* @param string $namespace
* @param array $arguments
* @return Controller The Controller object
* @throws ControllerException
* @throws NotFoundException
*/
protected function loadController(string $controllerName, array $controllerPaths, string $namespace, array $arguments): Controller
{
// Now figure out the className and subdir
$class = trim($controllerName, '/');
if (($last_slash = strrpos($class, '/')) !== FALSE) {
// Extract the path
$subdir = substr($class, 0, ++$last_slash);
// Get the filename from the path
$class = substr($class, $last_slash);
} else {
$subdir = '';
}
// If the class already exists, return a new instance directly
$class = ucfirst($class);
$className = $namespace . $class . 'Controller';
if (class_exists($className, false))
{
$controller = new $className(...$arguments);
if (!$controller instanceof Controller)
throw new ControllerException("Could not load controller. Provided controllerName is not instance of \FuzeWorks\Controller");
return $controller;
}
// Search for the controller file
for ($i=Priority::getHighestPriority(); $i<=Priority::getLowestPriority(); $i++)
{
if (!isset($controllerPaths[$i]))
continue;
foreach ($controllerPaths[$i] as $directory) {
// Determine the file
$file = $directory . DS . $subdir . "controller." . strtolower($class) . '.php';
// If it doesn't, try and load the file
if (file_exists($file)) {
include_once($file);
// Test if provided class is instance of Controller
$controller = new $className(...$arguments);
if (!$controller instanceof Controller)
throw new ControllerException("Could not load controller. Provided controllerName is not instance of \FuzeWorks\Controller");
return $controller;
}
}
}
// Maybe it's in a subdirectory with the same name as the class
if ($subdir === '') {
return $this->loadController($class . "/" . $class, $controllerPaths, $namespace, $arguments);
}
throw new NotFoundException("Could not load controller. Controller was not found", 1);
}
}

View File

@ -0,0 +1,86 @@
<?php
/**
* FuzeWorks Framework MVCR Component.
*
* 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 FuzeWorks\Event;
use FuzeWorks\Event;
/**
* Event that gets fired when a controller is loaded.
*
* Use this to cancel the loading of a controller, or change the controller to be loaded
*
* @author Abel Hoogeveen <abel@techfuze.net>
* @copyright Copyright (c) 2013 - 2019, TechFuze. (http://techfuze.net)
*/
class ControllerGetEvent extends Event
{
/**
* The directories the controller can get loaded from.
*
* @var array
*/
public $controllerPaths = array();
/**
* The name of the controller to be loaded.
*
* @var string|null
*/
public $controllerName = null;
/**
* The namespace of the controller to be loaded. Defaults to Application\Controller
*
* @var string
*/
public $namespace = '\Application\Controller\\';
/**
* Arguments provided to the constructor
*
* @var array
*/
public $arguments = [];
public function init($controllerName, $controllerPaths, $namespace, $arguments)
{
$this->controllerName = $controllerName;
$this->controllerPaths = $controllerPaths;
$this->namespace = $namespace;
$this->arguments = $arguments;
}
}

View File

@ -0,0 +1,87 @@
<?php
/**
* FuzeWorks Framework MVCR Component.
*
* 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 FuzeWorks\Event;
use FuzeWorks\Event;
/**
* Event that gets loaded when a model is loaded.
*
* Use this to cancel the loading of a model, or change the model to be loaded
*
* @author Abel Hoogeveen <abel@techfuze.net>
* @copyright Copyright (c) 2013 - 2019, TechFuze. (http://techfuze.net)
*/
class ModelGetEvent extends Event
{
/**
* The directories the model can get loaded from.
*
* @var array
*/
public $modelPaths = array();
/**
* The name of the model to be loaded.
*
* @var string|null
*/
public $modelName = null;
/**
* The namespace of the model to be loaded. Defaults to Application\Model
*
* @var string
*/
public $namespace = '\Application\Model\\';
/**
* Arguments provided to the constructor
*
* @var array
*/
public $arguments = [];
public function init($modelName, $modelPaths, $namespace, $arguments)
{
$this->modelName = $modelName;
$this->modelPaths = $modelPaths;
$this->namespace = $namespace;
$this->arguments = $arguments;
}
}

View File

@ -0,0 +1,113 @@
<?php
/**
* FuzeWorks Framework MVCR Component.
*
* 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 FuzeWorks\Event;
use FuzeWorks\Controller;
use FuzeWorks\Event;
/**
* Event that gets fired when a view and controller are loaded.
*
* Use this to cancel the loading of a combination, or change the details of what is loaded.
*
* Currently only used by OldRouter::defaultCallable();
*
* @author Abel Hoogeveen <abel@techfuze.net>
* @copyright Copyright (c) 2013 - 2019, TechFuze. (http://techfuze.net)
*/
class RouterLoadViewAndControllerEvent extends Event
{
/**
* The name of the view
*
* @var string
*/
public $viewName;
/**
* The type of view to be loaded
*
* @var string
*/
public $viewType;
/**
* The function that will be loaded in the view
*
* @var string
*/
public $viewMethod;
/**
* The parameters that will be provided to the function in the view
*
* @var string
*/
public $viewParameters;
/**
* The route that resulted in this controller and view
*
* @var string
*/
public $route;
/**
* A controller to be injected.
*
* @var Controller|null
*/
public $controller;
public function init(string $viewName, string $viewType, string $viewMethod, string $viewParameters, string $route)
{
$this->viewName = $viewName;
$this->viewType = $viewType;
$this->viewMethod = $viewMethod;
$this->viewParameters = $viewParameters;
$this->route = $route;
}
/**
* Override the controller to be provided to the view.
*
* @param Controller $controller
*/
public function overrideController(Controller $controller)
{
$this->controller = $controller;
}
}

View File

@ -0,0 +1,101 @@
<?php
/**
* FuzeWorks Framework MVCR Component.
*
* 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 FuzeWorks\Event;
use FuzeWorks\Controller;
use FuzeWorks\Event;
/**
* Event that gets fired when a view is loaded.
*
* Use this to cancel the loading of a view, or change the view to be loaded
*
* @author Abel Hoogeveen <abel@techfuze.net>
* @copyright Copyright (c) 2013 - 2019, TechFuze. (http://techfuze.net)
*/
class ViewGetEvent extends Event
{
/**
* The directories the view can get loaded from.
*
* @var array
*/
public $viewPaths = [];
/**
* The name of the view to be loaded.
*
* @var string|null
*/
public $viewName = null;
/**
* The type of view to be loaded. Eg: html, json, cli.
*
* @var string|null
*/
public $viewType = null;
/**
* The namespace of the View to be loaded. Defaults to Application\View
*
* @var string
*/
public $namespace = '\Application\View\\';
/**
* Arguments provided to the constructor
*
* @var array
*/
public $arguments = [];
/**
* @var Controller
*/
public $controller;
public function init($viewName, $viewType, $viewPaths, $namespace, $controller, $arguments)
{
$this->viewName = $viewName;
$this->viewType = $viewType;
$this->viewPaths = $viewPaths;
$this->namespace = $namespace;
$this->controller = $controller;
$this->arguments = $arguments;
}
}

View File

@ -0,0 +1,41 @@
<?php
/**
* FuzeWorks Framework MVCR Component.
*
* 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 FuzeWorks\Exception;
class ControllerException extends Exception
{
}

View File

@ -0,0 +1,41 @@
<?php
/**
* FuzeWorks Framework MVCR Component.
*
* 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 FuzeWorks\Exception;
class HaltException extends Exception
{
}

View File

@ -0,0 +1,41 @@
<?php
/**
* FuzeWorks Framework MVCR Component.
*
* 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 FuzeWorks\Exception;
class ModelException extends Exception
{
}

View File

@ -0,0 +1,41 @@
<?php
/**
* FuzeWorks Framework MVCR Component.
*
* 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 FuzeWorks\Exception;
class NotFoundException extends Exception
{
}

View File

@ -0,0 +1,41 @@
<?php
/**
* FuzeWorks Framework MVCR Component.
*
* 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 FuzeWorks\Exception;
class RouterException extends Exception
{
}

View File

@ -0,0 +1,41 @@
<?php
/**
* FuzeWorks Framework MVCR Component.
*
* 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 FuzeWorks\Exception;
class ViewException extends Exception
{
}

View File

@ -0,0 +1,71 @@
<?php
/**
* FuzeWorks Framework MVCR Component.
*
* 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 FuzeWorks;
class MVCRComponent implements iComponent
{
public function getName(): string
{
return "MVCRComponent";
}
public function getClasses(): array
{
return [
'controllers' => '\FuzeWorks\Controllers',
'models' => '\FuzeWorks\Models',
'views' => '\FuzeWorks\Views',
'router' => '\FuzeWorks\Router'
];
}
public function onAddComponent(Configurator $configurator)
{
// Add fallback config directory
$configurator->addDirectory(
dirname(__DIR__) . DIRECTORY_SEPARATOR . 'Config',
'config',
Priority::LOWEST
);
}
public function onCreateContainer(Factory $container)
{
}
}

50
src/FuzeWorks/Model.php Normal file
View File

@ -0,0 +1,50 @@
<?php
/**
* FuzeWorks Framework MVCR Component.
*
* 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 FuzeWorks;
/**
* Abstract class Model.
*
* Extends all models to use the Factory.
*
* @author Abel Hoogeveen <abel@techfuze.net>
* @copyright Copyright (c) 2013 - 2019, TechFuze. (http://techfuze.net)
*/
abstract class Model extends Factory
{
}

171
src/FuzeWorks/Models.php Normal file
View File

@ -0,0 +1,171 @@
<?php
/**
* FuzeWorks Framework MVCR Component.
*
* 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 FuzeWorks;
use FuzeWorks\Event\ModelGetEvent;
use FuzeWorks\Exception\EventException;
use FuzeWorks\Exception\ModelException;
use FuzeWorks\Exception\NotFoundException;
/**
* Models Class.
*
* Simple loader class for MVC Models.
* Typically loads models from Application\Model unless otherwise specified.
*
* @author Abel Hoogeveen <abel@techfuze.net>
* @copyright Copyright (c) 2013 - 2019, TechFuze. (http://techfuze.net)
*/
class Models
{
use ComponentPathsTrait;
/**
* Get a model.
*
* Supply the name and the model will be loaded from the supplied directory,
* or from one of the modelPaths (which you can add).
*
* @param string $modelName Name of the model
* @param array $modelPaths Alternative paths to use to load the model
* @param string $namespace Alternative namespace for the model. Defaults to \Application\Model
* @param mixed %arguments,... Arguments to be provided to the constructor [...]
* @return Model
* @throws NotFoundException
* @throws ModelException
*/
public function get(string $modelName, array $modelPaths = [], string $namespace = '\Application\Model\\'): Model
{
if (empty($modelName))
throw new ModelException("Could not load model. No name provided", 1);
// First get the directories where the model can be located
$modelPaths = (empty($modelPaths) ? $this->componentPaths : [3 => $modelPaths]);
// Get arguments for constructor
if (func_num_args() > 3)
$arguments = array_slice(func_get_args(), 3);
else
$arguments = [];
// Fire a model load event
/** @var ModelGetEvent $event */
try {
$event = Events::fireEvent('modelGetEvent', $modelName, $modelPaths, $namespace, $arguments);
} catch (EventException $e) {
throw new ModelException("Could not load model. modelGetEvent threw exception: '".$e->getMessage()."'");
}
// If the event is cancelled, stop loading
if ($event->isCancelled())
throw new ModelException("Could not load model. Model cancelled by modelGetEvent.");
// And attempt to load the model
return $this->loadModel($event->modelName, $event->modelPaths, $event->namespace, $event->arguments);
}
/**
* Load and return a model.
*
* Supply the name and the model will be loaded from one of the supplied directories
*
* @param string $modelName Name of the model
* @param array $modelPaths
* @param string $namespace
* @param array $arguments
* @return Model The Model object
* @throws ModelException
* @throws NotFoundException
*/
protected function loadModel(string $modelName, array $modelPaths, string $namespace, array $arguments): Model
{
// Now figure out the className and subdir
$class = trim($modelName, '/');
if (($last_slash = strrpos($class, '/')) !== FALSE) {
// Extract the path
$subdir = substr($class, 0, ++$last_slash);
// Get the filename from the path
$class = substr($class, $last_slash);
} else {
$subdir = '';
}
// If the class already exists, return a new instance directly
$class = ucfirst($class);
$className = $namespace . $class . 'Model';
if (class_exists($className, false)) {
$model = new $className(...$arguments);
if (!$model instanceof Model)
throw new ModelException("Could not load model. Provided modelName is not instance of \FuzeWorks\Model");
return $model;
}
// Search for the model file
for ($i=Priority::getHighestPriority(); $i<=Priority::getLowestPriority(); $i++)
{
if (!isset($modelPaths[$i]))
continue;
foreach ($modelPaths[$i] as $directory) {
// Determine the file
$file = $directory . DS . $subdir . "model." . strtolower($class) . '.php';
// If it doesn't, try and load the file
if (file_exists($file)) {
include_once($file);
// Test if provided class is instance of Model
$model = new $className(...$arguments);
if (!$model instanceof Model)
throw new ModelException("Could not load model. Provided modelName is not instance of \FuzeWorks\Model");
return $model;
}
}
}
// Maybe it's in a subdirectory with the same name as the class
if ($subdir === '') {
return $this->loadModel($class . "/" . $class, $modelPaths, $namespace, $arguments);
}
throw new NotFoundException("Could not load model. Model was not found", 1);
}
}

400
src/FuzeWorks/Router.php Normal file
View File

@ -0,0 +1,400 @@
<?php
/**
* FuzeWorks Framework MVCR Component.
*
* The FuzeWorks PHP FrameWork
*
* Copyright (C) 2013-2018 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 - 2018, 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 FuzeWorks;
use FuzeWorks\Event\RouterLoadViewAndControllerEvent;
use FuzeWorks\Exception\ConfigException;
use FuzeWorks\Exception\ControllerException;
use FuzeWorks\Exception\EventException;
use FuzeWorks\Exception\HaltException;
use FuzeWorks\Exception\NotFoundException;
use FuzeWorks\Exception\RouterException;
use FuzeWorks\Exception\ViewException;
class Router
{
/**
* The routes loaded into the Router
*
* @var array
*/
protected $routes = [];
/**
* The current callable used
*
* @var callable|null
*/
protected $callable = null;
/**
* The current matches used
*
* @var array|null
*/
protected $matches = null;
/**
* The current route used
*
* @var string|null
*/
protected $route = null;
/**
* The current View
*
* @var View|null
*/
protected $view = null;
/**
* The current Controller
*
* @var Controller|null
*/
protected $controller = null;