/** * @return string the configuration as a string */ public function dump() { $configuration = $this->configuration->getPreface() . PHP_EOL; foreach ($this->configuration->getSections() as $section) { $configuration .= $this->writeSection($section); foreach ($section->getMagicComments() as $magicComment) { $configuration .= $this->indent . $this->writeMagicComment($magicComment) . PHP_EOL; } foreach ($section->getParameters() as $parameter) { $configuration .= $this->indent . $this->writeParameter($parameter) . PHP_EOL; } $configuration .= PHP_EOL; } // Ensure there's only one empty line in the end return trim($configuration) . PHP_EOL; }
/** * Tests that indent handling works correctly */ public function testIndent() { $configuration = new Configuration(); $configuration->setPreface('# PREFACE'); $section = new GlobalSection(); $section->addParameter(new Parameter('foo', 'bar')); $configuration->addSection($section); $writer = new Writer($configuration); $writer->setIndent('INDENT'); $expected = <<<EOD # PREFACE global INDENTfoo bar EOD; $this->assertEquals($expected, $writer->dump()); }
/** * @return Configuration */ public function parse() { $configuration = new Configuration(); $currentSection = null; // Parse the preface $configuration->setPreface($this->parsePreface()); foreach ($this->getNormalizedConfigurationLines() as $line) { // Check for section changes $newSection = Factory::makeFactory($line); if ($newSection !== null) { $currentSection = $newSection; $configuration->addSection($currentSection); continue; } // Parse the current section line by line if ($currentSection !== null) { $this->parseSectionLine($currentSection, $line); } } return $configuration; }
/** * Tests that getSingleSection() and getMultipleSections() work properly */ public function testGetSections() { $configuration = new Configuration(); $configuration->addSection(new DefaultsSection()); $configuration->addSection(new FrontendSection('frontend frontend-1')); $configuration->addSection(new FrontendSection('frontend frontend-2')); $configuration->addSection(new FrontendSection('frontend frontend-3')); $this->assertNotNull($configuration->getDefaultsSection()); $this->assertCount(3, $configuration->getFrontendSections()); }