1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20:
21: class NPresenterLoader implements IPresenterLoader
22: {
23:
24: public $caseSensitive = FALSE;
25:
26:
27: private $baseDir;
28:
29:
30: private $cache = array();
31:
32:
33:
34: 35: 36:
37: public function __construct($baseDir)
38: {
39: $this->baseDir = $baseDir;
40: }
41:
42:
43:
44: 45: 46: 47: 48:
49: public function getPresenterClass(& $name)
50: {
51: if (isset($this->cache[$name])) {
52: list($class, $name) = $this->cache[$name];
53: return $class;
54: }
55:
56: if (!is_string($name) || !preg_match("#^[a-zA-Z\x7f-\xff][a-zA-Z0-9\x7f-\xff:]*$#", $name)) {
57: throw new NInvalidPresenterException("Presenter name must be alphanumeric string, '$name' is invalid.");
58: }
59:
60: $class = $this->formatPresenterClass($name);
61:
62: if (!class_exists($class)) {
63:
64: $file = $this->formatPresenterFile($name);
65: if (is_file($file) && is_readable($file)) {
66: NLimitedScope::load($file, TRUE);
67: }
68:
69: if (!class_exists($class)) {
70: throw new NInvalidPresenterException("Cannot load presenter '$name', class '$class' was not found in '$file'.");
71: }
72: }
73:
74: $reflection = new NClassReflection($class);
75: $class = $reflection->getName();
76:
77: if (!$reflection->implementsInterface('IPresenter')) {
78: throw new NInvalidPresenterException("Cannot load presenter '$name', class '$class' is not IPresenter implementor.");
79: }
80:
81: if ($reflection->isAbstract()) {
82: throw new NInvalidPresenterException("Cannot load presenter '$name', class '$class' is abstract.");
83: }
84:
85:
86: $realName = $this->unformatPresenterClass($class);
87: if ($name !== $realName) {
88: if ($this->caseSensitive) {
89: throw new NInvalidPresenterException("Cannot load presenter '$name', case mismatch. Real name is '$realName'.");
90: } else {
91: $this->cache[$name] = array($class, $realName);
92: $name = $realName;
93: }
94: } else {
95: $this->cache[$name] = array($class, $realName);
96: }
97:
98: return $class;
99: }
100:
101:
102:
103: 104: 105: 106: 107:
108: public function formatPresenterClass($presenter)
109: {
110: return strtr($presenter, ':', '_') . 'Presenter';
111: return str_replace(':', 'Module\\', $presenter) . 'Presenter';
112: }
113:
114:
115:
116: 117: 118: 119: 120:
121: public function unformatPresenterClass($class)
122: {
123: return strtr(substr($class, 0, -9), '_', ':');
124: return str_replace('Module\\', ':', substr($class, 0, -9));
125: }
126:
127:
128:
129: 130: 131: 132: 133:
134: public function formatPresenterFile($presenter)
135: {
136: $path = '/' . str_replace(':', 'Module/', $presenter);
137: return $this->baseDir . substr_replace($path, '/presenters', strrpos($path, '/'), 0) . 'Presenter.php';
138: }
139:
140: }
141: