/** * Iterates through the given files and builds the structure.xml file. * * @param DocBlox_Parser_Files $files A files container to parse. * * @api * * @return bool|string */ public function parseFiles(DocBlox_Parser_Files $files) { $timer = microtime(true); $dom = new DOMDocument('1.0', 'utf-8'); $dom->formatOutput = true; $dom->loadXML('<project version="' . DocBlox_Core_Abstract::VERSION . '" ' . 'title="' . addslashes($this->getTitle()) . '"></project>'); $paths = $files->getFiles(); $this->log('Starting to process ' . count($paths) . ' files') . PHP_EOL; $this->log(' Project root is: ' . $files->getProjectRoot()) . PHP_EOL; $this->log(' Ignore paths are: ' . implode(', ', $files->getIgnorePatterns())) . PHP_EOL; if (count($paths) < 1) { throw new DocBlox_Parser_Exception('No files were found', DocBlox_Parser_Exception::NO_FILES_FOUND); } foreach ($paths as $file) { $xml = $this->parseFile($file); if ($xml === false) { continue; } $dom_file = new DOMDocument(); $dom_file->loadXML(trim($xml)); // merge generated XML document into the main document $xpath = new DOMXPath($dom_file); $qry = $xpath->query('/*'); for ($i = 0; $i < $qry->length; $i++) { $dom->documentElement->appendChild($dom->importNode($qry->item($i), true)); } } $this->buildPackageTree($dom); $this->buildNamespaceTree($dom); $this->buildMarkerList($dom); $xml = $dom->saveXML(); // Visibility rules $this->log('--'); $this->log('Applying visibility rules'); $dom = new DOMDocument(); $dom->loadXML($xml); $visibilityQry = '//*['; $accessQry = '//tag[@name=\'access\' and ('; foreach ($this->visibility as $key => $vis) { $visibilityQry .= '(@visibility!=\'' . $vis . '\')'; $accessQry .= '@description!=\'' . $vis . '\''; if ($key + 1 < count($this->visibility)) { $visibilityQry .= ' and '; $accessQry .= ' and '; } } $visibilityQry .= ']'; $accessQry .= ')]'; $qry = '(' . $visibilityQry . ') | (' . $accessQry . ')'; $xpath = new DOMXPath($dom); $nodes = $xpath->query($qry); foreach ($nodes as $node) { if ($node->nodeName == 'tag' && $node->parentNode->parentNode->parentNode) { $remove = $node->parentNode->parentNode; $node->parentNode->parentNode->parentNode->removeChild($remove); } else { $node->parentNode->removeChild($node); } } $xml = $dom->saveXML(); $this->log('--'); $this->log('Elapsed time to parse all files: ' . round(microtime(true) - $timer, 2) . 's'); $this->log('Peak memory usage: ' . round(memory_get_peak_usage() / 1024 / 1024, 2) . 'M'); return $xml; }
/** * Build a list of files (from the fileset elements) and call the DocBlox parser * @return string */ private function parseFiles() { $parser = new DocBlox_Parser(); DocBlox_Parser_Abstract::$event_dispatcher = new sfEventDispatcher(); $parser->setTitle($this->title); $paths = array(); // filesets foreach ($this->filesets as $fs) { $ds = $fs->getDirectoryScanner($this->project); $dir = $fs->getDir($this->project); $srcFiles = $ds->getIncludedFiles(); foreach ($srcFiles as $file) { $paths[] = $dir . FileSystem::getFileSystem()->getSeparator() . $file; } } $this->log("Will parse " . count($paths) . " file(s)", Project::MSG_VERBOSE); $files = new DocBlox_Parser_Files(); $files->addFiles($paths); $parser->setPath($files->getProjectRoot()); return $parser->parseFiles($files); }
/** * Execute the parsing process. * * @throws Zend_Console_Getopt_Exception * * @return void */ public function execute() { $files = new DocBlox_Parser_Files(); $files->setAllowedExtensions($this->getExtensions()); $files->setIgnorePatterns($this->getIgnore()); $paths = array_unique($this->getFilename() ? explode(',', $this->getFilename()) : DocBlox_Core_Abstract::config()->getArrayFromPath('files/file')); $files->addFiles($paths); $paths = array_unique($this->getDirectory() || !empty($paths) ? explode(',', $this->getDirectory()) : DocBlox_Core_Abstract::config()->getArrayFromPath('files/directory')); $files->addDirectories($paths); $parser = new DocBlox_Parser(); $parser->setTitle(htmlentities($this->getTitle())); $parser->setExistingXml($this->getTarget() . '/structure.xml'); $parser->setForced($this->getForce()); $parser->setMarkers($this->getMarkers()); $parser->setValidate($this->getValidate()); $parser->setVisibility($this->getVisibility()); $parser->setDefaultPackageName($this->getDefaultpackagename()); $parser->setPath($files->getProjectRoot()); try { // save the generate file to the path given as the 'target' option file_put_contents($this->getTarget() . '/structure.xml', $parser->parseFiles($files)); } catch (Exception $e) { if ($e->getCode() === DocBlox_Parser_Exception::NO_FILES_FOUND) { throw new Zend_Console_Getopt_Exception('No parsable files were found, did you specify any using the -f or -d parameter?'); } throw new Zend_Console_Getopt_Exception($e->getMessage()); } }