/** * Creates a new FileReflector for the given filename or null if the file contains no modifications. * * @param ProjectDescriptorBuilder $builder * @param string $filename * * @return FileReflector|null Returns a new FileReflector or null if no modifications were detected for the given * filename. */ protected function createFileReflector(ProjectDescriptorBuilder $builder, $filename) { $file = new FileReflector($filename, $this->parser->doValidation(), $this->parser->getEncoding()); $file->setDefaultPackageName($this->parser->getDefaultPackageName()); $file->setMarkers($this->parser->getMarkers()); $file->setFilename($this->getRelativeFilename($filename)); $cachedFiles = $builder->getProjectDescriptor()->getFiles(); $hash = $cachedFiles->get($file->getFilename()) ? $cachedFiles->get($file->getFilename())->getHash() : null; return $hash === $file->getHash() && !$this->parser->isForced() ? null : $file; }
/** * Iterates through the given files feeds them to the builder. * * @param ProjectDescriptorBuilder $builder * @param Collection $files A files container to parse. * * @api * * @throws Exception if no files were found. * * @return bool|string */ public function parse(ProjectDescriptorBuilder $builder, Collection $files) { $timer = microtime(true); $paths = $this->getFilenames($files); $this->log(' Project root is: ' . $files->getProjectRoot()); $this->log(' Ignore paths are: ' . implode(', ', $files->getIgnorePatterns()->getArrayCopy())); if ($builder->getProjectDescriptor()->getSettings()->isModified()) { $this->setForced(true); $this->log('One of the project\'s settings have changed, forcing a complete rebuild'); } foreach ($paths as $filename) { if (class_exists('phpDocumentor\\Event\\Dispatcher')) { Dispatcher::getInstance()->dispatch('parser.file.pre', PreFileEvent::createInstance($this)->setFile($filename)); } $this->log('Starting to parse file: ' . $filename); $memory = memory_get_usage(); try { $file = new FileReflector($filename, $this->doValidation(), $this->getEncoding()); $file->setDefaultPackageName($this->getDefaultPackageName()); $file->setMarkers($this->getMarkers()); $file->setFilename($this->getRelativeFilename($filename)); // if the hash is unchanged; continue to the next file $cachedFiles = $builder->getProjectDescriptor()->getFiles(); $hash = $cachedFiles->get($file->getFilename()) ? $cachedFiles->get($file->getFilename())->getHash() : null; if ($hash === $file->getHash() && !$this->isForced()) { $this->log('>> Skipped file ' . $file->getFilename() . ' as no modifications were detected'); continue; } $file->process(); $builder->buildFileUsingSourceData($file); $fileDescriptor = $builder->getProjectDescriptor()->getFiles()->get($file->getFilename()); $errors = $fileDescriptor->getAllErrors(); foreach ($errors as $error) { $this->log($error->getCode(), $error->getSeverity(), $error->getContext()); } } catch (Exception $e) { $this->log(' Unable to parse file "' . $filename . '", an error was detected: ' . $e->getMessage(), LogLevel::ALERT); } $memoryDelta = memory_get_usage() - $memory; $this->log('>> Memory after processing of file: ' . number_format(memory_get_usage() / 1024 / 1024, 2) . ' megabytes (' . ($memoryDelta > -0 ? '+' : '') . number_format($memoryDelta / 1024) . ' kilobytes)', LogLevel::DEBUG); } $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 $builder->getProjectDescriptor(); }
/** * Runs a file through the static reflectors, generates an XML file element * and returns it. * * @param string $filename The filename to parse. * @param bool $include_source whether to include the source in the * generated output. * * @api * * @return void */ public function parseFile($filename, $include_source = false) { $this->log('Starting to parse file: ' . $filename); $this->log('Starting to parse file: ' . $filename, \phpDocumentor\Plugin\Core\Log::DEBUG); $dispatched = false; try { $file = new FileReflector($filename, $this->doValidation()); $file->setDefaultPackageName($this->getDefaultPackageName()); if (class_exists('phpDocumentor\\Event\\Dispatcher')) { \phpDocumentor\Event\Dispatcher::getInstance()->addListener('parser.log', array($file, 'addParserMarker')); } $dispatched = true; $file->setMarkers($this->getMarkers()); $file->setFilename($this->getRelativeFilename($filename)); // if an existing structure exists; and we do not force re-generation; // re-use the old definition if the hash differs if ($this->getExistingXml() !== null && !$this->isForced()) { $xpath = new \DOMXPath($this->getExistingXml()); // try to find the file with it's hash /** @var \DOMNodeList $qry */ $qry = $xpath->query('/project/file[@path=\'' . ltrim($file->getFilename(), './') . '\' and @hash=\'' . $file->getHash() . '\']'); // if an existing entry who matches the file, then re-use if ($qry->length > 0) { $this->exporter->getDomDocument()->documentElement->appendChild($this->exporter->getDomDocument()->importNode($qry->item(0), true)); $this->log('>> File has not changed since last build, re-using the ' . 'old definition'); } else { $this->log('Exporting file: ' . $filename); $file->process(); $this->exporter->setIncludeSource($include_source); $this->exporter->export($file); } } else { $this->log('Exporting file: ' . $filename); $file->process(); $this->exporter->setIncludeSource($include_source); $this->exporter->export($file); } } catch (Exception $e) { $this->log(' Unable to parse file "' . $filename . '", an error was detected: ' . $e->getMessage(), \phpDocumentor\Plugin\Core\Log::ALERT); $this->log('Unable to parse file "' . $filename . '", an error was detected: ' . $e->getMessage(), \phpDocumentor\Plugin\Core\Log::DEBUG); } //disconnects the dispatcher here so if any error occured, it still // removes the event if ($dispatched && class_exists('phpDocumentor\\Event\\Dispatcher')) { \phpDocumentor\Event\Dispatcher::getInstance()->removeListener('parser.log', array($file, 'addParserMarker')); } $this->log('>> Memory after processing of file: ' . number_format(memory_get_usage()) . ' bytes', \phpDocumentor\Plugin\Core\Log::DEBUG); $this->log('>> Parsed file', \phpDocumentor\Plugin\Core\Log::DEBUG); }