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\Annotations;
|
Chris@0
|
21
|
Chris@0
|
22 use SplFileObject;
|
Chris@0
|
23
|
Chris@0
|
24 /**
|
Chris@0
|
25 * Parses a file for namespaces/use/class declarations.
|
Chris@0
|
26 *
|
Chris@0
|
27 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
28 * @author Christian Kaps <christian.kaps@mohiva.com>
|
Chris@0
|
29 */
|
Chris@0
|
30 final class PhpParser
|
Chris@0
|
31 {
|
Chris@0
|
32 /**
|
Chris@0
|
33 * Parses a class.
|
Chris@0
|
34 *
|
Chris@0
|
35 * @param \ReflectionClass $class A <code>ReflectionClass</code> object.
|
Chris@0
|
36 *
|
Chris@0
|
37 * @return array A list with use statements in the form (Alias => FQN).
|
Chris@0
|
38 */
|
Chris@0
|
39 public function parseClass(\ReflectionClass $class)
|
Chris@0
|
40 {
|
Chris@0
|
41 if (method_exists($class, 'getUseStatements')) {
|
Chris@0
|
42 return $class->getUseStatements();
|
Chris@0
|
43 }
|
Chris@0
|
44
|
Chris@12
|
45 if (false === $filename = $class->getFileName()) {
|
Chris@0
|
46 return array();
|
Chris@0
|
47 }
|
Chris@0
|
48
|
Chris@0
|
49 $content = $this->getFileContent($filename, $class->getStartLine());
|
Chris@0
|
50
|
Chris@0
|
51 if (null === $content) {
|
Chris@0
|
52 return array();
|
Chris@0
|
53 }
|
Chris@0
|
54
|
Chris@0
|
55 $namespace = preg_quote($class->getNamespaceName());
|
Chris@0
|
56 $content = preg_replace('/^.*?(\bnamespace\s+' . $namespace . '\s*[;{].*)$/s', '\\1', $content);
|
Chris@0
|
57 $tokenizer = new TokenParser('<?php ' . $content);
|
Chris@0
|
58
|
Chris@0
|
59 $statements = $tokenizer->parseUseStatements($class->getNamespaceName());
|
Chris@0
|
60
|
Chris@0
|
61 return $statements;
|
Chris@0
|
62 }
|
Chris@0
|
63
|
Chris@0
|
64 /**
|
Chris@0
|
65 * Gets the content of the file right up to the given line number.
|
Chris@0
|
66 *
|
Chris@0
|
67 * @param string $filename The name of the file to load.
|
Chris@0
|
68 * @param integer $lineNumber The number of lines to read from file.
|
Chris@0
|
69 *
|
Chris@12
|
70 * @return string|null The content of the file or null if the file does not exist.
|
Chris@0
|
71 */
|
Chris@0
|
72 private function getFileContent($filename, $lineNumber)
|
Chris@0
|
73 {
|
Chris@0
|
74 if ( ! is_file($filename)) {
|
Chris@0
|
75 return null;
|
Chris@0
|
76 }
|
Chris@0
|
77
|
Chris@0
|
78 $content = '';
|
Chris@0
|
79 $lineCnt = 0;
|
Chris@0
|
80 $file = new SplFileObject($filename);
|
Chris@0
|
81 while (!$file->eof()) {
|
Chris@0
|
82 if ($lineCnt++ == $lineNumber) {
|
Chris@0
|
83 break;
|
Chris@0
|
84 }
|
Chris@0
|
85
|
Chris@0
|
86 $content .= $file->fgets();
|
Chris@0
|
87 }
|
Chris@0
|
88
|
Chris@0
|
89 return $content;
|
Chris@0
|
90 }
|
Chris@0
|
91 }
|