Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 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 | |
| 26 | namespace Thrift\ClassLoader; |
| 27 | |
| 28 | class ThriftClassLoader |
| 29 | { |
| 30 | /** |
| 31 | * Namespaces path |
| 32 | * @var array |
| 33 | */ |
| 34 | protected $namespaces = array(); |
| 35 | |
| 36 | /** |
| 37 | * Thrift definition paths |
| 38 | * @var type |
| 39 | */ |
| 40 | protected $definitions = array(); |
| 41 | |
| 42 | /** |
| 43 | * Do we use APC cache ? |
| 44 | * @var boolean |
| 45 | */ |
| 46 | protected $apc = false; |
| 47 | |
| 48 | /** |
| 49 | * APC Cache prefix |
| 50 | * @var string |
| 51 | */ |
| 52 | protected $apc_prefix; |
| 53 | |
| 54 | /** |
| 55 | * Set autoloader to use APC cache |
| 56 | * @param boolean $apc |
| 57 | * @param string $apc_prefix |
| 58 | */ |
| 59 | public function __construct($apc = false, $apc_prefix = null) |
| 60 | { |
| 61 | $this->apc = $apc; |
| 62 | $this->apc_prefix = $apc_prefix; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Registers a namespace. |
| 67 | * |
| 68 | * @param string $namespace The namespace |
| 69 | * @param array|string $paths The location(s) of the namespace |
| 70 | */ |
| 71 | public function registerNamespace($namespace, $paths) |
| 72 | { |
| 73 | $this->namespaces[$namespace] = (array) $paths; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Registers a Thrift definition namespace. |
| 78 | * |
| 79 | * @param string $namespace The definition namespace |
| 80 | * @param array|string $paths The location(s) of the definition namespace |
| 81 | */ |
| 82 | public function registerDefinition($namespace, $paths) |
| 83 | { |
| 84 | $this->definitions[$namespace] = (array) $paths; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Registers this instance as an autoloader. |
| 89 | * |
| 90 | * @param Boolean $prepend Whether to prepend the autoloader or not |
| 91 | */ |
| 92 | public function register($prepend = false) |
| 93 | { |
| 94 | spl_autoload_register(array($this, 'loadClass'), true, $prepend); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Loads the given class, definition or interface. |
| 99 | * |
| 100 | * @param string $class The name of the class |
| 101 | */ |
| 102 | public function loadClass($class) |
| 103 | { |
| 104 | if ( |
| 105 | (true === $this->apc && ($file = $this->findFileInApc($class))) or |
| 106 | ($file = $this->findFile($class)) |
| 107 | ) |
| 108 | { |
| 109 | require_once $file; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Loads the given class or interface in APC. |
| 115 | * @param string $class The name of the class |
| 116 | * @return string |
| 117 | */ |
| 118 | protected function findFileInApc($class) |
| 119 | { |
| 120 | if (false === $file = apc_fetch($this->apc_prefix.$class)) { |
| 121 | apc_store($this->apc_prefix.$class, $file = $this->findFile($class)); |
| 122 | } |
| 123 | |
| 124 | return $file; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Find class in namespaces or definitions directories |
| 129 | * @param string $class |
| 130 | * @return string |
| 131 | */ |
| 132 | public function findFile($class) |
| 133 | { |
| 134 | // Remove first backslash |
| 135 | if ('\\' == $class[0]) |
| 136 | { |
| 137 | $class = substr($class, 1); |
| 138 | } |
| 139 | |
| 140 | if (false !== $pos = strrpos($class, '\\')) |
| 141 | { |
| 142 | // Namespaced class name |
| 143 | $namespace = substr($class, 0, $pos); |
| 144 | |
| 145 | // Iterate in normal namespaces |
| 146 | foreach ($this->namespaces as $ns => $dirs) |
| 147 | { |
| 148 | //Don't interfere with other autoloaders |
| 149 | if (0 !== strpos($namespace, $ns)) |
| 150 | { |
| 151 | continue; |
| 152 | } |
| 153 | |
| 154 | foreach ($dirs as $dir) |
| 155 | { |
| 156 | $className = substr($class, $pos + 1); |
| 157 | |
| 158 | $file = $dir.DIRECTORY_SEPARATOR. |
| 159 | str_replace('\\', DIRECTORY_SEPARATOR, $namespace). |
| 160 | DIRECTORY_SEPARATOR. |
| 161 | $className.'.php'; |
| 162 | |
| 163 | if (file_exists($file)) |
| 164 | { |
| 165 | return $file; |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | // Iterate in Thrift namespaces |
| 171 | |
| 172 | // Remove first part of namespace |
| 173 | $m = explode('\\', $class); |
| 174 | |
| 175 | // Ignore wrong call |
| 176 | if(count($m) <= 1) |
| 177 | { |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | $class = array_pop($m); |
| 182 | $namespace = implode('\\', $m); |
| 183 | |
| 184 | foreach ($this->definitions as $ns => $dirs) |
| 185 | { |
| 186 | //Don't interfere with other autoloaders |
| 187 | if (0 !== strpos($namespace, $ns)) |
| 188 | { |
| 189 | continue; |
| 190 | } |
| 191 | |
| 192 | foreach ($dirs as $dir) |
| 193 | { |
| 194 | /** |
| 195 | * Available in service: Interface, Client, Processor, Rest |
| 196 | * And every service methods (_.+) |
| 197 | */ |
| 198 | if( |
| 199 | 0 === preg_match('#(.+)(if|client|processor|rest)$#i', $class, $n) and |
| 200 | 0 === preg_match('#(.+)_[a-z0-9]+_(args|result)$#i', $class, $n) |
| 201 | ) |
| 202 | { |
| 203 | $className = 'Types'; |
| 204 | } |
| 205 | else |
| 206 | { |
| 207 | $className = $n[1]; |
| 208 | } |
| 209 | |
| 210 | $file = $dir.DIRECTORY_SEPARATOR . |
| 211 | str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . |
| 212 | DIRECTORY_SEPARATOR . |
| 213 | $className . '.php'; |
| 214 | |
| 215 | if (file_exists($file)) |
| 216 | { |
| 217 | return $file; |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | } |