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