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