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