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