/libraries/koowa/service/identifier/identifier.php

https://bitbucket.org/nlabyt/bcf-ball-4eb2 · PHP · 349 lines · 147 code · 44 blank · 158 comment · 29 complexity · bd1b2dbc89403e69f02eae1418c3ac0c MD5 · raw file

  1. <?php
  2. /**
  3. * @version $Id: identifier.php 4266 2011-10-08 23:57:41Z johanjanssens $
  4. * @category Koowa
  5. * @package Koowa_Service
  6. * @copyright Copyright (C) 2007 - 2010 Johan Janssens. All rights reserved.
  7. * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
  8. */
  9. /**
  10. * Service Identifier
  11. *
  12. * Wraps identifiers of the form [application::]type.package.[.path].name
  13. * in an object, providing public accessors and methods for derived formats.
  14. *
  15. * @author Johan Janssens <johan@nooku.org>
  16. * @category Koowa
  17. * @package Koowa_Factory
  18. * @subpackage Identifier
  19. */
  20. class KServiceIdentifier implements KServiceIdentifierInterface
  21. {
  22. /**
  23. * An associative array of application paths
  24. *
  25. * @var array
  26. */
  27. protected static $_applications = array();
  28. /**
  29. * Associative array of identifier adapters
  30. *
  31. * @var array
  32. */
  33. protected static $_locators = array();
  34. /**
  35. * The identifier
  36. *
  37. * @var string
  38. */
  39. protected $_identifier = '';
  40. /**
  41. * The application name
  42. *
  43. * @var string
  44. */
  45. protected $_application = '';
  46. /**
  47. * The identifier type [com|plg|mod]
  48. *
  49. * @var string
  50. */
  51. protected $_type = '';
  52. /**
  53. * The identifier package
  54. *
  55. * @var string
  56. */
  57. protected $_package = '';
  58. /**
  59. * The identifier path
  60. *
  61. * @var array
  62. */
  63. protected $_path = array();
  64. /**
  65. * The identifier object name
  66. *
  67. * @var string
  68. */
  69. protected $_name = '';
  70. /**
  71. * The file path
  72. *
  73. * @var string
  74. */
  75. protected $_filepath = '';
  76. /**
  77. * The classname
  78. *
  79. * @var string
  80. */
  81. protected $_classname = '';
  82. /**
  83. * The base path
  84. *
  85. * @var string
  86. */
  87. protected $_basepath = '';
  88. /**
  89. * Constructor
  90. *
  91. * @param string Identifier string or object in [application::]type.package.[.path].name format
  92. * @throws KServiceIdentifierException if the identfier is not valid
  93. */
  94. public function __construct($identifier)
  95. {
  96. //Check if the identifier is valid
  97. if(strpos($identifier, ':') === FALSE) {
  98. throw new KServiceIdentifierException('Malformed identifier : '.$identifier);
  99. }
  100. //Get the parts
  101. if(false === $parts = parse_url($identifier)) {
  102. throw new KServiceIdentifierException('Malformed identifier : '.$identifier);
  103. }
  104. // Set the type
  105. $this->type = $parts['scheme'];
  106. //Set the application
  107. if(isset($parts['host'])) {
  108. $this->application = $parts['host'];
  109. }
  110. // Set the path
  111. $this->_path = trim($parts['path'], '/');
  112. $this->_path = explode('.', $this->_path);
  113. // Set the extension (first part)
  114. $this->_package = array_shift($this->_path);
  115. // Set the name (last part)
  116. if(count($this->_path)) {
  117. $this->_name = array_pop($this->_path);
  118. }
  119. //Cache the identifier to increase performance
  120. $this->_identifier = $identifier;
  121. }
  122. /**
  123. * Serialize the identifier
  124. *
  125. * @return string The serialised identifier
  126. */
  127. public function serialize()
  128. {
  129. $data = array(
  130. 'application' => $this->_application,
  131. 'type' => $this->_type,
  132. 'package' => $this->_package,
  133. 'path' => $this->_path,
  134. 'name' => $this->_name,
  135. 'identifier' => $this->_identifier,
  136. 'basepath' => $this->_basepath,
  137. 'filepath' => $this->filepath,
  138. 'classname' => $this->classname,
  139. );
  140. return serialize($data);
  141. }
  142. /**
  143. * Unserialize the identifier
  144. *
  145. * @return string The serialised identifier
  146. */
  147. public function unserialize($data)
  148. {
  149. $data = unserialize($data);
  150. foreach($data as $property => $value) {
  151. $this->{'_'.$property} = $value;
  152. }
  153. }
  154. /**
  155. * Set an application path
  156. *
  157. * @param string The name of the application
  158. * @param string The path of the application
  159. * @return void
  160. */
  161. public static function setApplication($application, $path)
  162. {
  163. self::$_applications[$application] = $path;
  164. }
  165. /**
  166. * Get an application path
  167. *
  168. * @param string The name of the application
  169. * @return string The path of the application
  170. */
  171. public static function getApplication($application)
  172. {
  173. return isset(self::$_applications[$application]) ? self::$_applications[$application] : null;
  174. }
  175. /**
  176. * Get a list of applications
  177. *
  178. * @return array
  179. */
  180. public static function getApplications()
  181. {
  182. return self::$_applications;
  183. }
  184. /**
  185. * Add a identifier adapter
  186. *
  187. * @param object A KServiceLocator
  188. * @return void
  189. */
  190. public static function addLocator(KServiceLocatorInterface $locator)
  191. {
  192. self::$_locators[$locator->getType()] = $locator;
  193. }
  194. /**
  195. * Get the registered adapters
  196. *
  197. * @return array
  198. */
  199. public static function getLocators()
  200. {
  201. return self::$_locators;
  202. }
  203. /**
  204. * Implements the virtual class properties
  205. *
  206. * This functions creates a string representation of the identifier.
  207. *
  208. * @param string The virtual property to set.
  209. * @param string Set the virtual property to this value.
  210. */
  211. public function __set($property, $value)
  212. {
  213. if(isset($this->{'_'.$property}))
  214. {
  215. //Force the path to an array
  216. if($property == 'path')
  217. {
  218. if(is_scalar($value)) {
  219. $value = (array) $value;
  220. }
  221. }
  222. //Set the basepath
  223. if($property == 'application')
  224. {
  225. if(!isset(self::$_applications[$value])) {
  226. throw new KServiceIdentifierException('Unknow application : '.$value);
  227. }
  228. $this->_basepath = self::$_applications[$value];
  229. }
  230. //Set the type
  231. if($property == 'type')
  232. {
  233. //Check the type
  234. if(!isset(self::$_locators[$value])) {
  235. throw new KServiceIdentifierException('Unknow type : '.$value);
  236. }
  237. }
  238. //Set the properties
  239. $this->{'_'.$property} = $value;
  240. //Unset the properties
  241. $this->_identifier = '';
  242. $this->_classname = '';
  243. $this->_filepath = '';
  244. }
  245. }
  246. /**
  247. * Implements access to virtual properties by reference so that it appears to be
  248. * a public property.
  249. *
  250. * @param string The virtual property to return.
  251. * @return array The value of the virtual property.
  252. */
  253. public function &__get($property)
  254. {
  255. if(isset($this->{'_'.$property}))
  256. {
  257. if($property == 'filepath' && empty($this->_filepath)) {
  258. $this->_filepath = self::$_locators[$this->_type]->findPath($this);
  259. }
  260. if($property == 'classname' && empty($this->_classname)) {
  261. $this->_classname = self::$_locators[$this->_type]->findClass($this);
  262. }
  263. return $this->{'_'.$property};
  264. }
  265. }
  266. /**
  267. * This function checks if a virtual property is set.
  268. *
  269. * @param string The virtual property to return.
  270. * @return boolean True if it exists otherwise false.
  271. */
  272. public function __isset($property)
  273. {
  274. return isset($this->{'_'.$property});
  275. }
  276. /**
  277. * Formats the indentifier as a [application::]type.package.[.path].name string
  278. *
  279. * @return string
  280. */
  281. public function __toString()
  282. {
  283. if($this->_identifier == '')
  284. {
  285. if(!empty($this->_type)) {
  286. $this->_identifier .= $this->_type;
  287. }
  288. if(!empty($this->_application)) {
  289. $this->_identifier .= '://'.$this->_application.'/';
  290. } else {
  291. $this->_identifier .= ':';
  292. }
  293. if(!empty($this->_package)) {
  294. $this->_identifier .= $this->_package;
  295. }
  296. if(count($this->_path)) {
  297. $this->_identifier .= '.'.implode('.',$this->_path);
  298. }
  299. if(!empty($this->_name)) {
  300. $this->_identifier .= '.'.$this->_name;
  301. }
  302. }
  303. return $this->_identifier;
  304. }
  305. }