]> git.mxchange.org Git - hub.git/blob - application/hub/main/tags/package/class_PackageTags.php
'public static final' is the right thing, some variables renamed to make clear what...
[hub.git] / application / hub / main / tags / package / class_PackageTags.php
1 <?php
2 /**
3  * A Package tags class
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 Hub Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24 class PackageTags extends BaseTags implements Tagable {
25         // Exception codes
26         const EXCEPTION_INVALID_TAG = 0x160;
27
28         /**
29          * Last found protocol
30          */
31         private $lastProtocol = 'invalid';
32
33         /**
34          * Last found recipient type
35          */
36         private $lastRecipientType = 'invalid';
37
38         /**
39          * Protected constructor
40          *
41          * @return      void
42          */
43         protected function __construct () {
44                 // Call parent constructor
45                 parent::__construct(__CLASS__);
46
47                 // Init the object registry
48                 $this->initObjectRegistry();
49         }
50
51         /**
52          * Creates an instance of this class
53          *
54          * @return      $tagsInstance   An instance of a Tagable class
55          */
56         public static final function createPackageTags () {
57                 // Get new instance
58                 $tagsInstance = new PackageTags();
59
60                 // Return the prepared instance
61                 return $tagsInstance;
62         }
63
64         /**
65          * Loads the XML file (our "object registry") and saves an instance for faster re-use
66          *
67          * @return      void
68          */
69         private function initObjectRegistry () {
70                 // Output debug message
71                 $this->debugOutput('TAGS: Initializing object registry - START');
72
73                 // Get the application instance
74                 $appInstance = Registry::getRegistry()->getInstance('app');
75
76                 // Get a XML template instance
77                 $templateInstance = ObjectFactory::createObjectByConfiguredName('object_registry_template_class', array($appInstance));
78
79                 // Disable language support
80                 $templateInstance->enableLanguageSupport(false);
81
82                 /*
83                  * Enable compacting/rewriting of the  XML to save bandwidth from XML
84                  * comments. This is expensive and should be avoided in general.
85                  */
86                 $templateInstance->enableXmlCompacting();
87
88                 // Set it for later use
89                 $this->setTemplateInstance($templateInstance);
90
91                 // Read the XML file
92                 $this->getTemplateInstance()->loadObjectRegistryTemplate('object_registry');
93
94                 // Render the XML content
95                 $this->getTemplateInstance()->renderXmlContent();
96
97                 // Output debug message
98                 $this->debugOutput('TAGS: Initializing object registry - FINISHED');
99         }
100
101         /**
102          * Extracts the tags from given package data
103          *
104          * @param       $packageData    Raw package data
105          * @return      void
106          */
107         private function extractTagsFromPackageData (array $packageData) {
108                 /*
109                  * We take a look at the tags (in most cases only one is needed) so
110                  * first we need the content data splitted up into all it's parts.
111                  */
112                 $contentData = explode(NetworkPackage::PACKAGE_MASK_SEPERATOR, $packageData['content']);
113
114                 // Get the tags and store them locally
115                 $this->setTags(explode(NetworkPackage::PACKAGE_TAGS_SEPERATOR, $contentData[NetworkPackage::INDEX_TAGS]));
116         }
117
118         /**
119          * Verifies all tags by looking them up in an XML file. This method is
120          * the key method to make sure only known objects are being distributed and
121          * shared over the whole hub-network. So if the "tag" (let's better say
122          * object type) isn't found in that XML the package won't be distributed.
123          *
124          * @return      void
125          * @throws      InvalidTagException             If a provided tag from the package data is invalid
126          */
127         private function verifyAllTags () {
128                 // Get the registry
129                 $objectRegistryInstance = ObjectFactory::createObjectByConfiguredName('object_type_registry_class');
130
131                 // "Walk" over all tags
132                 foreach ($this->getTags() as $tag) {
133                         // Debug output
134                         $this->debugOutput('TAGS: Validating tag ' . $tag . ' ...');
135
136                         // Get an array from this tag
137                         $entry = $objectRegistryInstance->getArrayFromKey($tag);
138
139                         // If the array is empty, the entry is invalid!
140                         if (count($entry) == 0) {
141                                 // Invalid entry found
142                                 throw new InvalidTagException(array($this, $tag), self::EXCEPTION_INVALID_TAG);
143                         } // END - if
144
145                         // Now save the last discovered protocol/recipient type
146                         $this->lastProtocol      = $entry['object-protocol'];
147                         $this->lastRecipientType = $entry['object-recipient-type'];
148                 } // END - foreach
149         }
150
151         /**
152          * Chooses the right protocol from given package data
153          *
154          * @param       $packageData    Raw package data
155          * @return      $protocolName   Name of the choosen procotol
156          */
157         public function chooseProtocolFromPackageData (array $packageData) {
158                 // Extract the tags
159                 $this->extractTagsFromPackageData($packageData);
160
161                 // Now we need to verify every single tag
162                 $this->verifyAllTags();
163
164                 // Use the last found protocol for transmission
165                 $protocolName = $this->lastProtocol;
166
167                 // Return it
168                 return $protocolName;
169         }
170
171         /**
172          * Checks wether the given package data is accepted by the listener
173          *
174          * @param       $packageData            Raw package data
175          * @param       $listenerInstance       A Listenable instance
176          * @return      $accepts                        Wether it is accepted
177          */
178         public function ifPackageDataIsAcceptedByListener (array $packageData, Listenable $listenerInstance) {
179                 // Extract the tags
180                 $this->extractTagsFromPackageData($packageData);
181
182                 // Now we need to verify every single tag
183                 $this->verifyAllTags();
184
185                 // Now simply check it out
186                 $accepts = (($this->lastRecipientType == $listenerInstance->getListenerType()) && ($listenerInstance->getListenerType() != 'invalid'));
187
188                 // And return the result
189                 return $accepts;
190         }
191 }
192
193 // [EOF]
194 ?>