blob: 67575ce18fa78bdbf37097e242ca78d9b9e4f024 [file] [log] [blame]
Roger Meier21c0a852012-09-05 19:47:14 +00001<?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
Roger Thomas6fb59232014-11-04 10:09:23 +000021 * Inspired from UniversalClassLoader from Symfony 2
Roger Meier21c0a852012-09-05 19:47:14 +000022 *
23 * @package thrift.classloader
24 */
25
26namespace Thrift\ClassLoader;
27
28class 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
Roger Thomas6fb59232014-11-04 10:09:23 +000057 * @param string $apc_prefix
Roger Meier21c0a852012-09-05 19:47:14 +000058 */
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.
Roger Thomas6fb59232014-11-04 10:09:23 +0000115 * @param string $class The name of the class
Roger Meier21c0a852012-09-05 19:47:14 +0000116 * @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
Roger Thomas6fb59232014-11-04 10:09:23 +0000129 * @param string $class
Roger Meier21c0a852012-09-05 19:47:14 +0000130 * @return string
131 */
132 public function findFile($class)
133 {
134 // Remove first backslash
Roger Thomas6fb59232014-11-04 10:09:23 +0000135 if ('\\' == $class[0]) {
Roger Meier21c0a852012-09-05 19:47:14 +0000136 $class = substr($class, 1);
137 }
138
Roger Thomas6fb59232014-11-04 10:09:23 +0000139 if (false !== $pos = strrpos($class, '\\')) {
Roger Meier21c0a852012-09-05 19:47:14 +0000140 // Namespaced class name
141 $namespace = substr($class, 0, $pos);
142
143 // Iterate in normal namespaces
Roger Thomas6fb59232014-11-04 10:09:23 +0000144 foreach ($this->namespaces as $ns => $dirs) {
Roger Meier21c0a852012-09-05 19:47:14 +0000145 //Don't interfere with other autoloaders
Roger Thomas6fb59232014-11-04 10:09:23 +0000146 if (0 !== strpos($namespace, $ns)) {
Roger Meier21c0a852012-09-05 19:47:14 +0000147 continue;
148 }
149
Roger Thomas6fb59232014-11-04 10:09:23 +0000150 foreach ($dirs as $dir) {
Roger Meier21c0a852012-09-05 19:47:14 +0000151 $className = substr($class, $pos + 1);
152
153 $file = $dir.DIRECTORY_SEPARATOR.
154 str_replace('\\', DIRECTORY_SEPARATOR, $namespace).
155 DIRECTORY_SEPARATOR.
156 $className.'.php';
157
Roger Thomas6fb59232014-11-04 10:09:23 +0000158 if (file_exists($file)) {
Roger Meier21c0a852012-09-05 19:47:14 +0000159 return $file;
160 }
161 }
162 }
163
164 // Iterate in Thrift namespaces
165
166 // Remove first part of namespace
167 $m = explode('\\', $class);
168
169 // Ignore wrong call
Roger Thomas6fb59232014-11-04 10:09:23 +0000170 if (count($m) <= 1) {
Roger Meier21c0a852012-09-05 19:47:14 +0000171 return;
172 }
173
174 $class = array_pop($m);
175 $namespace = implode('\\', $m);
176
Roger Thomas6fb59232014-11-04 10:09:23 +0000177 foreach ($this->definitions as $ns => $dirs) {
Roger Meier21c0a852012-09-05 19:47:14 +0000178 //Don't interfere with other autoloaders
Roger Thomas6fb59232014-11-04 10:09:23 +0000179 if (0 !== strpos($namespace, $ns)) {
Roger Meier21c0a852012-09-05 19:47:14 +0000180 continue;
181 }
182
Roger Thomas6fb59232014-11-04 10:09:23 +0000183 foreach ($dirs as $dir) {
Roger Meier21c0a852012-09-05 19:47:14 +0000184 /**
185 * Available in service: Interface, Client, Processor, Rest
186 * And every service methods (_.+)
187 */
188 if(
189 0 === preg_match('#(.+)(if|client|processor|rest)$#i', $class, $n) and
190 0 === preg_match('#(.+)_[a-z0-9]+_(args|result)$#i', $class, $n)
191 )
192 {
193 $className = 'Types';
Roger Thomas6fb59232014-11-04 10:09:23 +0000194 } else {
Roger Meier21c0a852012-09-05 19:47:14 +0000195 $className = $n[1];
196 }
197
198 $file = $dir.DIRECTORY_SEPARATOR .
199 str_replace('\\', DIRECTORY_SEPARATOR, $namespace) .
200 DIRECTORY_SEPARATOR .
201 $className . '.php';
202
Roger Thomas6fb59232014-11-04 10:09:23 +0000203 if (file_exists($file)) {
Roger Meier21c0a852012-09-05 19:47:14 +0000204 return $file;
205 }
206 }
207 }
208 }
209 }
210}