public function filterContent(sfEvent $event, $content) { $prepend = sfDynamicsConfig::getAssetsPositionInHead() == 'prepend'; $response = $event->getSubject(); $pos = $prepend ? strpos($content, '<head>') + 6 : strpos($content, '</head>'); if (false !== $pos) { $html = $this->generateAssetsHtml(); if ($html) { $content = substr($content, 0, $pos) . "\n" . $html . substr($content, $pos); } } return $content; }
<html> <head> <link rel="stylesheet" type="text/css" media="screen" href="/bar.css" /> <script type="text/javascript" src="/bar.js"></script> <link rel="stylesheet" type="text/css" media="screen" href="/dynamics/foo.css" /> <script type="text/javascript" src="/dynamics/foo.js"></script> </head> <body> <p>Lorem ipsum</p> </body> </html> END; $t->is($manager->filterContent(new sfEvent('lorem', 'event.name'), $content), $waited, '->addSfDynamicsTags() inserts tags at the bottom of <head> if position "append" and placeholder not found "(default)"'); } sfConfig::set('app_sfDynamicsPlugin_assets_position_in_head', 'prepend'); if ('prepend' !== sfDynamicsConfig::getAssetsPositionInHead()) { $t->error('We can’t test prepend inclusion'); $t->skip('skip 1 tests', 1); } else { $content = <<<END <html> <head> <link rel="stylesheet" type="text/css" media="screen" href="/bar.css" /> <script type="text/javascript" src="/bar.js"></script> </head> <body> <p>Lorem ipsum</p> </body> </html> END; $waited = <<<END
/** * Add sfDynamics css and js tags to the content */ protected function addSfDynamicsTags($content, $ext) { if (!in_array($ext, array('css', 'js'))) { throw new sfDynamicsException(sprintf('"%s" is an unknown file extension', $type)); } // Retrieve placeholders dedicated to this extension $placeholder = call_user_func(array('sfDynamicsConfig', 'get' . ucfirst($ext) . 'Placeholder')); // Retrieve the tags to insert if (!($sfDynamicTags = $this->{'generate' . ucfirst($ext) . 'Html'}())) { // Nothing to add, same content whithout placeholder return str_ireplace($placeholder, '', $content); } if (false !== stripos($content, $placeholder)) { return str_ireplace($placeholder, $sfDynamicTags, $content); } // Placeholder not found, insertion in the <head> section if ('prepend' === sfDynamicsConfig::getAssetsPositionInHead()) { return str_ireplace('<head>', '<head>' . "\n" . $sfDynamicTags, $content); } return str_ireplace('</head>', $sfDynamicTags . "\n" . '</head>', $content); }