]> git.mxchange.org Git - hub.git/blob - application/hub/main/tags/package/class_PackageTags.php
A whole bunch of classes/interfaces/exceptions added, many refacturings:
[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          * Protected constructor
35          *
36          * @return      void
37          */
38         protected function __construct () {
39                 // Call parent constructor
40                 parent::__construct(__CLASS__);
41
42                 // Init the object registry
43                 $this->initObjectRegistry();
44         }
45
46         /**
47          * Creates an instance of this class
48          *
49          * @return      $tagsInstance   An instance of a Tagable class
50          */
51         public final static function createPackageTags () {
52                 // Get new instance
53                 $tagsInstance = new PackageTags();
54
55                 // Return the prepared instance
56                 return $tagsInstance;
57         }
58
59         /**
60          * Loads the XML file (our "object registry") and saves an instance for faster re-use
61          *
62          * @return      void
63          */
64         private function initObjectRegistry () {
65                 // Output debug message
66                 $this->debugOutput('TAGS: Initializing object registry - START');
67
68                 // Get the application instance
69                 $appInstance = Registry::getRegistry()->getInstance('app');
70
71                 // Get a XML template instance
72                 $templateInstance = ObjectFactory::createObjectByConfiguredName('object_registry_template_class', array($appInstance));
73
74                 // Disable language support
75                 $templateInstance->enableLanguageSupport(false);
76
77                 /*
78                  * Enable compacting/rewriting of the  XML to save bandwidth from XML
79                  * comments. This is expensive and should be avoided in general.
80                  */
81                 $templateInstance->enableXmlCompacting();
82
83                 // Set it for later use
84                 $this->setTemplateInstance($templateInstance);
85
86                 // Read the XML file
87                 $this->getTemplateInstance()->loadObjectRegistryTemplate('object_registry');
88
89                 // Render the XML content
90                 $this->getTemplateInstance()->renderXmlContent();
91
92                 // Output debug message
93                 $this->debugOutput('TAGS: Initializing object registry - FINISHED');
94         }
95
96         /**
97          * Extracts the tags from given package data
98          *
99          * @param       $packageData    Raw package data
100          * @return      void
101          */
102         private function extractTagsFromPackageData (array $packageData) {
103                 /*
104                  * We take a look at the tags (in most cases only one is needed) so
105                  * first we need the content data splitted up into all it's parts.
106                  */
107                 $contentData = explode(NetworkPackage::PACKAGE_MASK_SEPERATOR, $packageData['content']);
108
109                 // Get the tags and store them locally
110                 $this->setTags(explode(NetworkPackage::PACKAGE_TAGS_SEPERATOR, $contentData[NetworkPackage::INDEX_TAGS]));
111         }
112
113         /**
114          * Verifies all tags by looking them up in an XML file. This method is
115          * the key method to make sure only known objects are being distributed and
116          * shared over the whole hub-network. So if the "tag" (let's better say
117          * object type) isn't found in that XML the package won't be distributed.
118          *
119          * @return      void
120          * @throws      InvalidTagException             If a provided tag from the package data is invalid
121          */
122         private function verifyAllTags () {
123                 // Get the registry
124                 $objectRegistryInstance = ObjectFactory::createObjectByConfiguredName('object_type_registry_class');
125
126                 // "Walk" over all tags
127                 foreach ($this->getTags() as $tag) {
128                         // Get an array from this tag
129                         $entry = $objectRegistryInstance->getArrayFromKey($tag);
130
131                         // If the array is empty, the entry is invalid!
132                         if (count($entry) == 0) {
133                                 // Invalid entry found
134                                 throw new InvalidTagException(array($this, $tag), self::EXCEPTION_INVALID_TAG);
135                         } // END - if
136
137                         // Now save the last discovered protocol
138                         $this->lastProtocol = $entry['object-protocol'];
139                 } // END - foreach
140         }
141
142         /**
143          * Chooses the right protocol from given package data
144          *
145          * @param       $packageData    Raw package data
146          * @return      $protocolName   Name of the choosen procotol
147          */
148         public function chooseProtocolFromPackageData (array $packageData) {
149                 // Extract the tags
150                 $this->extractTagsFromPackageData($packageData);
151
152                 // Now we need to verify every single tag
153                 $this->verifyAllTags();
154
155                 // Use the last found protocol for transmission
156                 $protocolName = $this->lastProtocol;
157
158                 // Return it
159                 return $protocolName;
160         }
161 }
162
163 // [EOF]
164 ?>