added controller resolver (unfinished maybe)
[core.git] / application / tests / classes / resolver / controller / class_TestsConsoleControllerResolver.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Resolver\Controller;
4
5 /**
6  * A resolver for resolving controllers locally
7  *
8  * @author              Roland Haeder <webmaster@shipsimu.org>
9  * @version             0.0.0
10  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
11  * @license             GNU GPL 3.0 or any newer version
12  * @link                http://www.shipsimu.org
13  *
14  * This program is free software: you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation, either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program. If not, see <http://www.gnu.org/licenses/>.
26  */
27 class TestsConsoleControllerResolver extends BaseControllerResolver implements ControllerResolver {
28         /**
29          * Protected constructor
30          *
31          * @return      void
32          */
33         protected function __construct () {
34                 // Call parent constructor
35                 parent::__construct(__CLASS__);
36
37                 // Set prefix to "TestsConsole"
38                 $this->setClassPrefix('tests_console');
39         }
40
41         /**
42          * Creates an instance of a resolver class with a given command
43          *
44          * @param       $controllerName                         The controller we shall resolve
45          * @param       $applicationInstance            An instance of a manageable application helper class
46          * @return      $resolverInstance                       The prepared controller resolver instance
47          * @throws      EmptyVariableException          Thrown if default command is not set
48          * @throws      InvalidControllerException      Thrown if default controller is invalid
49          */
50         public static final function createTestsConsoleControllerResolver ($controllerName, ManageableApplication $applicationInstance) {
51                 // Create the new instance
52                 $resolverInstance = new TestsConsoleControllerResolver();
53
54                 // Is the variable $controllerName set and the command is valid?
55                 if (empty($controllerName)) {
56                         // Then thrown an exception here
57                         throw new EmptyVariableException(array($resolverInstance, 'commandName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
58                 } elseif ($resolverInstance->isControllerValid($controllerName) === FALSE) {
59                         // Invalid command found
60                         throw new InvalidControllerException(array($resolverInstance, $controllerName), self::EXCEPTION_INVALID_CONTROLLER);
61                 }
62
63                 // Set the application instance
64                 $resolverInstance->setApplicationInstance($applicationInstance);
65
66                 // Set command name
67                 $resolverInstance->setControllerName($controllerName);
68
69                 // Return the prepared instance
70                 return $resolverInstance;
71         }
72
73         /**
74          * Resolves the default controller of the given command
75          *
76          * @return      $controllerInstance             A controller instance for the default
77          *                                                                      command
78          * @throws      InvalidControllerInstanceException      Thrown if $controllerInstance
79          *                                                                                              is invalid
80          */
81         public function resolveController () {
82                 // Init variables
83                 $controllerName = '';
84                 $controllerInstance = NULL;
85
86                 // Get the command name 
87                 $controllerName = $this->getControllerName();
88
89                 // Get the command
90                 $controllerInstance = $this->loadController($controllerName);
91
92                 // And validate it
93                 if ((!is_object($controllerInstance)) || (!$controllerInstance instanceof Controller)) {
94                         // This command has an invalid instance!
95                         throw new InvalidControllerInstanceException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER);
96                 } // END - if
97
98                 // Set last controller
99                 $this->setResolvedInstance($controllerInstance);
100
101                 // Return the maybe resolved instance
102                 return $controllerInstance;
103         }
104 }
105
106 // [EOF]
107 ?>