]> git.mxchange.org Git - core.git/blob - inc/main/classes/resolver/command/class_BaseCommandResolver.php
Continued:
[core.git] / inc / main / classes / resolver / command / class_BaseCommandResolver.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Resolver\Controller;
4
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Request\Requestable;
8
9 /**
10  * A generic command resolver class
11  *
12  * @author              Roland Haeder <webmaster@shipsimu.org>
13  * @version             0.0.0
14  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
15  * @license             GNU GPL 3.0 or any newer version
16  * @link                http://www.shipsimu.org
17  *
18  * This program is free software: you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation, either version 3 of the License, or
21  * (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program. If not, see <http://www.gnu.org/licenses/>.
30  */
31 class BaseCommandResolver extends BaseResolver {
32         /**
33          * Protected constructor
34          *
35          * @param       $className      Name of the class
36          * @return      void
37          */
38         protected function __construct ($className) {
39                 // Call parent constructor
40                 parent::__construct($className);
41         }
42
43         /**
44          * "Loads" a given command and instances it if not yet cached
45          *
46          * @param       $commandName                            A command name we shall look for
47          * @return      $commandInstance                        A loaded command instance
48          * @throws      InvalidCommandException         Thrown if even the default
49          *                                                                              command class is missing (bad!)
50          */
51         protected function loadCommand ($commandName) {
52                 // Init command instance
53                 $commandInstance = NULL;
54
55                 // Create class name
56                 $className = $this->getCapitalizedClassPrefix() . self::convertToClassName($commandName) . 'Command';
57
58                 // Create command class name
59                 $this->setClassName($className);
60
61                 // Is this class loaded?
62                 if (!class_exists($this->getClassName())) {
63                         // Class not found, so throw an exception
64                         throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
65                 } // END - if
66
67                 // Initiate the command
68                 $commandInstance = ObjectFactory::createObjectByName($this->getClassName(), array($this));
69
70                 // Return the result
71                 return $commandInstance;
72         }
73
74         /**
75          * Returns an command instance for a given request class or null if
76          * it was not found
77          *
78          * @param       $requestInstance        An instance of a Requestable class
79          * @return      $commandInstance        An instance of the resolved command
80          * @throws      InvalidCommandException                         Thrown if $commandName is
81          *                                                                                              invalid
82          * @throws      InvalidCommandInstanceException         Thrown if $commandInstance
83          *                                                                                              is an invalid instance
84          */
85         public function resolveCommandByRequest (Requestable $requestInstance) {
86                 // Init variables
87                 $commandName = '';
88                 $commandInstance = NULL;
89
90                 // This goes fine so let's resolve the command
91                 $commandName = $requestInstance->getRequestElement('command');
92
93                 // Is the command empty? Then fall back to default command
94                 if (empty($commandName)) {
95                         $commandName = $this->getConfigInstance()->getConfigEntry('default_' . self::getResponseTypeFromSystem() . '_command');
96                 } // END - if
97
98                 // Check if command is valid
99                 if ($this->isCommandValid($commandName) === FALSE) {
100                         // This command is invalid!
101                         throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
102                 } // END - if
103
104                 // Get the command
105                 $commandInstance = $this->loadCommand($commandName);
106
107                 // And validate it
108                 if ((!is_object($commandInstance)) || (!$commandInstance instanceof Commandable)) {
109                         // This command has an invalid instance!
110                         throw new InvalidCommandInstanceException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
111                 } // END - if
112
113                 // Set last command
114                 $this->setResolvedInstance($commandInstance);
115
116                 // Return the resolved command instance
117                 return $commandInstance;
118         }
119
120         /**
121          * Resolves the command by its direct name and returns an instance of its class
122          *
123          * @param       $commandName            The direct command name we shall resolve
124          * @return      $commandInstance        An instance of the command class
125          * @throws      InvalidCommandException         Thrown if $commandName is invalid
126          */
127         public function resolveCommand ($commandName) {
128                 // Initiate the instance variable
129                 $commandInstance = NULL;
130
131                 // Is the command empty? Then fall back to default command
132                 if (empty($commandName)) {
133                         $commandName = $this->getConfigInstance()->getConfigEntry('default_' . self::getResponseTypeFromSystem() . '_command');
134                 } // END - if
135
136                 // Check if command is valid
137                 if ($this->isCommandValid($commandName) === FALSE) {
138                         // This command is invalid!
139                         throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
140                 } // END - if
141
142                 // Get the command
143                 $commandInstance = $this->loadCommand($commandName);
144
145                 // Return the instance
146                 return $commandInstance;
147         }
148
149         /**
150          * Checks whether the given command is valid
151          *
152          * @param       $commandName    The default command we shall execute
153          * @return      $isValid                Whether the given command is valid
154          * @throws      EmptyVariableException  Thrown if given command is not set
155          */
156         public function isCommandValid ($commandName) {
157                 // By default nothing shall be valid
158                 $isValid = FALSE;
159
160                 // Is a command set?
161                 if (empty($commandName)) {
162                         // Then thrown an exception here
163                         throw new EmptyVariableException(array($this, 'commandName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
164                 } // END - if
165
166                 // Create the full class name
167                 $className = $this->getCapitalizedClassPrefix() . self::convertToClassName($commandName) . 'Command';
168
169                 // Now, let us create the full name of the command class
170                 $this->setClassName($className);
171
172                 // Is this class already loaded?
173                 if (class_exists($this->getClassName())) {
174                         // This class does exist. :-)
175                         $isValid = TRUE;
176                 } // END - if
177
178                 // Set command name
179                 $this->setCommandName($commandName);
180
181                 // Return the result
182                 return $isValid;
183         }
184
185 }