コード例 #1
0
ファイル: XmlConfigReader.php プロジェクト: graste/environaut
 /**
  * Reads the config from the given location and
  * returns the data as an associative array.
  *
  * @param mixed $location location to read config data from (usually file/directory path)
  *
  * @return array config data as associative array
  *
  * @throws \InvalidArgumentException in case of problems handling the given location
  * @throws \DomException in case of schema validation errors
  */
 public function getConfigData($location)
 {
     if (is_dir($location)) {
         $location = $location . DIRECTORY_SEPARATOR . 'environaut.xml';
     }
     $this->doc = $doc = new Dom\DomDocument();
     $doc->load($location, LIBXML_NOCDATA);
     $doc->xinclude(LIBXML_NOCDATA);
     $doc->schemaValidate(__DIR__ . '/Schema/environaut.xsd');
     $config_data = array();
     $value = $doc->getElementValue('name');
     if (null !== $value) {
         $config_data['name'] = $value;
     }
     $value = $doc->getElementValue('description');
     if (null !== $value) {
         $config_data['description'] = $value;
     }
     $value = $doc->getElementValue('introduction');
     if (null !== $value) {
         $config_data['introduction'] = $value;
     }
     $keywords_element = $doc->getElement('keywords');
     if (null !== $keywords_element) {
         $keywords = array();
         foreach ($keywords_element->getChildren('keyword') as $keyword) {
             $keywords[] = (string) $keyword;
         }
         if (!empty($keywords)) {
             $config_data['keywords'] = $keywords;
         }
     }
     $element = $doc->getElement('cache');
     if (null !== $element) {
         $config_data['cache'] = $element->getParameters();
         $value = $element->getAttributeValue('class');
         if (null !== $value) {
             $config_data['cache']['__class'] = $value;
         }
     }
     $element = $doc->getElement('report');
     if (null !== $element) {
         $config_data['report'] = $element->getParameters();
         $value = $element->getAttributeValue('class');
         if (null !== $value) {
             $config_data['report']['__class'] = $value;
         }
     }
     $element = $doc->getElement('runner');
     if (null !== $element) {
         $config_data['runner'] = $element->getParameters();
         $value = $element->getAttributeValue('class');
         if (null !== $value) {
             $config_data['runner']['__class'] = $value;
         }
     }
     $element = $doc->getElement('export');
     if (null !== $element) {
         $config_data['export'] = $element->getParameters();
         $formatter_nodes = $element->get('formatters');
         $formatters = array();
         foreach ($formatter_nodes as $formatter_node) {
             $attributes = $formatter_node->getAttributes();
             foreach ($attributes as $name => $value) {
                 if ($name === 'class') {
                     $attributes['__class'] = $value;
                 }
             }
             unset($attributes['class']);
             $parameters = $formatter_node->getParameters();
             $formatters[] = array_merge($parameters, $attributes);
         }
         $config_data['export']['formatters'] = $formatters;
         $value = $element->getAttributeValue('class');
         if (null !== $value) {
             $config_data['export']['__class'] = $value;
         }
     }
     $element = $doc->getElement('checks');
     if (null !== $element) {
         $checks = array();
         $check_nodes = $element->get('check');
         foreach ($check_nodes as $check) {
             $attributes = $check->getAttributes();
             foreach ($attributes as $name => $value) {
                 if ($name === 'class') {
                     $attributes['__class'] = $value;
                 } elseif ($name === 'name') {
                     $attributes['__name'] = $value;
                 } elseif ($name === 'group') {
                     $attributes['__group'] = $value;
                 }
             }
             unset($attributes['class']);
             unset($attributes['name']);
             unset($attributes['group']);
             $parameters = $check->getParameters();
             $checks[] = array_merge($parameters, $attributes);
         }
         $config_data['checks'] = $checks;
     }
     return $config_data;
 }
コード例 #2
0
ファイル: DomDocumentTest.php プロジェクト: graste/environaut
    public function testExampleConfigReading()
    {
        $schema_file = realpath(__DIR__ . '/../../../../../../src/Environaut/Config/Reader/Schema/environaut.xsd');
        $this->assertTrue(is_readable($schema_file), 'Schema file ' . $schema_file . ' is not readable.');
        $doc = new DomDocument();
        $doc->load(__DIR__ . '/Fixtures/example.xml', LIBXML_NOCDATA);
        $doc->xinclude(LIBXML_NOCDATA);
        $doc->schemaValidate($schema_file);
        $this->assertSame('example with included checks', $doc->getElementValue('name'));
        $expected = <<<EOT

omgomgomg
        multiline
foobar
        stuff

EOT;
        $this->assertSame($expected, $doc->getElementValue('introduction'));
        $this->assertSame(null, $doc->getElementValue('description'));
        $this->assertSame('ec', $doc->getDefaultNamespacePrefix());
        $this->assertSame('http://mivesto.de/environaut/config/1.0', $doc->getDefaultNamespaceUri());
        $report_node = $doc->getElement('report');
        $this->assertNotNull($report_node);
        $this->assertTrue($report_node->hasParameters());
        $this->assertSame('Custom\\Report', $report_node->getAttributeValue('class'));
        $this->assertSame(array('class' => 'Custom\\Report'), $report_node->getAttributes());
        $this->assertSame(array('test' => 'hahaha'), $report_node->getParameters());
        $runner_node = $doc->getElement('runner');
        $this->assertNotNull($runner_node);
        $this->assertTrue($runner_node->hasParameters());
        $this->assertSame('Custom\\Runner', $runner_node->getAttributeValue('class'));
        $this->assertSame(array('class' => 'Custom\\Runner'), $runner_node->getAttributes());
        $this->assertSame(array('foo' => array(array('deeply_nested' => true, 'nested' => 'true', 'non_literalized' => 'on', 'literalized' => false, 'whitespace_preserved' => ' haha '))), $runner_node->getParameters());
        $keywords_expected = array('asdf', 'blub', 'foo');
        $keywords_actual = array();
        $keywords_node = $doc->getElement('keywords');
        foreach ($keywords_node as $keyword) {
            $keywords_actual[] = (string) $keyword;
        }
        $this->assertSame($keywords_expected, $keywords_actual);
        $keywords_actual = array();
        foreach ($keywords_node->getChildren('keyword') as $keyword) {
            $keywords_actual[] = (string) $keyword;
        }
        $this->assertSame($keywords_expected, $keywords_actual);
        $checks_node = $doc->getElement('checks');
        $this->assertNotNull($checks_node);
        $this->assertTrue($checks_node->hasChildren('check'));
        $this->assertSame(8, $checks_node->countChildren('check'));
        $checks = $checks_node->getChildren('check');
        $this->assertSame('Blah\\Check', $checks->item(0)->getAttributeValue('class'));
        $this->assertSame('group', $checks->item(0)->getAttributeValue('group'));
        $this->assertSame(true, $checks->item(0)->hasParameters());
        $this->assertSame('default value', $checks->item(0)->getAttributeValue('hahahaha', 'default value'));
    }