Completed
Push — master ( 3011b4...e02e3f )
by Filip
10:21
created

ProxyAutoloader   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 8
c 3
b 1
f 0
lcom 2
cbo 2
dl 0
loc 95
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A create() 0 4 1
A register() 0 9 2
A unregister() 0 5 1
A tryLoad() 0 9 3
1
<?php
2
3
/**
4
 * This file is part of the Kdyby (http://www.kdyby.org)
5
 *
6
 * Copyright (c) 2008 Filip Procházka ([email protected])
7
 *
8
 * For the full copyright and license information, please view the file license.txt that was distributed with this source code.
9
 */
10
11
namespace Kdyby\Doctrine\Proxy;
12
13
use Kdyby;
14
use Nette;
15
16
17
18
/**
19
 * @author Filip Procházka <[email protected]>
20
 */
21
class ProxyAutoloader extends Nette\Object
22
{
23
24
	/**
25
	 * @var string
26
	 */
27
	private $dir;
28
29
	/**
30
	 * @var string
31
	 */
32
	private $namespace;
33
34
	/**
35
	 * @var array list of registered loaders
36
	 */
37
	static private $loaders = [];
38
39
40
41
	/**
42
	 * @param string $proxyDir
43
	 * @param string $proxyNamespace
44
	 */
45
	public function __construct($proxyDir, $proxyNamespace)
46
	{
47
		$this->dir = $proxyDir;
48
		$this->namespace = ltrim($proxyNamespace, "\\");
49
	}
50
51
52
53
	/**
54
	 * @param string $proxyDir
55
	 * @param string $proxyNamespace
56
	 * @return ProxyAutoloader
57
	 */
58
	public static function create($proxyDir, $proxyNamespace)
59
	{
60
		return new static($proxyDir, $proxyNamespace);
61
	}
62
63
64
65
	/**
66
	 * Register autoloader.
67
	 * @param bool $prepend prepend autoloader?
68
	 * @throws \Nette\NotSupportedException
69
	 * @return void
70
	 */
71
	public function register($prepend = FALSE)
72
	{
73
		if (!function_exists('spl_autoload_register')) {
74
			throw new Nette\NotSupportedException('spl_autoload does not exist in this PHP installation.');
75
		}
76
77
		spl_autoload_register([$this, 'tryLoad'], TRUE, (bool) $prepend);
78
		self::$loaders[spl_object_hash($this)] = $this;
79
	}
80
81
82
83
	/**
84
	 * Unregister autoloader.
85
	 * @return bool
86
	 */
87
	public function unregister()
88
	{
89
		unset(self::$loaders[spl_object_hash($this)]);
90
		return spl_autoload_unregister([$this, 'tryLoad']);
91
	}
92
93
94
95
	/**
96
	 * Resolve proxy class name to a filename based on the following pattern.
97
	 *
98
	 * 1. Remove Proxy namespace from class name
99
	 * 2. Remove namespace seperators from remaining class name.
100
	 * 3. Return PHP filename from proxy-dir with the result from 2.
101
	 *
102
	 * @param  string
103
	 * @return void
104
	 */
105
	public function tryLoad($type)
106
	{
107
		if (strpos($type, $this->namespace) === 0) {
108
			$type = str_replace('\\', '', substr($type, strlen($this->namespace) + 1));
109
			if (file_exists($file = $this->dir . DIRECTORY_SEPARATOR . $type . '.php')) {
110
				include $file;
111
			}
112
		}
113
	}
114
115
}
116