Skip to content

Instantly share code, notes, and snippets.

@ohvitorino
Last active September 12, 2020 07:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ohvitorino/0c6c9f860122d4fabb352c59b888b4af to your computer and use it in GitHub Desktop.
Save ohvitorino/0c6c9f860122d4fabb352c59b888b4af to your computer and use it in GitHub Desktop.

Symfony 4 - cookbook

Initial installation

composer create-project symfony/website-skeleton ~/Sites/client/project

composer req encore

Add to config/services.yaml

parameters:
    locale: 'nl'
    locales:
        - '%locale%'
    mailer.default_sender_name: '%env(resolve:MAILER_DEFAULT_SENDER_NAME)%'
    mailer.default_sender_email: '%env(resolve:MAILER_DEFAULT_SENDER_EMAIL)%'
    mailer.default_to_name: '%env(resolve:MAILER_DEFAULT_TO_NAME)%'
    mailer.default_to_email: '%env(resolve:MAILER_DEFAULT_TO_EMAIL)%'
    mailer.default_reply_to_name: '%mailer.default_sender_name%'
    mailer.default_reply_to_email: '%mailer.default_sender_email%'
    fallbacks:
        site_title: '%env(resolve:SITE_TITLE)%'

Create a config/packages/jms_i18n_routing.yaml file with the following content

jms_i18n_routing:
    default_locale: '%locale%'
    locales: '%locales%'
    strategy: prefix_except_default

Add to config/packages/twig.yaml file

twig:
    ...
    globals:
        fallbacks: "@framework.fallbacks"
        jsData: "@framework.jsdata"
        locales: "%locales%"

    form_themes:
        - "bootstrap_4_layout.html.twig"
        - "@SumoCodersFrameworkCore/Form/fields.html.twig"

Do not forget to add the env variables to your .env file

SITE_TITLE=<my kickass app>
MAILER_DEFAULT_SENDER_NAME=<my kickass sender name>
MAILER_DEFAULT_SENDER_EMAIL=<my kickass sender name>
MAILER_DEFAULT_TO_NAME=<my kickass to name>
MAILER_DEFAULT_TO_EMAIL=<my kickass to email>

composer req sumocoders/framework-core-bundle

Install sentry

composer req sentry/sentry-symfony:^3.0

Install the apache pack for a nice .htaccess

composer require symfony/apache-pack --dev

Add framework style package

npm add git+ssh://git@github.com/sumocoders/FrameworkStylePackage.git --save-dev

npm install

Add sass loader

npm install sass-loader@^7.0.1 node-sass --save-dev

Edit the base template generated by the Symfony skeleton at templates/base.html.twig (if it doesn't exist create a new file) so that it looks like this:

{% extends '@SumoCodersFrameworkCore/base.html.twig' %}

{% block stylesheets %}
  {{ parent() }}
  {# 'app' must match the first argument to addEntry() in webpack.config.js #}
  {{ encore_entry_link_tags('app') }}

  <!-- Renders a link tag (if your module requires any CSS)
       <link rel="stylesheet" href="/build/app.css"> -->
{% endblock %}

{% block javascripts %}
  {{ parent() }}
  {{ encore_entry_script_tags('app') }}

  <!-- Renders app.js & a webpack runtime.js file
      <script src="/build/runtime.js"></script>
      <script src="/build/app.js"></script> -->
{% endblock %}

This is the template which your page templates should extend from.

Your initial assets/js/app.js should look like this

// any CSS you require will output into a single css file (app.css in this case)
import '../css/app.scss';

// load js vendors

import { Framework } from 'frameworkstylepackage/src/js/Index';

// Initialization
new Framework();

And your assets/css/app.scss should include the frameworkstylepackage

@import '~frameworkstylepackage/src/sass/style';

Make sure you include app.js in your webpack configuration.

Environment

Create a .php-version file in the root of your project with the desired PHP version of your project.

Example:

7.2

Create a docker-compose.yml file for your MySQL database container:

version: '2'
services:
  mysql:
    environment:
      MYSQL_ROOT_PASSWORD: root
    image: mysql:5.7
    ports:
      - 3306:3306
    restart: always
    volumes:
      - "./docker/data/mysql:/var/lib/mysql"

Create php.ini file with the following content in the root of your project to enable xdebug

xdebug.remote_enable = 1

Create .editorconfig

#
# @see http://editorconfig.org/
#

root = true

[*.{js,php,py,yml}]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.js]
indent_size = 2

[*.{html,rb,tpl,twig,xml}]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

#
# Following languages aren't used at the moment in Fork CMS, but just in case.
# (for instance: Capistrano is used in a lot of projects for deployment)
#
# [*.{py,rb}]

Create .gitlab-ci.yml and but first some extra package needed for the CI

composer require --dev mglaman/phpstan-junit

composer require --dev sensiolabs/security-checker

composer require --dev tijsverkoyen/convert-to-junit-xml

stages:
    - code quality
    - dependency scanning
    - build
    - test
    - deploy


# Code Quality section
Check code standards with PHP_CodeSniffer:
    image: sumocoders/framework-php72:latest
    before_script:
        - curl -sS https://getcomposer.org/installer | php
        - php composer.phar install --no-scripts --quiet --ignore-platform-reqs
    script:
        - vendor/bin/phpcs --report-full --report-junit=phpcs-report.xml
    artifacts:
        expire_in: 1 week
        reports:
            junit: phpcs-report.xml
    stage: code quality
    tags:
        - docker
    allow_failure: true

Check for bugs with PHPStan:
    image: sumocoders/framework-php72:latest
    before_script:
        - curl -sS https://getcomposer.org/installer | php
        - php composer.phar install --no-scripts --quiet --ignore-platform-reqs
        - bin/console cache:warmup --env=dev
    script:
        - vendor/bin/phpstan analyse --memory-limit=512M --error-format=junit --no-progress > phpstan-report.xml
    artifacts:
        expire_in: 1 week
        reports:
            junit: phpstan-report.xml
    stage: code quality
    tags:
        - docker
    allow_failure: true


## Dependency Scanning section
Check NPM dependencies for vulnerabilities:
    image: sumocoders/cli-tools-php72:latest
    before_script:
        - curl -sS https://getcomposer.org/installer | php
        - php composer.phar install --no-scripts --quiet --ignore-platform-reqs
    script:
        - vendor/bin/convert-to-junit-xml convert:npm-audit "$(npm audit --json)" > npm-audit-report.xml
    artifacts:
        expire_in: 1 week
        reports:
            junit: npm-audit-report.xml
    stage: dependency scanning
    tags:
        - docker
    allow_failure: true

Check Composer dependencies for vulnerabilities:
    image: sumocoders/cli-tools-php72:latest
    before_script:
        - curl -sS https://getcomposer.org/installer | php
        - php composer.phar install --no-scripts --quiet --ignore-platform-reqs
    script:
        - vendor/bin/convert-to-junit-xml convert:sensiolabs-security-check "$(vendor/bin/security-checker security:check --format=json)" > security-checker-report.xml
    artifacts:
        expire_in: 1 week
        reports:
            junit: security-checker-report.xml
    stage: dependency scanning
    tags:
        - docker
    allow_failure: true


## Build section
Build assets with Encore:
    image: node:11
    before_script:
        - npm install
    script:
        - npm run build
    cache:
        paths:
            - node_modules
            - public/build
    stage: build
    tags:
        - docker


## Test section
Run test with PHPUnit:
    image: sumocoders/framework-php72:latest
    before_script:
        - curl -sS https://getcomposer.org/installer | php
        - php composer.phar install --no-scripts --quiet --ignore-platform-reqs
    script:
        - vendor/bin/simple-phpunit --log-junit phpunit-report.xml
    artifacts:
        reports:
            junit: phpunit-report.xml
    cache:
        paths:
            - public/build
        policy: pull
    stage: test
    tags:
        - docker


## Deploy section
#Deploy to staging:
#    image: sumocoders/cli-tools-php72:latest
#    before_script:
#        # Add the private SSH key to the CI environment
#        - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
#        - eval $(ssh-agent -s)
#        - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
#        - mkdir -p ~/.ssh
#        - chmod 700 ~/.ssh
#        - echo "$SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts
#        - chmod 644 ~/.ssh/known_hosts
#        # Install composer binary and install the vendors
#        - curl -sS https://getcomposer.org/installer | php
#        - php composer.phar install --no-scripts --quiet --ignore-platform-reqs
#        # Install NPM dependencies
#        - npm install
#    script:
#        - vendor/bin/dep deploy staging
#    environment:
#        name: staging
#        url: https://logger.tijs.php72.sumocoders.eu
#    only:
#        - staging
#    stage: deploy
#    tags:
#        - docker

Install PHP code sniffer composer req squizlabs/php_codesniffer --dev

Create a phpcs.xml.dist file with the following content.

<?xml version="1.0" encoding="UTF-8"?>

<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd">

    <arg name="basepath" value="."/>
    <arg name="cache" value=".phpcs-cache"/>
    <arg name="colors"/>
    <arg name="extensions" value="php"/>

    <rule ref="PSR2"/>

    <file>src/</file>
    <file>tests/</file>

</ruleset>

Install PHPStan

composer req phpstan/phpstan-symfony --dev

Create phpstan.neon

includes:
  - vendor/phpstan/phpstan-symfony/extension.neon
services:
  errorFormatter.junit:
    class: PHPStan\Command\ErrorFormatter\JUnitErrorFormatter
parameters:
  symfony:
    container_xml_path: %rootDir%/../../../var/cache/dev/srcApp_KernelDevDebugContainer.xml
  level: 7
  paths:
    - public/
    - src/
  excludes_analyse:
    - %rootDir%/../../../src/Migrations

Install deployer

composer req --dev tijsverkoyen/deployer-sumo

Add a deployment script to your project deploy.php and change the needed variables according to your project.

<?php

namespace Deployer;

require 'recipe/symfony4.php';
require 'recipe/cachetool.php';
require __DIR__ . '/vendor/tijsverkoyen/deployer-sumo/sumo.php';

// Define some variables
set('client', 'tijs');
set('project', 'logger');
set('repository', 'git@git.sumocoders.be:tijs/logger.git');
set('production_url', 'https://logger.verkoyen.eu');

// Define staging
host('dev02.sumocoders.eu')
    ->user('sites')
    ->stage('staging')
    ->set('deploy_path', '~/apps/{{client}}/{{project}}')
    ->set('branch', 'staging')
    ->set('bin/php', 'php7.2')
    ->set('cachetool', '/var/run/php_71_fpm_sites.sock')
    ->set('document_root', '~/php72/{{client}}/{{project}}');

/*************************
 * No need to edit below *
 *************************/

set('use_relative_symlink', false);

// Shared files/dirs between deploys
add('shared_files', []);
add('shared_dirs', []);

// Writable dirs by web server
add('writable_dirs', []);

// Disallow stats
set('allow_anonymous_stats', false);

/*****************
 * Task sections *
 *****************/
// Build tasks
task(
    'build:assets:npm',
    function () {
        run('npm run build');
    }
)
    ->desc('Run the build script which will build our needed assets.')
    ->local();

// Upload tasks
task(
    'upload:assets',
    function () {
        upload(__DIR__ . '/public/build', '{{release_path}}/public');
    }
)
    ->desc('Uploads the assets')
    ->addBefore('build:assets:npm');
after('deploy:update_code', 'upload:assets');

/**********************
 * Flow configuration *
 **********************/
// Clear the Opcache
after('deploy:symlink', 'cachetool:clear:opcache');
// Unlock the deploy when it failed
after('deploy:failed', 'deploy:unlock');
// Migrate database before symlink new release.
before('deploy:symlink', 'database:migrate'); // I don't have doctrine, so I don't need migrations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment