~ubuntu-branches/ubuntu/wily/php-doctrine-common/wily-proposed

« back to all changes in this revision

Viewing changes to lib/Doctrine/Common/Proxy/Autoloader.php

  • Committer: Package Import Robot
  • Author(s): David Prévot
  • Date: 2014-06-15 11:26:00 UTC
  • mfrom: (2.1.1 experimental)
  • Revision ID: package-import@ubuntu.com-20140615112600-sg4mgpwq9sfg4mre
Tags: 2.4.2-2
* Upload to unstable
* No tests if DEB_BUILD_OPTIONS contains nocheck

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 MIT license. For more information, see
 
17
 * <http://www.doctrine-project.org>.
 
18
 */
 
19
 
 
20
namespace Doctrine\Common\Proxy;
 
21
 
 
22
use Doctrine\Common\Proxy\Exception\InvalidArgumentException;
 
23
 
 
24
/**
 
25
 * Special Autoloader for Proxy classes, which are not PSR-0 compliant.
 
26
 *
 
27
 * @author Benjamin Eberlei <kontakt@beberlei.de>
 
28
 */
 
29
class Autoloader
 
30
{
 
31
    /**
 
32
     * Resolves proxy class name to a filename based on the following pattern.
 
33
     *
 
34
     * 1. Remove Proxy namespace from class name.
 
35
     * 2. Remove namespace separators from remaining class name.
 
36
     * 3. Return PHP filename from proxy-dir with the result from 2.
 
37
     *
 
38
     * @param string $proxyDir
 
39
     * @param string $proxyNamespace
 
40
     * @param string $className
 
41
     *
 
42
     * @return string
 
43
     *
 
44
     * @throws InvalidArgumentException
 
45
     */
 
46
    public static function resolveFile($proxyDir, $proxyNamespace, $className)
 
47
    {
 
48
        if (0 !== strpos($className, $proxyNamespace)) {
 
49
            throw InvalidArgumentException::notProxyClass($className, $proxyNamespace);
 
50
        }
 
51
 
 
52
        $className = str_replace('\\', '', substr($className, strlen($proxyNamespace) + 1));
 
53
 
 
54
        return $proxyDir . DIRECTORY_SEPARATOR . $className . '.php';
 
55
    }
 
56
 
 
57
    /**
 
58
     * Registers and returns autoloader callback for the given proxy dir and namespace.
 
59
     *
 
60
     * @param string        $proxyDir
 
61
     * @param string        $proxyNamespace
 
62
     * @param callable|null $notFoundCallback Invoked when the proxy file is not found.
 
63
     *
 
64
     * @return \Closure
 
65
     *
 
66
     * @throws InvalidArgumentException
 
67
     */
 
68
    public static function register($proxyDir, $proxyNamespace, $notFoundCallback = null)
 
69
    {
 
70
        $proxyNamespace = ltrim($proxyNamespace, '\\');
 
71
 
 
72
        if ( ! (null === $notFoundCallback || is_callable($notFoundCallback))) {
 
73
            throw InvalidArgumentException::invalidClassNotFoundCallback($notFoundCallback);
 
74
        }
 
75
 
 
76
        $autoloader = function ($className) use ($proxyDir, $proxyNamespace, $notFoundCallback) {
 
77
            if (0 === strpos($className, $proxyNamespace)) {
 
78
                $file = Autoloader::resolveFile($proxyDir, $proxyNamespace, $className);
 
79
 
 
80
                if ($notFoundCallback && ! file_exists($file)) {
 
81
                    call_user_func($notFoundCallback, $proxyDir, $proxyNamespace, $className);
 
82
                }
 
83
 
 
84
                require $file;
 
85
            }
 
86
        };
 
87
 
 
88
        spl_autoload_register($autoloader);
 
89
 
 
90
        return $autoloader;
 
91
    }
 
92
}