~ubuntu-branches/debian/experimental/php-nette/experimental

« back to all changes in this revision

Viewing changes to Nette-2.0.13/Nette/Reflection/ClassType.php

  • Committer: Package Import Robot
  • Author(s): David Prévot
  • Date: 2013-11-30 08:47:54 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20131130084754-4udf1xsu9085tnfc
Tags: 2.1.0~rc-1
* New upstream branch
* Update copyright

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
 
3
 
/**
4
 
 * This file is part of the Nette Framework (http://nette.org)
5
 
 *
6
 
 * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
7
 
 *
8
 
 * For the full copyright and license information, please view
9
 
 * the file license.txt that was distributed with this source code.
10
 
 */
11
 
 
12
 
namespace Nette\Reflection;
13
 
 
14
 
use Nette,
15
 
        Nette\ObjectMixin;
16
 
 
17
 
 
18
 
/**
19
 
 * Reports information about a class.
20
 
 *
21
 
 * @author     David Grudl
22
 
 * @property-read Method $constructor
23
 
 * @property-read Extension $extension
24
 
 * @property-read ClassType[] $interfaces
25
 
 * @property-read Method[] $methods
26
 
 * @property-read ClassType $parentClass
27
 
 * @property-read Property[] $properties
28
 
 * @property-read IAnnotation[][] $annotations
29
 
 * @property-read string $description
30
 
 * @property-read string $name
31
 
 * @property-read bool $internal
32
 
 * @property-read bool $userDefined
33
 
 * @property-read bool $instantiable
34
 
 * @property-read string $fileName
35
 
 * @property-read int $startLine
36
 
 * @property-read int $endLine
37
 
 * @property-read string $docComment
38
 
 * @property-read mixed[] $constants
39
 
 * @property-read string[] $interfaceNames
40
 
 * @property-read bool $interface
41
 
 * @property-read bool $abstract
42
 
 * @property-read bool $final
43
 
 * @property-read int $modifiers
44
 
 * @property-read array $staticProperties
45
 
 * @property-read array $defaultProperties
46
 
 * @property-read bool $iterateable
47
 
 * @property-read string $extensionName
48
 
 * @property-read string $namespaceName
49
 
 * @property-read string $shortName
50
 
 */
51
 
class ClassType extends \ReflectionClass
52
 
{
53
 
 
54
 
        /** @var array (method => array(type => callable)) */
55
 
        private static $extMethods;
56
 
 
57
 
 
58
 
        /**
59
 
         * @param  string|object
60
 
         * @return ClassType
61
 
         */
62
 
        public static function from($class)
63
 
        {
64
 
                return new static($class);
65
 
        }
66
 
 
67
 
 
68
 
        public function __toString()
69
 
        {
70
 
                return 'Class ' . $this->getName();
71
 
        }
72
 
 
73
 
 
74
 
        /**
75
 
         * @return bool
76
 
         */
77
 
        public function hasEventProperty($name)
78
 
        {
79
 
                if (preg_match('#^on[A-Z]#', $name) && $this->hasProperty($name)) {
80
 
                        $rp = $this->getProperty($name);
81
 
                        return $rp->isPublic() && !$rp->isStatic();
82
 
                }
83
 
                return FALSE;
84
 
        }
85
 
 
86
 
 
87
 
        /**
88
 
         * Adds a method to class.
89
 
         * @param  string  method name
90
 
         * @param  mixed   callable
91
 
         * @return self
92
 
         */
93
 
        public function setExtensionMethod($name, $callback)
94
 
        {
95
 
                $l = & self::$extMethods[strtolower($name)];
96
 
                $l[strtolower($this->getName())] = new Nette\Callback($callback);
97
 
                $l[''] = NULL;
98
 
                return $this;
99
 
        }
100
 
 
101
 
 
102
 
        /**
103
 
         * Returns extension method.
104
 
         * @param  string  method name
105
 
         * @return mixed
106
 
         */
107
 
        public function getExtensionMethod($name)
108
 
        {
109
 
                $class = strtolower($this->getName());
110
 
                $l = & self::$extMethods[strtolower($name)];
111
 
 
112
 
                if (empty($l)) {
113
 
                        return FALSE;
114
 
 
115
 
                } elseif (isset($l[''][$class])) { // cached value
116
 
                        return $l[''][$class];
117
 
                }
118
 
 
119
 
                $cl = $class;
120
 
                do {
121
 
                        if (isset($l[$cl])) {
122
 
                                return $l[''][$class] = $l[$cl];
123
 
                        }
124
 
                } while (($cl = strtolower(get_parent_class($cl))) !== '');
125
 
 
126
 
                foreach (class_implements($class) as $cl) {
127
 
                        $cl = strtolower($cl);
128
 
                        if (isset($l[$cl])) {
129
 
                                return $l[''][$class] = $l[$cl];
130
 
                        }
131
 
                }
132
 
                return $l[''][$class] = FALSE;
133
 
        }
134
 
 
135
 
 
136
 
        /**
137
 
         * @param  string
138
 
         * @return bool
139
 
         */
140
 
        public function is($type)
141
 
        {
142
 
                return $this->isSubclassOf($type) || strcasecmp($this->getName(), ltrim($type, '\\')) === 0;
143
 
        }
144
 
 
145
 
 
146
 
        /********************* Reflection layer ****************d*g**/
147
 
 
148
 
 
149
 
        /**
150
 
         * @return Method|NULL
151
 
         */
152
 
        public function getConstructor()
153
 
        {
154
 
                return ($ref = parent::getConstructor()) ? Method::from($this->getName(), $ref->getName()) : NULL;
155
 
        }
156
 
 
157
 
 
158
 
        /**
159
 
         * @return Extension|NULL
160
 
         */
161
 
        public function getExtension()
162
 
        {
163
 
                return ($name = $this->getExtensionName()) ? new Extension($name) : NULL;
164
 
        }
165
 
 
166
 
 
167
 
        /**
168
 
         * @return ClassType[]
169
 
         */
170
 
        public function getInterfaces()
171
 
        {
172
 
                $res = array();
173
 
                foreach (parent::getInterfaceNames() as $val) {
174
 
                        $res[$val] = new static($val);
175
 
                }
176
 
                return $res;
177
 
        }
178
 
 
179
 
 
180
 
        /**
181
 
         * @return Method
182
 
         */
183
 
        public function getMethod($name)
184
 
        {
185
 
                return new Method($this->getName(), $name);
186
 
        }
187
 
 
188
 
 
189
 
        /**
190
 
         * @return Method[]
191
 
         */
192
 
        public function getMethods($filter = -1)
193
 
        {
194
 
                foreach ($res = parent::getMethods($filter) as $key => $val) {
195
 
                        $res[$key] = new Method($this->getName(), $val->getName());
196
 
                }
197
 
                return $res;
198
 
        }
199
 
 
200
 
 
201
 
        /**
202
 
         * @return ClassType|NULL
203
 
         */
204
 
        public function getParentClass()
205
 
        {
206
 
                return ($ref = parent::getParentClass()) ? new static($ref->getName()) : NULL;
207
 
        }
208
 
 
209
 
 
210
 
        /**
211
 
         * @return Property[]
212
 
         */
213
 
        public function getProperties($filter = -1)
214
 
        {
215
 
                foreach ($res = parent::getProperties($filter) as $key => $val) {
216
 
                        $res[$key] = new Property($this->getName(), $val->getName());
217
 
                }
218
 
                return $res;
219
 
        }
220
 
 
221
 
 
222
 
        /**
223
 
         * @return Property
224
 
         */
225
 
        public function getProperty($name)
226
 
        {
227
 
                return new Property($this->getName(), $name);
228
 
        }
229
 
 
230
 
 
231
 
        /********************* Nette\Annotations support ****************d*g**/
232
 
 
233
 
 
234
 
        /**
235
 
         * Has class specified annotation?
236
 
         * @param  string
237
 
         * @return bool
238
 
         */
239
 
        public function hasAnnotation($name)
240
 
        {
241
 
                $res = AnnotationsParser::getAll($this);
242
 
                return !empty($res[$name]);
243
 
        }
244
 
 
245
 
 
246
 
        /**
247
 
         * Returns an annotation value.
248
 
         * @param  string
249
 
         * @return IAnnotation
250
 
         */
251
 
        public function getAnnotation($name)
252
 
        {
253
 
                $res = AnnotationsParser::getAll($this);
254
 
                return isset($res[$name]) ? end($res[$name]) : NULL;
255
 
        }
256
 
 
257
 
 
258
 
        /**
259
 
         * Returns all annotations.
260
 
         * @return IAnnotation[][]
261
 
         */
262
 
        public function getAnnotations()
263
 
        {
264
 
                return AnnotationsParser::getAll($this);
265
 
        }
266
 
 
267
 
 
268
 
        /**
269
 
         * Returns value of annotation 'description'.
270
 
         * @return string
271
 
         */
272
 
        public function getDescription()
273
 
        {
274
 
                return $this->getAnnotation('description');
275
 
        }
276
 
 
277
 
 
278
 
        /********************* Nette\Object behaviour ****************d*g**/
279
 
 
280
 
 
281
 
        /**
282
 
         * @return ClassType
283
 
         */
284
 
        public static function getReflection()
285
 
        {
286
 
                return new ClassType(get_called_class());
287
 
        }
288
 
 
289
 
 
290
 
        public function __call($name, $args)
291
 
        {
292
 
                return ObjectMixin::call($this, $name, $args);
293
 
        }
294
 
 
295
 
 
296
 
        public function &__get($name)
297
 
        {
298
 
                return ObjectMixin::get($this, $name);
299
 
        }
300
 
 
301
 
 
302
 
        public function __set($name, $value)
303
 
        {
304
 
                return ObjectMixin::set($this, $name, $value);
305
 
        }
306
 
 
307
 
 
308
 
        public function __isset($name)
309
 
        {
310
 
                return ObjectMixin::has($this, $name);
311
 
        }
312
 
 
313
 
 
314
 
        public function __unset($name)
315
 
        {
316
 
                ObjectMixin::remove($this, $name);
317
 
        }
318
 
 
319
 
}