/Classes/Parser/Impl/ProjectParser.php
https://github.com/RichardDownes/PHPCodeParser · PHP · 198 lines · 73 code · 40 blank · 85 comment · 7 complexity · a01ef84a25056c329535c143ad645440 MD5 · raw file
- <?php
- namespace Parser\Impl;
- /*
- * Copyright 2011 Richard Downes
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- use Meta\PHPNamespace;
- use Meta\AbstractPHPStructure;
- /**
- * Contains Parser\ProjectParser
- *
- * @author Richard Downes <richard@sculpt.ie>
- * @copyright Copyright (c) 2011, Richard Downes
- * @package Parser.Impl
- */
- /**
- * Responsible for parsing an entire project
- *
- * @author Richard Downes <richard@sculpt.ie>
- */
- class ProjectParser
- {
- /**
- * The namespaces found in the project
- *
- * @var array $namespaces A list of namespaces
- */
- protected $namespaces;
- /**
- * Constructs a new instance of ProjectParser
- *
- */
- public function __construct()
- {
- $this->namespaces = array();
- }
- /**
- * Responsible for parsing the files in a directory
- *
- * @param string $directory The directory to parse
- * @return \Meta\PHPNamespace[]
- */
- public function parse($directory)
- {
- $iterator = new \DirectoryIterator($directory);
- foreach($iterator as $fileInfo)
- {
- if ($this->isParseableDirectory($fileInfo))
- {
- $this->parse($fileInfo->getPathName());
- }
- else if ($this->isParseableFile($fileInfo))
- {
- $structures = $this->parseFile($fileInfo->getPathName());
- $this->addStructures($structures);
- }
- }
- return $this->getNamespaces();
- }
- /**
- * Responsible for adding a list of structures to the overall list
- *
- * @param array $structures The structures to add
- * @return void
- */
- public function addStructures($structures)
- {
- foreach($structures as $structure)
- {
- $this->addStructure($structure);
- }
- }
- /**
- * Responsible for adding a structure to the correct namespace
- *
- * @param \Meta\AbstractPHPStructure $structure The structure to add
- * @return void
- */
- public function addStructure(AbstractPHPStructure $structure)
- {
- $ns = $structure->getNamespace();
- if (!array_key_exists($ns, $this->namespaces))
- {
- $namespace = new PHPNamespace();
- $namespace->setName($ns);
- $this->namespaces[$ns] = $namespace;
- }
- $this->namespaces[$ns]->addStructure($structure);
- }
- /**
- * Gets the list of namespaces that have been parsed
- *
- * @return array
- */
- public function getNamespaces()
- {
- return $this->namespaces;
- }
- /**
- * Sets the list of namespaces
- *
- * @param array $namespaces An array of PHPNamespaces objects
- * @return void
- */
- public function setNamespaces($namespaces)
- {
- $this->namespaces = $namespaces;
- }
- /**
- * Responsible for checking if a directory is parseable
- * This just checks if it is a directory and not a dot
- *
- * @param \SplFileInfo $fileInfo Information about the file to check
- * @return bool
- */
- protected function isParseableDirectory($fileInfo)
- {
- return ($fileInfo->isDir() && !$fileInfo->isDot());
- }
- /**
- * Responsible for checking if a file is parseable
- * This checks that the extension is .php
- *
- * @param \SQLFileInfo $fileInfo Information about the file to check
- * @return bool
- */
- protected function isParseableFile($fileInfo)
- {
- $tmp = strtolower($fileInfo->getFileName());
- $pos = strrpos($tmp, ".");
- return ($pos !== false && (substr($tmp, $pos) == ".php"));
- }
- /**
- * Responsible for parsing a file
- *
- * @param string $file The full path to the file to parse
- * @return \Meta\AbstractPHPStructure[]
- */
- protected function parseFile($file)
- {
- $content = file_get_contents($file);
- $tokens = token_get_all($content);
- $parser = new FileParser();
- $parser->parse($tokens);
- return $parser->getStructures();
- }
- }