Skip to content

Commit

Permalink
Breakpoint configuration (#18)
Browse files Browse the repository at this point in the history
* add breakpoint config
  • Loading branch information
davidhoelzel authored Apr 3, 2024
1 parent e32be54 commit 7c8dff7
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 2 deletions.
17 changes: 17 additions & 0 deletions docs/BundleConfiguration.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ twig_doc:
- '%twig.default_path%/components'
categories:
- name: Components
breakpoints:
small: 240
medium: 640
large: 768
```
### Directories
Expand Down Expand Up @@ -66,3 +70,16 @@ twig_doc:
```

The default category is always merged into the configuration.


### Breakpoints

To use custom breakpoints, simply provide a breakpoint-config. You can name the breakpoints as you like:

```yaml
twig_doc:
breakpoints:
iphone: 598
unusual: 743
...
```
9 changes: 9 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ public function getConfigTreeBuilder(): TreeBuilder
->thenInvalid('The twig_doc documentation identifier must match \w (regex)')
->end()
->end()
->arrayNode('breakpoints')
->defaultValue([
'small' => 240,
'medium' => 640,
'large' => 768,
])
->integerPrototype()
->end()
->end()
->arrayNode('directories')->defaultValue(['%twig.default_path%/components'])
->scalarPrototype()->end()
->end()
Expand Down
1 change: 1 addition & 0 deletions src/DependencyInjection/TwigDocExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function load(array $configs, ContainerBuilder $container): void

$definition = $container->getDefinition('twig_doc.service.component');
$definition->setArgument('$componentsConfig', $config['components']);
$definition->setArgument('$breakpointConfig', $config['components']);
$definition->setArgument('$configReadTime', time());

$categories = array_merge([['name' => ComponentCategory::DEFAULT_CATEGORY]], $config['categories']);
Expand Down
6 changes: 6 additions & 0 deletions src/Service/ComponentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function __construct(
private readonly ComponentItemFactory $itemFactory,
private readonly array $componentsConfig,
private readonly CacheInterface $cache,
private readonly array $breakpointConfig,
private readonly int $configReadTime = 0
) {
}
Expand Down Expand Up @@ -95,4 +96,9 @@ public function getComponent(string $name): ?ComponentItem
{
return array_values(array_filter((array) $this->getComponents(), fn (ComponentItem $c) => $c->getName() === $name))[0] ?? null;
}

public function getBreakpoints(): array
{
return $this->breakpointConfig;
}
}
7 changes: 6 additions & 1 deletion tests/Functional/Service/ComponentServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ public function testParsePerformance(): void

$start = microtime(true);

$service = new ComponentService($factory, $this->getLargeConfig(), static::getContainer()->get(CacheInterface::class));
$service = new ComponentService(
$factory,
$this->getLargeConfig(),
static::getContainer()->get(CacheInterface::class),
[]
);

$service->getComponents();

Expand Down
35 changes: 34 additions & 1 deletion tests/Unit/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function testConfigTree(array $options, array $expectedResult)
$configuration = new Configuration();
$config = $processor->processConfiguration($configuration, [$options]);

$this->assertEqualsCanonicalizing($expectedResult, $config);
$this->assertEquals($expectedResult, $config);
}

public static function getTestConfiguration(): iterable
Expand All @@ -41,6 +41,11 @@ public static function getTestConfiguration(): iterable
],
[
'doc_identifier' => 'TWIG_DOC',
'breakpoints' => [
'small' => 240,
'medium' => 640,
'large' => 768,
],
'directories' => [
__DIR__.'/../../TestApp/templates/components',
__DIR__.'/../../TestApp/templates/snippets',
Expand Down Expand Up @@ -100,6 +105,11 @@ public static function getTestConfiguration(): iterable
],
[
'doc_identifier' => 'TWIG_DOC',
'breakpoints' => [
'small' => 240,
'medium' => 640,
'large' => 768,
],
'directories' => [
__DIR__.'/../../TestApp/templates/components',
__DIR__.'/../../TestApp/templates/snippets',
Expand Down Expand Up @@ -139,5 +149,28 @@ public static function getTestConfiguration(): iterable
],
],
];

yield 'Breakpoint config' => [
[
'breakpoints' => [
'iphone' => 568,
'galaxy s10' => 658,
'generic' => 896,
],
],
[
'breakpoints' => [
'iphone' => 568,
'galaxy s10' => 658,
'generic' => 896,
],
'doc_identifier' => 'TWIG_DOC',
'directories' => [
'%twig.default_path%/components',
],
'categories' => [],
'components' => [],
],
];
}
}

0 comments on commit 7c8dff7

Please sign in to comment.