1 | <?php |
---|
2 | /* |
---|
3 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
4 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
5 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
6 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
---|
7 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
8 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
---|
9 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
10 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
11 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
12 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
13 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
14 | * |
---|
15 | * This software consists of voluntary contributions made by many individuals |
---|
16 | * and is licensed under the LGPL. For more information, see |
---|
17 | * <http://www.doctrine-project.org>. |
---|
18 | */ |
---|
19 | |
---|
20 | namespace Doctrine\ORM\Proxy; |
---|
21 | |
---|
22 | /** |
---|
23 | * Special Autoloader for Proxy classes because them not being PSR-0 compatible. |
---|
24 | * |
---|
25 | * @author Benjamin Eberlei <kontakt@beberlei.de> |
---|
26 | */ |
---|
27 | class Autoloader |
---|
28 | { |
---|
29 | /** |
---|
30 | * Resolve proxy class name to a filename based on the following pattern. |
---|
31 | * |
---|
32 | * 1. Remove Proxy namespace from class name |
---|
33 | * 2. Remove namespace seperators from remaining class name. |
---|
34 | * 3. Return PHP filename from proxy-dir with the result from 2. |
---|
35 | * |
---|
36 | * @param string $proxyDir |
---|
37 | * @param string $proxyNamespace |
---|
38 | * @param string $className |
---|
39 | * @return string |
---|
40 | */ |
---|
41 | static public function resolveFile($proxyDir, $proxyNamespace, $className) |
---|
42 | { |
---|
43 | if (0 !== strpos($className, $proxyNamespace)) { |
---|
44 | throw ProxyException::notProxyClass($className, $proxyNamespace); |
---|
45 | } |
---|
46 | |
---|
47 | $className = str_replace('\\', '', substr($className, strlen($proxyNamespace) +1)); |
---|
48 | return $proxyDir . DIRECTORY_SEPARATOR . $className.'.php'; |
---|
49 | } |
---|
50 | |
---|
51 | /** |
---|
52 | * Register and return autoloader callback for the given proxy dir and |
---|
53 | * namespace. |
---|
54 | * |
---|
55 | * @param string $proxyDir |
---|
56 | * @param string $proxyNamespace |
---|
57 | * @param Closure $notFoundCallback Invoked when the proxy file is not found. |
---|
58 | * @return Closure |
---|
59 | */ |
---|
60 | static public function register($proxyDir, $proxyNamespace, \Closure $notFoundCallback = null) |
---|
61 | { |
---|
62 | $proxyNamespace = ltrim($proxyNamespace, "\\"); |
---|
63 | $autoloader = function($className) use ($proxyDir, $proxyNamespace, $notFoundCallback) { |
---|
64 | if (0 === strpos($className, $proxyNamespace)) { |
---|
65 | $file = Autoloader::resolveFile($proxyDir, $proxyNamespace, $className); |
---|
66 | |
---|
67 | if ($notFoundCallback && ! file_exists($file)) { |
---|
68 | $notFoundCallback($proxyDir, $proxyNamespace, $className); |
---|
69 | } |
---|
70 | |
---|
71 | require $file; |
---|
72 | } |
---|
73 | }; |
---|
74 | spl_autoload_register($autoloader); |
---|
75 | return $autoloader; |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|