/** * Tests setting, getting and removing custom media types. * * @return void */ public function testMediaTypes() { $result = Media::types(); $this->assertTrue(is_array($result)); $this->assertTrue(in_array('json', $result)); $this->assertFalse(in_array('my', $result)); $this->assertEqual($result, Media::formats()); $result = Media::type('json'); $expected = 'application/json'; $this->assertEqual($expected, $result['content']); $expected = array('view' => false, 'layout' => false, 'encode' => 'json_encode', 'decode' => 'json_decode'); $this->assertEqual($expected, $result['options']); Media::type('my', 'text/x-my', array('view' => '\\my\\custom\\View', 'layout' => false)); $result = Media::types(); $this->assertTrue(in_array('my', $result)); $result = Media::type('my'); $expected = 'text/x-my'; $this->assertEqual($expected, $result['content']); $expected = array('view' => '\\my\\custom\\View', 'template' => null, 'layout' => null, 'encode' => null, 'decode' => null); $this->assertEqual($expected, $result['options']); Media::type('my', false); $result = Media::types(); $this->assertFalse(in_array('my', $result)); }
/** * Tests that the `Media` class' configuration can be reset to its default state. * * @return void */ public function testStateReset() { $this->assertFalse(in_array('foo', Media::types())); Media::type('foo', 'text/x-foo'); $this->assertTrue(in_array('foo', Media::types())); Media::reset(); $this->assertFalse(in_array('foo', Media::types())); }
/** * Run the Lexer and gather the block objects * @param blob $source template contents * @param string $template path to template * @return object|string BlockSet object or path to cached template */ public static function run($template, $data = array(), array $options = array()) { /** * Lets check for cache, if exists then we'll return the cache file name * @var Cache */ $options += array('type' => 'html'); $_cache = new Cache(); $file = static::_template($template); $_cacheFile = sha1($file . $options['type']); if ($_isCached = $_cache->file($_cacheFile) and $_cache->cache()) { return $_isCached; } /** * Guess there was no cache file, lets lex this mother. */ $source = self::_read($template); $_blockSet = static::$_blockSet; $template = self::_template($template); // Set the final template, this gets reset on every iteration to the current template // landing, finally, on the last. static::$_blockSet->templates($template); foreach (static::$_terminals as $pattern => $terminal) { // attempt to match block/parent regex $_preg = $terminal == "T_BLOCK" ? preg_match_all($pattern, $source, $matches) : preg_match($pattern, $source, $matches); if ($_preg) { // Blocks, load them in the BlockSet if ($terminal == "T_BLOCK") { foreach ($matches[2] as $index => $name) { /** * Block parameters * Options that can be set to modify how the block is rendered * @var [type] */ $params = explode(',', $matches[3][$index]); $params = array_map(function ($param) { return trim($param); }, $params); static::$_blockSet->push($name, $matches[4][$index], $template, $params); } // Parent templates, read these and throw them back to the lexer. } else { if (!empty(static::$_hierarchy)) { list($type, $file) = array_slice($matches, 2); if (empty($type)) { $type = 'template'; } else { $type = trim($type); } // override the controller if more than simple file name is passed in. First directory becomes controller $file = explode('/', ltrim($file, '/')); if (count($file) > 1) { $options['controller'] = array_shift($file); } // set filename to template/layout $file = implode('/', $file); preg_match('/(' . implode('|', Media::types()) . ')\\.php$/', $file, $extensions); if (count($extensions) == 2) { // remove .{:type}.php $file = substr($file, 0, strlen($file) - strlen($extensions[0]) - 1); // set type to render as extension specified $options['type'] = $extensions[1]; } $options[$type] = $file; // template path from \lithium\template\view\adapter\File $_template = static::$_hierarchy->template($type, $options); // if $_hierarchy not passed in, use template as is } else { $_template = $matches[3]; } static::run($_template, $data, $options); } } } return static::$_blockSet; }