xfiveco / generator-chisel

Chisel is a development framework for creating easy to maintain and fast WordPress websites.
https://www.getchisel.co
MIT License
269 stars 37 forks source link

Add Symfony support #128

Closed luboskmetko closed 6 years ago

luboskmetko commented 7 years ago

Adding Symfony support should be relatively easy. We don't have to setup Symfony like WordPress, we could just ensure that correct paths are set and some additional files are created when running Chisel in a Symfony project which was already setup. When doing so there are 2 conflicting files in Symfony installation - .gitignore and README.md, we could merge them with our files.

Below is a working configuration which can serve as a documentation for manual setup in the meanwhile.

Directory structure when Chisel files are added to a Symfony project:

- app
- bin
- gulp
- node_modules
- src
  - AppBundle
- tests
- var
- vendor
- web - publicly available web folder in Symfony
  - src
     - assets
     - scripts
     - styles
  - assets
  - scripts
  - styles

Use of revisioned_path function in Twig templates:

<!-- app/config/Resources/views/base.html.twig -->
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <title>{% block title %}Welcome!{% endblock %}</title>
        {% block stylesheets %}{% endblock %}
        <link rel="icon" type="image/x-icon" href="{{ asset('favicon.ico') }}" />
        <link rel="stylesheet" href="{{ revisioned_path(asset('styles/main.css')) }}">
    </head>
    <body>
        {% block body %}{% endblock %}
        <script src="{{ revisioned_path(asset('scripts/bundle.js')) }}" async></script>
        {% block javascripts %}{% endblock %}
    </body>
</html>

Twig extension for the revisioned_path function:

# app/config/services.yml
parameters:

services:
    app.twig_extension:
        class: AppBundle\Twig\AppExtension
        public: false
        arguments: ['%kernel.root_dir%']
        tags:
            - { name: twig.extension }
<?php
// src/AppBundle/Twig/AppExtension.php
namespace AppBundle\Twig;

class AppExtension extends \Twig_Extension
{

    public function __construct($rootDir)
    {
        $this->webRoot = realpath($rootDir . '/../web');
    }

    public function getFunctions()
    {
        return array(
            new \Twig_SimpleFunction('revisioned_path', array($this, 'revisionedPath')),
        );
    }

    public function revisionedPath($asset)
    {
        $manifestPath = $this->webRoot . '/rev-manifest.json';

        if (!isset($_SERVER['HTTP_X_CHISEL_PROXY']) && file_exists($manifestPath)) {
            $manifest = json_decode(file_get_contents($manifestPath), true);
            $pathinfo = pathinfo($asset);
            if (array_key_exists($pathinfo['basename'], $manifest)) {
                return $pathinfo['dirname'] . '/' .  $manifest[$pathinfo['basename']];
            } else {
                return $asset;
            }
        } else {
            return $asset;
        }
    }

    public function getName()
    {
        return 'app_extension';
    }
}
luboskmetko commented 6 years ago

Closing this one as it's more suitable for a recipe than Chisel feature.