Skip to content

Commit

Permalink
Add ci, static analysis and code style
Browse files Browse the repository at this point in the history
  • Loading branch information
WebMamba committed Jan 8, 2024
1 parent 473ece2 commit f35947a
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 49 deletions.
50 changes: 50 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: CI

on:
pull_request: ~
push:
branches: ['main']
schedule:
- cron: '0 */12 * * *'

jobs:
composer-validate:
name: Validate composer.json
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.2
coverage: none
- name: Validate composer.json
run: composer validate --strict
cs:
name: Check coding standards
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.2
tools: php-cs-fixer
- name: Run PHP-CS-Fixer
run: php-cs-fixer fix . --dry-run --diff
sca:
name: PHPStan
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.2
- name: Install composer dependencies
run: composer install --no-progress --prefer-dist --optimize-autoloader
- name: Run PHPStan
run: vendor/bin/phpstan analyse
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@
/.phpunit.result.cache
/phpunit.xml.dist

.php-cs-fixer.cache

4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "sensiolabs/storybook-bundle",
"type": "library",
"license": "MIT",
"description": "Symfony bundle to generate a static storybook",
"authors": [
{
"name": "Nicolas Rigaud",
Expand All @@ -27,6 +28,7 @@
"symfony/framework-bundle": "^7.0",
"symfony/twig-bundle": "^7.0",
"symfony/phpunit-bridge": "^7.0",
"symfony/ux-twig-component": "^2.13"
"symfony/ux-twig-component": "^2.13",
"phpstan/phpstan": "^1.10"
}
}
42 changes: 21 additions & 21 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@
use Storybook\Twig\TemplateLocator;

return static function (ContainerConfigurator $container) {
$container->services()
->set('storybook.controller.render_story', StorybookController::class)
$container->services()
->set('storybook.controller.render_story', StorybookController::class)
->args([
service('twig'),
service('storybook.loader'),
service('storybook.twig.template_locator')
])
->tag('controller.service_arguments')
->set('storybook.listener.cors', CorsListener::class)
->args([
service('request_stack'),
abstract_arg('storybook dev server host'),
])
->tag('kernel.event_listener')
->set('storybook.loader', StorybookLoader::class)
->set('storybook.twig.template_locator', TemplateLocator::class)
->args([
service('twig'),
service('storybook.loader'),
service('storybook.twig.template_locator')
param('kernel.project_dir')
])
->tag('controller.service_arguments')
->set('storybook.listener.cors', CorsListener::class)
->set('storybook.init_command', InitCommand::class)
->args([
service('request_stack'),
abstract_arg('storybook dev server host'),
service('twig'),
])
->tag('kernel.event_listener')
->set('storybook.loader', StorybookLoader::class)
->set('storybook.twig.template_locator', TemplateLocator::class)
->args([
param('kernel.project_dir')
])
->set('storybook.init_command', InitCommand::class)
->args([
service('twig'),
])
->tag('console.command', ['name' => 'storybook:init'])
;
->tag('console.command', ['name' => 'storybook:init'])
;
};
14 changes: 14 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
parameters:
level: 4
paths:
- src
- tests
ignoreErrors:
-
message: '#^Call to an undefined method Symfony\\Component\\Config\\Definition\\Builder\\NodeParentInterface\:\:end\(\)\.$#'
count: 1
path: src/StorybookBundle.php
-
message: '#^Call to an undefined method Symfony\\Component\\Config\\Definition\\Builder\\NodeParentInterface\:\:scalarNode\(\)\.$#'
count: 1
path: src/DependencyInjection/StorybookExtension.php
27 changes: 13 additions & 14 deletions src/Command/InitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Storybook\Command;


use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -15,23 +14,23 @@
#[AsCommand(name: 'storybook:init', description: 'Some desc')]
class InitCommand extends Command
{
public function __construct(private readonly Environment $twig, private readonly string $runtimeDir, private readonly string $previewTemplate)
{
parent::__construct();
}
public function __construct(private readonly Environment $twig, private readonly string $runtimeDir, private readonly string $previewTemplate)
{
parent::__construct();
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);

$content = $this->twig->render($this->previewTemplate);
$content = $this->twig->render($this->previewTemplate);

$fs = new Filesystem();
$fs = new Filesystem();

$fs->dumpFile(Path::join($this->runtimeDir, 'preview', 'preview.ejs'), $content);
$fs->dumpFile(Path::join($this->runtimeDir, 'preview', 'preview.ejs'), $content);

$io->success('Content generated');
$io->success('Content generated');

return self::SUCCESS;
}
return self::SUCCESS;
}
}
6 changes: 5 additions & 1 deletion src/DependencyInjection/StorybookExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Twig\Loader\FilesystemLoader;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;

class StorybookExtension extends Extension implements ConfigurationInterface
{
Expand Down Expand Up @@ -45,7 +46,10 @@ public function load(array $configs, ContainerBuilder $container): void
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('storybook');
$treeBuilder->getRootNode()
$rootNode = $treeBuilder->getRootNode();
assert($rootNode instanceof ArrayNodeDefinition);

$rootNode
->children()
->scalarNode('server')
->info('The URL of the Storybook server. Pass null to disable the CORS headers.')
Expand Down
2 changes: 1 addition & 1 deletion src/EventListener/CorsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ public function __invoke(ResponseEvent $event): void

$event->getResponse()->headers->set('Access-Control-Allow-Origin', $this->host);
}
}
}
2 changes: 1 addition & 1 deletion src/Exception/RenderException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ public function __construct(string $message = "", ?\Throwable $previous = null)
{
parent::__construct($message, 0, $previous);
}
}
}
6 changes: 5 additions & 1 deletion src/StorybookBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;

class StorybookBundle extends Bundle implements ConfigurationInterface
{
Expand Down Expand Up @@ -40,7 +41,10 @@ public function getPath(): string
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('storybook');
$treeBuilder->getRootNode()
$rootNode = $treeBuilder->getRootNode();
assert($rootNode instanceof ArrayNodeDefinition);

$rootNode
->children()
->scalarNode('server')
->info('The URL of the Storybook server. Pass null to disable the CORS headers.')
Expand Down
12 changes: 6 additions & 6 deletions src/Twig/TemplateLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ public function __construct(private readonly string $projectDir)
}

public function locateTemplate(string $id): string
{
// TODO: cache this
$file = \file_get_contents(Path::join($this->projectDir, 'var/storybook/stories/storiesMap.json'));
$map = json_decode($file, true);
{
// TODO: cache this
$file = \file_get_contents(Path::join($this->projectDir, 'var/storybook/stories/storiesMap.json'));
$map = json_decode($file, true);

return \sprintf('@Stories/%s.html.twig', $map[$id]);
}
return \sprintf('@Stories/%s.html.twig', $map[$id]);
}
}
5 changes: 2 additions & 3 deletions tests/Fixtures/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,9 @@ protected function configureContainer(ContainerConfigurator $container): void
'secrets' => false,
'http_method_override' => false,
'php_errors' => ['log' => true],
'handle_all_throwables' => true,
];
if (self::VERSION_ID >= 60200) {
$frameworkConfig['handle_all_throwables'] = true;
}

$container->extension('framework', $frameworkConfig);

$container->extension('twig', [
Expand Down

0 comments on commit f35947a

Please sign in to comment.