ThriftClassLoader.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. *
  20. * ClassLoader to load Thrift library and definitions
  21. * Inspired from UniversalClassLoader from Symfony 2
  22. *
  23. * @package thrift.classloader
  24. */
  25. namespace Thrift\ClassLoader;
  26. class ThriftClassLoader
  27. {
  28. /**
  29. * Namespaces path
  30. * @var array
  31. */
  32. protected $namespaces = array();
  33. /**
  34. * Thrift definition paths
  35. * @var type
  36. */
  37. protected $definitions = array();
  38. /**
  39. * Do we use APC cache ?
  40. * @var boolean
  41. */
  42. protected $apc = false;
  43. /**
  44. * APC Cache prefix
  45. * @var string
  46. */
  47. protected $apc_prefix;
  48. /**
  49. * Set autoloader to use APC cache
  50. * @param boolean $apc
  51. * @param string $apc_prefix
  52. */
  53. public function __construct($apc = false, $apc_prefix = null)
  54. {
  55. $this->apc = $apc;
  56. $this->apc_prefix = $apc_prefix;
  57. }
  58. /**
  59. * Registers a namespace.
  60. *
  61. * @param string $namespace The namespace
  62. * @param array|string $paths The location(s) of the namespace
  63. */
  64. public function registerNamespace($namespace, $paths)
  65. {
  66. $this->namespaces[$namespace] = (array) $paths;
  67. }
  68. /**
  69. * Registers a Thrift definition namespace.
  70. *
  71. * @param string $namespace The definition namespace
  72. * @param array|string $paths The location(s) of the definition namespace
  73. */
  74. public function registerDefinition($namespace, $paths)
  75. {
  76. $this->definitions[$namespace] = (array) $paths;
  77. }
  78. /**
  79. * Registers this instance as an autoloader.
  80. *
  81. * @param Boolean $prepend Whether to prepend the autoloader or not
  82. */
  83. public function register($prepend = false)
  84. {
  85. spl_autoload_register(array($this, 'loadClass'), true, $prepend);
  86. }
  87. /**
  88. * Loads the given class, definition or interface.
  89. *
  90. * @param string $class The name of the class
  91. */
  92. public function loadClass($class)
  93. {
  94. if (
  95. (true === $this->apc && ($file = $this->findFileInApc($class))) or
  96. ($file = $this->findFile($class))
  97. )
  98. {
  99. require_once $file;
  100. }
  101. }
  102. /**
  103. * Loads the given class or interface in APC.
  104. * @param string $class The name of the class
  105. * @return string
  106. */
  107. protected function findFileInApc($class)
  108. {
  109. if (false === $file = apc_fetch($this->apc_prefix.$class)) {
  110. apc_store($this->apc_prefix.$class, $file = $this->findFile($class));
  111. }
  112. return $file;
  113. }
  114. /**
  115. * Find class in namespaces or definitions directories
  116. * @param string $class
  117. * @return string
  118. */
  119. public function findFile($class)
  120. {
  121. // Remove first backslash
  122. if ('\\' == $class[0]) {
  123. $class = substr($class, 1);
  124. }
  125. if (false !== $pos = strrpos($class, '\\')) {
  126. // Namespaced class name
  127. $namespace = substr($class, 0, $pos);
  128. // Iterate in normal namespaces
  129. foreach ($this->namespaces as $ns => $dirs) {
  130. //Don't interfere with other autoloaders
  131. if (0 !== strpos($namespace, $ns)) {
  132. continue;
  133. }
  134. foreach ($dirs as $dir) {
  135. $className = substr($class, $pos + 1);
  136. $file = $dir.DIRECTORY_SEPARATOR.
  137. str_replace('\\', DIRECTORY_SEPARATOR, $namespace).
  138. DIRECTORY_SEPARATOR.
  139. $className.'.php';
  140. if (file_exists($file)) {
  141. return $file;
  142. }
  143. }
  144. }
  145. // Iterate in Thrift namespaces
  146. // Remove first part of namespace
  147. $m = explode('\\', $class);
  148. // Ignore wrong call
  149. if (count($m) <= 1) {
  150. return;
  151. }
  152. $class = array_pop($m);
  153. $namespace = implode('\\', $m);
  154. foreach ($this->definitions as $ns => $dirs) {
  155. //Don't interfere with other autoloaders
  156. if (0 !== strpos($namespace, $ns)) {
  157. continue;
  158. }
  159. foreach ($dirs as $dir) {
  160. /**
  161. * Available in service: Interface, Client, Processor, Rest
  162. * And every service methods (_.+)
  163. */
  164. if(
  165. 0 === preg_match('#(.+)(if|client|processor|rest)$#i', $class, $n) and
  166. 0 === preg_match('#(.+)_[a-z0-9]+_(args|result)$#i', $class, $n)
  167. )
  168. {
  169. $className = 'Types';
  170. } else {
  171. $className = $n[1];
  172. }
  173. $file = $dir.DIRECTORY_SEPARATOR .
  174. str_replace('\\', DIRECTORY_SEPARATOR, $namespace) .
  175. DIRECTORY_SEPARATOR .
  176. $className . '.php';
  177. if (file_exists($file)) {
  178. return $file;
  179. }
  180. }
  181. }
  182. }
  183. }
  184. }