Protocol.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. /**
  3. * Hoa
  4. *
  5. *
  6. * @license
  7. *
  8. * New BSD License
  9. *
  10. * Copyright © 2007-2017, Hoa community. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of the Hoa nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. namespace Hoa\Protocol;
  36. use Hoa\Consistency;
  37. /**
  38. * Class \Hoa\Protocol\Protocol.
  39. *
  40. * Root of the `hoa://` protocol.
  41. *
  42. * @copyright Copyright © 2007-2017 Hoa community
  43. * @license New BSD License
  44. */
  45. class Protocol extends Node
  46. {
  47. /**
  48. * No resolution value.
  49. *
  50. * @const string
  51. */
  52. const NO_RESOLUTION = '/hoa/flatland';
  53. /**
  54. * Singleton.
  55. *
  56. * @var \Hoa\Protocol\Protocol
  57. */
  58. private static $_instance = null;
  59. /**
  60. * Cache of resolver.
  61. *
  62. * @var array
  63. */
  64. private static $_cache = [];
  65. /**
  66. * Initialize the protocol.
  67. *
  68. */
  69. public function __construct()
  70. {
  71. $this->initialize();
  72. return;
  73. }
  74. /**
  75. * Singleton.
  76. * To use the `hoa://` protocol shared by everyone.
  77. *
  78. * @return \Hoa\Protocol\Protocol
  79. */
  80. public static function getInstance()
  81. {
  82. if (null === static::$_instance) {
  83. static::$_instance = new static();
  84. }
  85. return static::$_instance;
  86. }
  87. /**
  88. * Initialize the protocol.
  89. *
  90. * @return void
  91. */
  92. protected function initialize()
  93. {
  94. $root = dirname(dirname(__DIR__));
  95. $cwd =
  96. 'cli' === PHP_SAPI
  97. ? dirname(realpath($_SERVER['argv'][0]))
  98. : getcwd();
  99. $this[] = new Node(
  100. 'Application',
  101. $cwd . DS,
  102. [
  103. new Node('Public', 'Public' . DS)
  104. ]
  105. );
  106. $this[] = new Node(
  107. 'Data',
  108. dirname($cwd) . DS,
  109. [
  110. new Node(
  111. 'Etc',
  112. 'Etc' . DS,
  113. [
  114. new Node('Configuration', 'Configuration' . DS),
  115. new Node('Locale', 'Locale' . DS)
  116. ]
  117. ),
  118. new Node('Lost+found', 'Lost+found' . DS),
  119. new Node('Temporary', 'Temporary' . DS),
  120. new Node(
  121. 'Variable',
  122. 'Variable' . DS,
  123. [
  124. new Node('Cache', 'Cache' . DS),
  125. new Node('Database', 'Database' . DS),
  126. new Node('Log', 'Log' . DS),
  127. new Node('Private', 'Private' . DS),
  128. new Node('Run', 'Run' . DS),
  129. new Node('Test', 'Test' . DS)
  130. ]
  131. )
  132. ]
  133. );
  134. $this[] = new Node\Library(
  135. 'Library',
  136. $root . DS . 'Hoathis' . DS . RS .
  137. $root . DS . 'Hoa' . DS
  138. );
  139. return;
  140. }
  141. /**
  142. * Resolve (unfold) an `hoa://` path to its real resource.
  143. *
  144. * @param string $path Path to resolve.
  145. * @param bool $exists If `true`, try to find the first that exists,
  146. * else return the first solution.
  147. * @param bool $unfold Return all solutions instead of one.
  148. * @return mixed
  149. */
  150. public function resolve($path, $exists = true, $unfold = false)
  151. {
  152. if (substr($path, 0, 6) !== 'hoa://') {
  153. if (true === is_dir($path)) {
  154. $path = rtrim($path, '/\\');
  155. if (0 === strlen($path)) {
  156. $path = '/';
  157. }
  158. }
  159. return $path;
  160. }
  161. if (isset(self::$_cache[$path])) {
  162. $handle = self::$_cache[$path];
  163. } else {
  164. $out = $this->_resolve($path, $handle);
  165. // Not a path but a resource.
  166. if (!is_array($handle)) {
  167. return $out;
  168. }
  169. $handle = array_values(array_unique($handle, SORT_REGULAR));
  170. foreach ($handle as &$entry) {
  171. if (true === is_dir($entry)) {
  172. $entry = rtrim($entry, '/\\');
  173. if (0 === strlen($entry)) {
  174. $entry = '/';
  175. }
  176. }
  177. }
  178. self::$_cache[$path] = $handle;
  179. }
  180. if (true === $unfold) {
  181. if (true !== $exists) {
  182. return $handle;
  183. }
  184. $out = [];
  185. foreach ($handle as $solution) {
  186. if (file_exists($solution)) {
  187. $out[] = $solution;
  188. }
  189. }
  190. return $out;
  191. }
  192. if (true !== $exists) {
  193. return $handle[0];
  194. }
  195. foreach ($handle as $solution) {
  196. if (file_exists($solution)) {
  197. return $solution;
  198. }
  199. }
  200. return static::NO_RESOLUTION;
  201. }
  202. /**
  203. * Clear the cache.
  204. *
  205. * @return void
  206. */
  207. public static function clearCache()
  208. {
  209. self::$_cache = [];
  210. return;
  211. }
  212. }
  213. /**
  214. * Flex entity.
  215. */
  216. Consistency::flexEntity('Hoa\Protocol\Protocol');