gergelypolonkai-web-symfony2/src/GergelyPolonkai/FrontBundle/Twig/RandomHeader.php

56 lines
1.1 KiB
PHP
Raw Normal View History

2012-09-05 15:41:00 +00:00
<?php
namespace GergelyPolonkai\FrontBundle\Twig;
use JMS\DiExtraBundle\Annotation as DI;
/**
* Description of RandomHeader
*
* @author polonkai.gergely
*
* @DI\Service
* @DI\Tag("twig.extension")
*/
class RandomHeader extends \Twig_Extension
{
/**
* @var string $rootDir
*/
private $rootDir;
/**
* @DI\InjectParams({
* "kernelRootDir" = @DI\Inject("%kernel.root_dir%")
* })
*/
public function __construct($kernelRootDir)
{
$this->rootDir = $kernelRootDir;
}
public function getGlobals()
{
return array(
'random_header' => $this->randomHeader(),
);
}
private function randomHeader()
{
$dh = opendir($this->rootDir . '/../web/bundles/gergelypolonkaifront/images');
$files = array();
while ($fn = readdir($dh)) {
if (preg_match('/^header_\d+\.png/', $fn)) {
$files[] = $fn;
}
}
2012-09-15 12:42:14 +00:00
2012-09-05 15:41:00 +00:00
return $files[array_rand($files)];
}
2012-09-15 12:42:14 +00:00
public function getName()
{
2012-09-05 15:41:00 +00:00
return 'random_header';
}
}