/** * @param string[] $units * @param string[] $visibilities * @return fDOMDocument */ private function createPhpDoxConfig(array $units, array $visibilities) { $dom = new fDOMDocument(); $dom->loadXML($this->factory->getConfigSkeleton()->renderStripped()); $dom->registerNamespace('config', ConfigLoader::XMLNS); // set up silent $silent = $this->output->getVerbosity() <= OutputInterface::VERBOSITY_NORMAL ? 'true' : 'false'; $dom->queryOne('//config:phpdox')->setAttribute('silent', $silent); // set up directories $projectNode = $dom->queryOne('//config:project'); $projectNode->setAttribute('source', $this->sourceDirectory); $projectNode->setAttribute('workdir', $this->buildDirectory); // inject flag to avoid unecessary processing according to the visibility filter if (1 === count($visibilities) && in_array('public', $visibilities)) { $collectorNode = $dom->queryOne('//config:collector'); $projectNode->setAttribute('publiconly', 'true'); } // remove current masks $query = "//config:collector/*[name()='include' or name()='exclude']"; foreach ($dom->query($query) as $mask) { $mask->parentNode->removeChild($mask); } // append files to be parsed $collector = $dom->queryOne('//config:collector'); foreach ($units as $unitFile) { $include = $dom->createElement('include'); $include->setAttribute('mask', $unitFile); $collector->appendChild($include); } return $dom; }
public function generate() { $document = new fDOMDocument(); // summary $root = $document->createElement('phpact'); $root->setAttribute('project', $this->summary->getProjectName()); $root->setAttribute('base', $this->summary->getBaseVersion()); $root->setAttribute('challenger', $this->summary->getChallengerVersion()); $root->setAttribute('date', date('c', $this->summary->getTime())); $root->setAttribute('signature', strip_tags($this->summary->getSignature())); // difference list $grouped = $this->groupDifferences($this->differences, Difference::UNIT_NAME); foreach ($grouped as $unitName => $differences) { /* @var $differences \RenanBr\PhpAct\Difference\DifferenceCollection */ $family = $differences->current()->getTag(Difference::UNIT_FAMILY); $unitElement = $root->createElement($family); $unitElement->setAttribute('name', $unitName); $this->append($unitElement, 'constant', $differences, Difference::CONSTANT_NAME); $this->append($unitElement, 'member', $differences, Difference::MEMBER_NAME); $this->append($unitElement, 'method', $differences, Difference::METHOD_NAME); $root->appendChild($unitElement); } $document->appendChild($root); $document->preserveWhiteSpace = false; $document->formatOutput = true; return $document->saveXML(); }
/** * @return array */ private function parse() { if ($this->specification !== NULL) { return; } $queries = array(); $states = array(); foreach ($this->dom->query('states/state') as $state) { /** @var TheSeer\fDOM\fDOMElement $state */ $states[$state->getAttribute('name')] = array('transitions' => array(), 'query' => $state->getAttribute('query')); $queries[] = $state->getAttribute('query'); } $operations = array(); foreach ($this->dom->query('operations/operation') as $operation) { /** @var TheSeer\fDOM\fDOMElement $operation */ $operations[$operation->getAttribute('name')] = array('allowed' => $operation->getAttribute('allowed'), 'disallowed' => $operation->getAttribute('disallowed')); } foreach ($this->dom->query('transitions/transition') as $transition) { /** @var TheSeer\fDOM\fDOMElement $transition */ $from = $transition->getAttribute('from'); $to = $transition->getAttribute('to'); $operation = $transition->getAttribute('operation'); $states[$from]['transitions'][$operation] = $to; } $this->specification = array('operations' => $operations, 'queries' => $queries, 'states' => $states); }
public function getUnitByName($name) { $parts = explode('\\', $name); $local = array_pop($parts); $namespace = join('\\', $parts); $indexNode = $this->index->queryOne(sprintf('//phpdox:namespace[@name="%s"]/*[@name="%s"]', $namespace, $local)); if (!$indexNode) { throw new DependencyException(sprintf("Unit '%s' not found", $name), DependencyException::UnitNotFound); } $dom = new fDOMDocument(); $dom->load($this->baseDir . '/' . $indexNode->getAttribute('xml')); switch ($indexNode->localName) { case 'interface': $unit = new InterfaceObject(); $unit->import($dom); $this->project->addInterface($unit); break; case 'trait': $unit = new TraitObject(); $unit->import($dom); $this->project->addTrait($unit); break; case 'class': $unit = new ClassObject(); $unit->import($dom); $this->project->addClass($unit); break; default: throw new DependencyException(sprintf("Invalid unit type '%s'", $indexNode->localName), DependencyException::InvalidUnitType); } return $unit; }
public function getUnitByName($name) { $parts = explode('\\', $name); $local = array_pop($parts); $namespace = join('\\', $parts); $indexNode = $this->index->queryOne(sprintf('//phpdox:namespace[@name="%s"]/*[@name="%s"]', $namespace, $local)); if (!$indexNode) { return; } $dom = new fDOMDocument(); $dom->load($this->baseDir . '/' . $indexNode->getAttribute('xml')); switch ($indexNode->localName) { case 'interface': $unit = new InterfaceObject(); $unit->import($dom); $this->project->addInterface($unit); break; case 'trait': $unit = new TraitObject(); $unit->import($dom); $this->project->addTrait($unit); break; case 'class': $unit = new ClassObject(); $unit->import($dom); $this->project->addClass($unit); break; } return $unit; }
public function import(fDOMDocument $dom) { $dom->registerNamespace('phpdox', 'http://xml.phpdox.net/src#'); $dir = $dom->queryOne('/phpdox:source/phpdox:dir'); if (!$dir) { return; } $this->importDirNode($dir, ''); }
private function init($cfgName) { $this->version = new Version('0.0'); $this->fileInfo = new FileInfo($this->baseDir . $cfgName . '.xml'); $this->cfgDom = new fDOMDocument(); $this->cfgDom->load($this->fileInfo->getPathname()); $this->cfgDom->registerNamespace('cfg', 'http://xml.phpdox.net/config'); $this->config = new GlobalConfig($this->version, new FileInfo('/tmp'), $this->cfgDom, $this->fileInfo); }
/** * @return string */ public function renderStripped() { $dom = new fDOMDocument(); $dom->preserveWhiteSpace = FALSE; $dom->loadXML(preg_replace("/\\s{2,}/u", " ", $this->render())); foreach ($dom->query('//comment()') as $c) { $c->parentNode->removeChild($c); } $dom->formatOutput = TRUE; return $dom->saveXML(); }
protected function loadDocument($dir) { $path = $dir . '/' . $this->getNode()->getAttribute('xml'); if (!isset($this->dom[$path])) { $classDom = new fDOMDocument(); $classDom->load($path); $classDom->registerNamespace('phpdox', 'http://xml.phpdox.net/src'); $this->dom[$path] = $classDom; } return $this->dom[$path]; }
public function asDom(\TheSeer\fDOM\fDOMDocument $ctx) { $node = $ctx->createElementNS('http://xml.phpdox.net/src', 'invalid'); $node->setAttribute('annotation', $this->name); foreach ($this->attributes as $attribute => $value) { $node->setAttribute($attribute, $value); } if ($this->body !== null && $this->body !== '') { $node->appendChild($ctx->createTextnode($this->body)); } return $node; }
private function loadXML($fname) { try { if (!file_exists($fname)) { throw new EnricherException(sprintf('PHPLoc xml file "%s" not found.', $fname), EnricherException::LoadError); } $this->dom = new fDOMDocument(); $this->dom->load($fname); } catch (fDOMException $e) { throw new EnricherException('Parsing PHPLoc xml file failed: ' . $e->getMessage(), EnricherException::LoadError); } }
protected function processFinding(fDOMDocument $dom, $ref, \DOMElement $finding) { $enrichment = $this->getEnrichtmentContainer($ref, 'checkstyle'); $enrichFinding = $dom->createElementNS(self::XMLNS, $finding->getAttribute('severity', 'error')); $enrichment->appendChild($enrichFinding); foreach ($finding->attributes as $attr) { if ($attr->localName == 'severity') { continue; } $enrichFinding->setAttributeNode($dom->importNode($attr, true)); } }
public function asDom(\TheSeer\fDOM\fDOMDocument $ctx) { $node = $ctx->createElementNS('http://xml.phpdox.net/src#', strtolower($this->name)); foreach ($this->attributes as $attribute => $value) { $node->setAttribute($attribute, $value); } if ($this->body !== null && $this->body !== '') { $parser = $this->factory->getInstanceFor('InlineProcessor', $ctx); $node->appendChild($parser->transformToDom($this->body)); } return $node; }
/** * @ covers TheSeer\phpDox\DocBlock\Parser::__construct * @ covers TheSeer\phpDox\DocBlock\Parser::parse * * @dataProvider docblockSources */ public function testParse($src) { $expected = new fDOMDocument(); $dir = __DIR__ . '/../../data/docbock/'; $block = file_get_contents($dir . $src); $expected->load($dir . $src . '.xml'); $factory = new Factory(); $parser = new Parser($factory); $result = $parser->parse($block, array()); $this->assertInstanceOf('TheSeer\\phpDox\\DocBlock\\DocBlock', $result); $dom = new fDOMDocument(); $dom->appendChild($result->asDom($dom)); $this->assertEquals($expected->documentElement, $dom->documentElement); }
/** * @param \TheSeer\fDOM\fDOMDocument $doc * @return \TheSeer\fDOM\fDOMElement */ public function asDom(\TheSeer\fDOM\fDOMDocument $doc) { $node = $doc->createElementNS('http://xml.phpdox.net/src#', 'docblock'); // add lines and such? foreach ($this->elements as $element) { if (is_array($element)) { foreach ($element as $el) { $node->appendChild($el->asDom($doc)); } continue; } $node->appendChild($element->asDom($doc)); } return $node; }
/** * @param AbstractUnitObject $unit */ public function importExports(AbstractUnitObject $unit, $container = 'parent') { $parent = $this->rootNode->queryOne(sprintf('//phpdox:%s[@full="%s"]', $container, $unit->getName())); if ($parent instanceof fDOMElement) { $parent->parentNode->removeChild($parent); } $parent = $this->rootNode->appendElementNS(self::XMLNS, $container); $parent->setAttribute('full', $unit->getName()); $parent->setAttribute('namepsace', $unit->getNamespace()); $parent->setAttribute('name', $unit->getLocalName()); if ($unit->hasExtends()) { foreach ($unit->getExtends() as $name) { $extends = $parent->appendElementNS(self::XMLNS, 'extends'); $this->setName($name, $extends); } } foreach ($unit->getConstants() as $constant) { $parent->appendChild($this->dom->importNode($constant->export(), TRUE)); } foreach ($unit->getExportedMembers() as $member) { $parent->appendChild($this->dom->importNode($member->export(), TRUE)); } foreach ($unit->getExportedMethods() as $method) { $parent->appendChild($this->dom->importNode($method->export(), TRUE)); } }
/** * https://github.com/theseer/fDOMDocument/issues/15 */ public function testQueryReturnsNodeFromClonedDocument() { $this->dom->loadXML('<?xml version="1.0" ?><test />'); $clone = clone $this->dom; $node = $clone->queryOne('/test'); $this->assertNotSame($this->dom->documentElement, $node); }
private function getCacheDom() { if ($this->cacheDom === NULL) { $this->cacheDom = new fDOMDocument(); $cacheFile = $this->config->getLogfilePath(); if (file_exists($cacheFile)) { $this->cacheDom->load($cacheFile); $sha1 = $this->cacheDom->documentElement->getAttribute('sha1'); $cwd = getcwd(); chdir($this->config->getSourceDirectory()); exec($this->config->getGitBinary() . ' diff --name-only ' . $sha1, $files, $rc); foreach ($files as $file) { $fields = array('path' => dirname($file), 'file' => basename($file)); $query = $this->cacheDom->prepareQuery('//*[@path = :path and @file = :file]', $fields); $node = $this->cacheDom->queryOne($query); if (!$node) { continue; } $node->parentNode->removeChild($node); } chdir($cwd); } else { $this->cacheDom->loadXML('<?xml version="1.0" ?><gitlog xmlns="' . self::GITNS . '" />'); $this->cacheDom->documentElement->setAttribute('sha1', $this->commitSha1); } } return $this->cacheDom; }
public function testGetChildrenByTagnameNSReturnsCorrectNodelist() { $this->dom->loadXML('<?xml version="1.0" ?><root xmlns="test:uri"><node><child/></node><node /></root>'); $this->node = $this->dom->documentElement; $list = $this->node->getChildrenByTagNameNS('test:uri', 'node'); $this->assertInstanceOf('DOMNodeList', $list); $this->assertEquals(2, $list->length); }
public function testAppendingANewElementWithinANamespaceByPrefix() { $this->dom->registerNamespace('t', 'test:uri'); $node = $this->frag->appendElementPrefix('t', 'append', 'text'); $this->assertInstanceOf('TheSeer\\fDOM\\fDOMElement', $node); $this->assertEquals(1, $this->frag->query('count(t:append)')); $this->assertEquals('text', $node->nodeValue); }
/** * @param string $source * * @return fDOMDocument * * @throws \TheSeer\fDOM\fDOMException */ public function toXML($source) { $this->writer = new \XMLWriter(); $this->writer->openMemory(); $this->writer->setIndent(true); $this->writer->startDocument(); $this->writer->startElement('source'); $this->writer->writeAttribute('xmlns', 'http://xml.phpdox.net/token'); $this->writer->startElement('line'); $this->writer->writeAttribute('no', 1); $this->lastLine = 1; $tokens = token_get_all($source); foreach ($tokens as $pos => $tok) { if (is_string($tok)) { $line = 1; $step = 1; while (!is_array($tokens[$pos - $step])) { $step++; if ($pos - $step == -1) { break; } } if ($pos - $step != -1) { $line = $tokens[$pos - $step][2]; $line += count(preg_split('/\\R+/', $tokens[$pos - $step][1])) - 1; } $token = array('name' => $this->map[$tok], 'value' => $tok, 'line' => $line); $this->addToken($token); } else { $line = $tok[2]; $values = preg_split('/\\R+/Uu', $tok[1]); foreach ($values as $v) { $token = array('name' => token_name($tok[0]), 'value' => $v, 'line' => $line); $this->addToken($token); $line++; } } } $this->writer->endElement(); $this->writer->endElement(); $this->writer->endDocument(); $dom = new fDOMDocument(); $dom->preserveWhiteSpace = false; $dom->loadXML($this->writer->outputMemory()); return $dom; }
private function getMethod($name) { $ctx = $this->dom->queryOne(sprintf('phpdox:method[@name="%s"]', $name)); if (!$ctx) { throw new UnitObjectException(sprintf('Method "%s" not found', $name), UnitObjectException::NoSuchMethod); } return new MethodObject($this, $ctx); }
/** * @return array */ public function getProjects() { $list = array(); foreach ($this->cfg->query('//cfg:project[@enabled="true" or not(@enabled)]') as $pos => $project) { $list[$project->getAttribute('name', $pos)] = new ProjectConfig($this->runResolver($project)); } return $list; }
public function testCSSSelectorHonorsContextNode() { $this->dom->load(__DIR__ . '/_data/selector.xml'); $ctx = $this->dom->getElementsByTagName('child')->item(0); $result = $this->dom->select('child', $ctx); $this->assertEquals(1, $result->length); $this->assertEquals('child', $result->item(0)->nodeName); $this->assertEquals('other', $result->item(0)->getAttribute('attr')); }
/** * @return void */ private function initCollections() { $this->source = new fDOMDocument(); $this->source->load($this->xmlDir . '/source.xml'); $this->source->registerNamespace('phpdox', 'http://xml.phpdox.net/src'); $this->index = new fDOMDocument(); $this->index->load($this->xmlDir . '/index.xml'); $this->index->registerNamespace('phpdox', 'http://xml.phpdox.net/src'); }
protected function createInstanceFor($fname) { try { $dom = new fDOMDocument(); $dom->load($fname); $root = $dom->documentElement; if ($root->namespaceURI == 'http://phpdox.de/config') { throw new ConfigLoaderException("File '{$fname}' uses an outdated xml namespace. Please update the xmlns to 'http://phpdox.net/config'", ConfigLoaderException::OldNamespace); } if ($root->namespaceURI != 'http://phpdox.net/config' || $root->localName != 'phpdox') { throw new ConfigLoaderException("File '{$fname}' is not a valid phpDox configuration.", ConfigLoaderException::WrongType); } $dom->registerNamespace('cfg', 'http://phpdox.net/config'); return new GlobalConfig($dom, new FileInfo($fname)); } catch (fDOMException $e) { throw new ConfigLoaderException("Parsing config file '{$fname}' failed.", ConfigLoaderException::ParseError, $e); } }
public function getProjectConfig($project) { $filter = is_int($project) ? $project : "@name = '{$project}'"; $ctx = $this->cfg->queryOne("//cfg:project[{$filter}]"); if (!$ctx) { throw new ConfigException("Project '{$project}' not found in configuration xml file", ConfigException::ProjectNotFound); } return new ProjectConfig($this->runResolver($ctx)); }
private function getGeneralBuildInfo() { if ($this->buildInfo != NULL) { return $this->buildInfo; } $dom = new fDOMDocument(); $this->buildInfo = $dom->createDocumentFragment(); $dateNode = $dom->createElementNS(self::XMLNS, 'date'); $this->buildInfo->appendChild($dateNode); $date = new \DateTime('now'); $dateNode->setAttribute('unix', $date->getTimestamp()); $dateNode->setAttribute('date', $date->format('d-m-Y')); $dateNode->setAttribute('time', $date->format('H:i:s')); $dateNode->setAttribute('iso', $date->format('c')); $dateNode->setAttribute('rfc', $date->format('r')); $phpdoxNode = $dom->createElementNS(self::XMLNS, 'phpdox'); $this->buildInfo->appendChild($phpdoxNode); $phpdoxNode->setAttribute('version', $this->version->getVersion()); $phpdoxNode->setAttribute('info', $this->version->getInfoString()); $phpdoxNode->setAttribute('generated', $this->version->getGeneratedByString()); $phpdoxNode->setAttribute('phar', defined('PHPDOX_PHAR') ? 'yes' : 'no'); foreach ($this->enrichers as $enricher) { $enricherNode = $phpdoxNode->appendElementNS(self::XMLNS, 'enricher'); $enricherNode->setAttribute('type', $enricher); } $phpNode = $dom->createElementNS(self::XMLNS, 'php'); $this->buildInfo->appendChild($phpNode); $phpNode->setAttribute('version', PHP_VERSION); $phpNode->setAttribute('os', PHP_OS); foreach (get_loaded_extensions(true) as $extension) { $extNode = $dom->createElementNS(self::XMLNS, 'zendextension'); $extNode->setAttribute('name', $extension); $phpNode->appendChild($extNode); } foreach (get_loaded_extensions(false) as $extension) { $extNode = $dom->createElementNS(self::XMLNS, 'extension'); $extNode->setAttribute('name', $extension); $phpNode->appendChild($extNode); } return $this->buildInfo; }
private function processViolations(fDOMDocument $dom, \DOMNodeList $violations) { foreach ($violations as $violation) { /** @var fDOMElement $violation */ $line = $violation->getAttribute('beginline'); $ref = $dom->queryOne(sprintf('//phpdox:*/*[@line = %d or (@start <= %d and @end >= %d)]', $line, $line, $line)); if (!$ref) { // One src file may contain multiple classes/traits/interfaces, so the // finding might not apply to the current object since violations are based on filenames // but we have individual objects - so we just ignore the finding for this context continue; } $enrichment = $this->getEnrichtmentContainer($ref, 'pmd'); $enrichViolation = $dom->createElementNS(self::XMLNS, 'violation'); $enrichment->appendChild($enrichViolation); $enrichViolation->setAttribute('message', trim($violation->nodeValue)); foreach ($violation->attributes as $attr) { $enrichViolation->setAttributeNode($dom->importNode($attr, true)); } } }
/** * @param string $filename * * @return Configuration */ public function load($filename) { $document = new fDOMDocument(); $document->load($filename); $configuration = new Configuration($document->getElementsByTagName('directory')->item(0)->textContent, $document->getElementsByTagName('domain')->item(0)->textContent, $document->getElementsByTagName('email')->item(0)->textContent); if ($document->getElementsByTagName('nginx')->item(0)) { $configuration->setNginxConfigurationFile($document->getElementsByTagName('nginx')->item(0)->textContent); } foreach ($document->getElementsByTagName('series') as $series) { /* @var fDOMElement $series */ $configuration->addAdditionalReleaseSeries($series->getAttribute('package'), $series->getAttribute('series'), $series->getAttribute('alias')); } return $configuration; }