PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Trinity/Web/Area/Strategy/File.php

http://github.com/zyxist/Trinity
PHP | 253 lines | 139 code | 20 blank | 94 comment | 18 complexity | b08295a0c6f0bfc3bab5032cb8988d79 MD5 | raw file
  1. <?php
  2. /*
  3. * TRINITY FRAMEWORK <http://www.invenzzia.org>
  4. *
  5. * This file is subject to the new BSD license that is bundled
  6. * with this package in the file LICENSE. It is also available through
  7. * WWW at this URL: <http://www.invenzzia.org/license/new-bsd>
  8. *
  9. * Copyright (c) Invenzzia Group <http://www.invenzzia.org>
  10. * and other contributors. See website for details.
  11. */
  12. namespace Trinity\Web\Area\Strategy;
  13. use \Trinity\Basement\Application as BaseApplication;
  14. use \Trinity\Basement\Module as Module;
  15. use \Trinity\Web\Area\Strategy;
  16. use \Trinity\Web\Area\Exception as Area_Exception;
  17. /**
  18. * The area discovery strategy that stores area definitions in files.
  19. *
  20. * @author Tomasz Jędrzejewski
  21. * @copyright Invenzzia Group <http://www.invenzzia.org/> and contributors.
  22. * @license http://www.invenzzia.org/license/new-bsd New BSD License
  23. */
  24. class File implements Strategy
  25. {
  26. /**
  27. * Discover the area name from host.
  28. */
  29. const DISCOVERY_HOST = 0;
  30. /**
  31. * Discover the area from query path
  32. */
  33. const DISCOVERY_QUERY_PATH = 1;
  34. /**
  35. * The discovery type.
  36. * @var int
  37. */
  38. private $_discoveryType;
  39. /**
  40. * The discovery data
  41. * @var string
  42. */
  43. private $_discoveryData;
  44. /**
  45. * The file name.
  46. * @var string
  47. */
  48. private $_fileName;
  49. /**
  50. * The name of the default area.
  51. * @var string
  52. */
  53. private $_defaultArea = null;
  54. /**
  55. * Name of the discoveried area.
  56. */
  57. private $_discoveriedArea = null;
  58. /**
  59. * The list of available areas.
  60. * @var array
  61. */
  62. private $_areas;
  63. /**
  64. * Constructs the strategy object.
  65. *
  66. * @throws Area_Exception
  67. * @param string $fileName Area definition file
  68. */
  69. public function __construct($fileName)
  70. {
  71. if(!file_exists($fileName))
  72. {
  73. throw new Area_Exception('Cannot load area definitions from '.$fileName.': file not accessible.');
  74. }
  75. $this->_fileName = $fileName;
  76. } // end __construct();
  77. /**
  78. * Sets the name of the default area.
  79. *
  80. * @param string $defaultArea Default area name
  81. * @return Strategy_File Fluent interface.
  82. */
  83. public function setDefaultArea($defaultArea)
  84. {
  85. $this->_defaultArea = (string)$defaultArea;
  86. return $this;
  87. } // end setDefaultArea();
  88. /**
  89. * Sets the requested discovery type.
  90. *
  91. * @param int $type Discovery type
  92. * @param Opc_Visit $visit Visit object used to grab the discovery type data.
  93. * @return Strategy_File Fluent interface.
  94. */
  95. public function setDiscoveryType($type, \Opc_Visit $visit)
  96. {
  97. $this->_discoveryType = (int)$type;
  98. switch($type)
  99. {
  100. case 0:
  101. $this->_discoveryData = $visit->currentHost;
  102. break;
  103. case 1:
  104. $this->_discoveryData = $visit->currentParams;
  105. break;
  106. default:
  107. throw new Area_Exception('Unknown discovery type: '.$type.'.');
  108. }
  109. return $this;
  110. } // end setDiscoveryType();
  111. /**
  112. * Returns the discovery type.
  113. *
  114. * @return int
  115. */
  116. public function getDiscoveryType()
  117. {
  118. return $this->_discoveryType;
  119. } // end getDiscoveryType();
  120. /**
  121. * Returns the information about the specified area.
  122. *
  123. * @throws Area_Exception
  124. * @param string $name Area name
  125. * @return array
  126. */
  127. public function getAreaOptions($name)
  128. {
  129. if($this->_areas === null)
  130. {
  131. $this->_loadAreas();
  132. }
  133. if(!isset($this->_areas[$name]))
  134. {
  135. throw new Area_Exception('Unknown area: '.$name.'.');
  136. }
  137. return $this->_areas[$name];
  138. } // end getAreaOptions();
  139. /**
  140. * Discoveries the area from a file.
  141. *
  142. * @return array
  143. */
  144. public function discoverArea()
  145. {
  146. if($this->_areas === null)
  147. {
  148. $this->_loadAreas();
  149. }
  150. if($this->_discoveriedArea !== null)
  151. {
  152. return array($name, $this->_areas[$this->_discoveriedArea]);
  153. }
  154. if($this->_discoveryType == self::DISCOVERY_HOST)
  155. {
  156. return $this->_discoveryHost();
  157. }
  158. else
  159. {
  160. return $this->_discoveryQueryPath();
  161. }
  162. } // end discoverArea();
  163. /**
  164. * Discoveries the area from host.
  165. *
  166. * @return array
  167. */
  168. private function _discoveryHost()
  169. {
  170. foreach($this->_areas as $name => $area)
  171. {
  172. if(!isset($area['host']))
  173. {
  174. throw new Area_Exception('Missing \'host\' attribute in '.$name.' area.');
  175. }
  176. if(preg_match($area['host'], $this->_discoveryData))
  177. {
  178. $this->_discoveriedArea = $name;
  179. return array($name, $area);
  180. }
  181. if($name == $this->_defaultArea)
  182. {
  183. $store = array($name, $area);
  184. }
  185. }
  186. // Throw an exception or return the default area.
  187. if(!isset($store))
  188. {
  189. throw new Area_Exception('No area matches the host '.$this->_discoveryData);
  190. }
  191. $this->_discoveriedArea = $name;
  192. return $store;
  193. } // end _discoveryHost();
  194. /**
  195. * Discoveries the area from query path.
  196. *
  197. * @return array
  198. */
  199. private function _discoveryQueryPath()
  200. {
  201. foreach($this->_areas as $name => $area)
  202. {
  203. if(!isset($area['path']))
  204. {
  205. throw new Area_Exception('Missing \'path\' attribute in '.$name.' area.');
  206. }
  207. if(stripos($this->_discoveryData, '/'.$area['path']) === 0)
  208. {
  209. $this->_discoveriedArea = $name;
  210. return array($name, $area);
  211. }
  212. if($name == $this->_defaultArea)
  213. {
  214. $store = array($name, $area);
  215. }
  216. }
  217. // Throw an exception or return the default area.
  218. if(!isset($store))
  219. {
  220. throw new Area_Exception('No area matches the query path '.$this->_discoveryData);
  221. }
  222. $this->_discoveriedArea = $name;
  223. return $store;
  224. } // end _discoveryHost();
  225. private function _loadAreas()
  226. {
  227. $this->_areas = parse_ini_file($this->_fileName, true);
  228. if(!is_array($this->_areas))
  229. {
  230. throw new Area_Exception('Error while loading the area definition file.');
  231. }
  232. } // end _loadAreas();
  233. } // end File;