renamed lib-local.php -> lib-lfdb.php because it really loads the "legendary"
[core.git] / inc / main / classes / resolver / action / class_BaseActionResolver.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Resolver\Action;
4
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Generic\EmptyVariableException;
8 use CoreFramework\Resolver\BaseResolver;
9
10 /**
11  * A generic action 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 BaseActionResolver extends BaseResolver {
33         /**
34          * Validated action name
35          */
36         private $actionName = '';
37
38         /**
39          * Protected constructor
40          *
41          * @param       $className      Name of the class
42          * @return      void
43          */
44         protected function __construct ($className) {
45                 // Call parent constructor
46                 parent::__construct($className);
47         }
48
49         /**
50          * Setter for action name
51          *
52          * @param       $actionName     Last validated action name
53          * @return      void
54          */
55         protected final function setActionName ($actionName) {
56                 $this->actionName = (string) $actionName;
57         }
58
59         /**
60          * Getter for action name
61          *
62          * @return      $actionName     Last validated action name
63          */
64         public final function getActionName () {
65                 return $this->actionName;
66         }
67
68         /**
69          * Checks whether the given action is valid
70          *
71          * @param       $namespace              Namespace to look in
72          * @param       $actionName             The default action we shall execute
73          * @return      $isValid                Whether the given action is valid
74          * @throws      EmptyVariableException  Thrown if given action is not set
75          */
76         public function isActionValid ($namespace, $actionName) {
77                 // By default nothing shall be valid
78                 $isValid = FALSE;
79
80                 // Is a action set?
81                 if (empty($namespace)) {
82                         // Then thrown an exception here
83                         throw new EmptyVariableException(array($this, 'namespace'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
84                 } elseif (empty($actionName)) {
85                         // Then thrown an exception here
86                         throw new EmptyVariableException(array($this, 'actionName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
87                 } // END - if
88
89                 // Create class name
90                 $className = sprintf(
91                         '%s\%s%sAction',
92                         $namespace,
93                         $this->getCapitalizedClassPrefix(),
94                         self::convertToClassName($actionName)
95                 );
96
97                 // Now, let us create the full name of the action class
98                 $this->setClassName($className);
99
100                 // Is this class already loaded?
101                 if (class_exists($this->getClassName())) {
102                         // This class does exist. :-)
103                         $isValid = TRUE;
104                 } // END - if
105
106                 // Set action name
107                 $this->setActionName($actionName);
108
109                 // Return the result
110                 return $isValid;
111         }
112
113         /**
114          * "Loads" current action and instances it if not yet cached
115          *
116          * @return      $actionInstance                 A loaded action instance
117          */
118         protected function loadAction () {
119                 // Init action instance
120                 $actionInstance = NULL;
121
122                 // Create class name
123                 $className = sprintf(
124                         '%s\%s%sAction',
125                         $this->getNamespace(),
126                         $this->getCapitalizedClassPrefix(),
127                         self::convertToClassName($actionName)
128                 );
129
130                 // ... and set it
131                 $this->setClassName($className);
132
133                 // Initiate the action
134                 $actionInstance = ObjectFactory::createObjectByName($this->getClassName(), array($this));
135
136                 // Return the result
137                 return $actionInstance;
138         }
139
140 }