Skip to content

Commit 061ee16

Browse files
committed
Fallback to default theme if modern is not installed
1 parent 3b08528 commit 061ee16

File tree

9 files changed

+133
-11
lines changed

9 files changed

+133
-11
lines changed

src/Ladybug/DependencyInjection/AbstractCompilerPass.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ abstract class AbstractCompilerPass implements CompilerPassInterface
3535
* @param string $tag
3636
* @param string $method
3737
*/
38-
protected function processTaggedServices(ContainerBuilder $container, $manager, $tag, $method)
38+
protected function processTaggedServices(ContainerBuilder $container, $manager, $tag, $method, $params = array())
3939
{
4040
if (!$container->hasDefinition($manager)) {
4141
return;
@@ -46,9 +46,19 @@ protected function processTaggedServices(ContainerBuilder $container, $manager,
4646
$taggedServices = $container->findTaggedServiceIds($tag);
4747

4848
foreach ($taggedServices as $id => $attributes) {
49+
$methodParams = array();
50+
51+
if (!empty($attributes)) {
52+
foreach ($params as $param) {
53+
if (isset($attributes[0][$param])) {
54+
$methodParams[] = $attributes[0][$param];
55+
}
56+
}
57+
}
58+
4959
$definition->addMethodCall(
5060
$method,
51-
array(new Reference($id), $id)
61+
array_merge(array(new Reference($id), $id), $methodParams)
5262
);
5363
}
5464
}

src/Ladybug/DependencyInjection/ThemeCompilerPass.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class ThemeCompilerPass extends AbstractCompilerPass
2222
{
2323
public function process(ContainerBuilder $container)
2424
{
25-
$this->processTaggedServices($container, 'theme_resolver', self::TAG_THEME, 'addTheme');
25+
$this->processTaggedServices($container, 'theme_resolver', self::TAG_THEME, 'addTheme', array(
26+
'theme_default'
27+
));
2628
}
2729
}

src/Ladybug/Dumper.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ public function dump(/*$var1 [, $var2...$varN]*/)
111111
$render = $this->getRender();
112112
$render->setGlobals(array(
113113
'id' => uniqid(),
114-
'expanded' => $this->application->container->getParameter('expanded')
114+
'expanded' => $this->application->container->getParameter('expanded'),
115+
'theme' => $render->getTheme()->getName()
115116
));
116117

117118
return $render->render($this->nodes, array(

src/Ladybug/Renderer/HtmlRenderer.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,20 @@ protected function getCssContents()
4545

4646
// check last modification
4747
$lastModification = 0;
48+
4849
foreach ($theme->getHtmlCssDependencies() as $item) {
49-
$lastModification = max($lastModification, filemtime($theme->getResourcesPath().$item));
50+
$filename = $theme->getResourcesPath().$item;
51+
52+
if (file_exists($filename)) {
53+
$lastModification = max($lastModification, filemtime($filename));
54+
}
5055
}
5156

5257
// cache
5358
$cacheFile = sprintf('%s/ladybug_cache/theme/%s.css', sys_get_temp_dir(), $theme->getName());
54-
$lastModificationCache = file_exists($cacheFile) ? filemtime($cacheFile) : 0;
59+
$lastModificationCache = file_exists($cacheFile) ? filemtime($cacheFile) : -1;
5560

5661
if ($lastModification > $lastModificationCache) {
57-
5862
$pce = new \CssEmbed\CssEmbed();
5963

6064
$css = '';

src/Ladybug/Resources/container/themes.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</service>
1212

1313
<service id="theme_simple" class="Ladybug\Theme\Simple\SimpleTheme">
14-
<tag name="ladybug.theme" />
14+
<tag name="ladybug.theme" theme_default="true" />
1515
</service>
1616

1717
</services>

src/Ladybug/Theme/Base/View/Html/iframe.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<iframe id="ladybug_iframe_{{ id }}" frameborder="0" width="500" height="100" style="margin: 5px;overflow:hidden"></iframe>
2-
2+
<input type="hidden" id="ladybug_theme_{{ id }}" value="{{ theme }}" />
33
<script>
44
function resizeIframe{{ id }}(height, width) {
55
document.getElementById("ladybug_iframe_{{ id }}").height = height + "px";

src/Ladybug/Theme/ThemeResolver.php

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818
*
1919
* @author Raul Fraile <raulfraile@gmail.com>
2020
*/
21-
class ThemeResolver
21+
class ThemeResolver implements \Countable
2222
{
2323

2424
/** @var ThemeInterface[] An array of ThemeInterface objects */
2525
protected $themes;
2626

27+
protected $default = null;
28+
2729
/**
2830
* Constructor.
2931
*
@@ -40,9 +42,15 @@ public function __construct(array $themes = array())
4042
* @param ThemeInterface $theme A ThemeInterface instance
4143
* @param $key
4244
*/
43-
public function addTheme(ThemeInterface $theme, $key)
45+
public function addTheme(ThemeInterface $theme, $key, $default = false)
4446
{
4547
$this->themes[$key] = $theme;
48+
49+
if ($default) {
50+
$this->default = $key;
51+
}
52+
53+
return true;
4654
}
4755

4856
/**
@@ -64,6 +72,10 @@ public function resolve($format)
6472

6573
public function getTheme($key, $format)
6674
{
75+
if (!array_key_exists('theme_' . $key, $this->themes)) {
76+
return $this->getDefaultTheme();
77+
}
78+
6779
/** @var $theme ThemeInterface */
6880
$theme = $this->themes['theme_' . $key];
6981

@@ -88,4 +100,30 @@ protected function supportsFormat(ThemeInterface $theme, $format)
88100
{
89101
return in_array($format, $theme->getFormats());
90102
}
103+
104+
/**
105+
* Gets the default theme.
106+
*
107+
* @return ThemeInterface
108+
*/
109+
protected function getDefaultTheme()
110+
{
111+
if (!is_null($this->default) && array_key_exists($this->default, $this->themes)) {
112+
return $this->themes[$this->default];
113+
}
114+
115+
return null;
116+
}
117+
118+
/**
119+
* Count number of themes.
120+
*
121+
* @return int Number of registered themes
122+
*/
123+
public function count()
124+
{
125+
return count($this->themes);
126+
}
127+
128+
91129
}

tests/Ladybug/Tests/DumperTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ public function testThemeCanBeSet()
4141
$this->assertEquals('modern', $this->dumper->getTheme());
4242
}
4343

44+
public function testUnknownThemeFallbacksToDefault()
45+
{
46+
$var = 1;
47+
$this->dumper->setTheme('unknown');
48+
$this->dumper->setFormat(Format\HtmlFormat::FORMAT_NAME);
49+
50+
$html = $this->dumper->dump($var);
51+
52+
$crawler = new Crawler($html);
53+
$this->assertEquals('Simple', $crawler->filterXPath('//input[@type="hidden"]')->attr('value'));
54+
}
55+
4456
public function testFormatCanBeSet()
4557
{
4658
$this->dumper->setFormat(Format\XmlFormat::FORMAT_NAME);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace Ladybug\Tests\Theme;
4+
5+
use Ladybug\Theme\ThemeResolver;
6+
use Ladybug\Theme\ThemeInterface;
7+
use Ladybug\Format\HtmlFormat;
8+
use \Mockery as m;
9+
10+
class ThemeResolverTest extends \PHPUnit_Framework_TestCase
11+
{
12+
13+
/** @var ThemeResolver */
14+
protected $resolver;
15+
16+
protected $theme;
17+
18+
public function setUp()
19+
{
20+
$this->resolver = new ThemeResolver();
21+
22+
$this->theme = m::mock('Ladybug\Theme\ThemeInterface');
23+
$this->theme->shouldReceive('getFormats')->andReturn(array(HtmlFormat::FORMAT_NAME));
24+
}
25+
26+
public function tearDown()
27+
{
28+
m::close();
29+
}
30+
31+
public function testAddTheme()
32+
{
33+
$result = $this->resolver->addTheme($this->theme, 'theme_test');
34+
35+
$this->assertTrue($result);
36+
$this->assertCount(1, $this->resolver);
37+
}
38+
39+
public function testGetRegisteredTheme()
40+
{
41+
$this->resolver->addTheme($this->theme, 'theme_test');
42+
43+
$this->assertInstanceOf('Ladybug\Theme\ThemeInterface', $this->resolver->getTheme('test', 'html'));
44+
}
45+
46+
public function testGetDefaultThemeWhenDoesNotExist()
47+
{
48+
$this->resolver->addTheme($this->theme, 'theme_test');
49+
$this->resolver->addTheme($this->theme, 'theme_test_default', true);
50+
51+
$this->assertInstanceOf('Ladybug\Theme\ThemeInterface', $this->resolver->getTheme('notfound', HtmlFormat::FORMAT_NAME));
52+
}
53+
54+
55+
}

0 commit comments

Comments
 (0)