/** * Creates an instance with decorators specified in a YAML file * @param string $path The path to the file * @return Context * @throws Exception when the given file is not readable */ public static function fromYaml($path) { if (!is_file($path)) { throw new Exception('File ' . $path . ' not found'); } $parser = new Parser(); $contexts = $parser->parse(file_get_contents($path)); reset($contexts); $mainContextName = key($contexts); $mainContextDecorators = array_shift($contexts); if (is_array($mainContextDecorators)) { $mainContext = new Context($mainContextName); $mainSelector = $mainContext->select($mainContextName); $mainSelector->addMultiple($mainContextDecorators); } else { $file = dirname($path) . '/' . $mainContextDecorators . '.yaml'; if (!is_file($file)) { throw new Exception('Cannot extend file ' . $file); } $mainContext = ContextFactory::fromYaml($file); } foreach ($contexts as $selector => $decorators) { $selector = $mainContext->select($selector); $selector->addMultiple($decorators); } return $mainContext; }
public function testNestedSelectors() { $context = new Context(); $context->select('foo bar')->add('dummy'); $sub = $context->createContext('foo'); $actual = $sub->select('bar')->decorators['dummy']; $expected = new Dummy(); $this->assertEquals($expected, $actual); }
public function testComputedTags() { $context = new Context('foo'); $context->setContent(array('one' => 'value', 'two' => '', 'three' => 'value')); $context->select('sub')->add('content', array('computedTags' => true)); $decorator = new Contexts(array('name' => 'sub', 'separator' => ';')); $decorator->context($context); $actual = $decorator->render(); $expected = 'first,even;empty,odd;last,even;'; $this->assertEquals($expected, $actual); }