2017 © Pedro Peláez
 

symfony-bundle bootcamp-bundle

Symfony2 BootCamp Bundle

image

netgusto/bootcamp-bundle

Symfony2 BootCamp Bundle

  • Tuesday, September 29, 2015
  • by netgusto
  • Repository
  • 1 Watchers
  • 2 Stars
  • 715 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 14 Versions
  • 0 % Grown

The README.md

composer.json:, (*1)

"require": {
    "netgusto/bootcamp-bundle": "1.0.1",
    "brianium/habitat": "v1.0.0",
    "doctrine/migrations": "dev-master",
    "doctrine/doctrine-migrations-bundle": "dev-master",
    "netgusto/parametertouch-bundle": "1.0.1"
},
"extra": {
    "touch-parameters": [
        {
            "src": "app/config/defaults/data.parameters.dist.yml",
            "dest": "data/parameters.yml"
        },
        {
            "src": "app/config/defaults/data.environment.dist.yml",
            "dest": "data/environment.yml"
        }
    ]
}

.gitignore:, (*2)

remove lines:

    /app/config/parameters.yml

add lines:

    /data/*
    !data/.gitkeep

app/AppKernel.php:, (*3)

new Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle(),
new Netgusto\BootCampBundle\NetgustoBootCampBundle(),

app/config/config_bootcamp.yml:, (*4)

imports:
    - { resource: environment.yml }
    - { resource: parameters.yml }
    - { resource: ../../data/environment.yml }
    - { resource: ../../data/parameters.yml }

parameters:

    bootcamp.environment.databaseurl_variablename: DATABASE_URL
    bootcamp.environment.authorizedkeys:
        - DATABASE_URL

    bootcamp.appname: "Application name"
    bootcamp.appversion: "1.0.0"

    bootcamp.init.config.handler: appname.bootcamp.configinithandler
    bootcamp.init.user.handler: appname.bootcamp.userinithandler

services:

app/config/environment.yml:, (*5)

parameters:

    environment.application.defaults:

        # Packaged App environment defaults here
        # To be modified by the app developer only
        # To override these parameters for your running application, see /data/environment.yml

        DATABASE_URL: sqlite://%kernel.root_dir%/../data/database.db?absolute

app/config/defaults/data.environment.dist.yml:, (*6)

parameters:
    environment.user:

        # ####
        # Uncomment and define properly the line below to point your database
        # Valid drivers are: mysql://, mssql://, postgres:// and sqlite://
        # ####

        # DATABASE_URL: mysql://user:password@hostname/dbname

app/config/defaults/data.parameters.dist.yml:, (*7)

parameters:

    # ####
    # Override application parameters here
    # ####

    # hello: world

app/DoctrineMigrations/Version20140731100000.php, (*8)

<?php

namespace Application\Migrations;

use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;

class Version20140731100000 extends AbstractMigration {

    public function up(Schema $schema) {

        #######################################################################
        # Netgusto\BootCampBundle\Entity\BootCampStatus
        #######################################################################

        $bootcampstatus = $schema->createTable('BootCampStatus');

        $bootcampstatus->addColumn('id', 'integer')->setAutoincrement(true);

        $bootcampstatus->addColumn('configuredversion', 'string', array(
            'length' => 32,
        ));

        $bootcampstatus->setPrimaryKey(array('id'));

        #######################################################################
        # Netgusto\BootCampBundle\Entity\ConfigContainer
        #######################################################################

        $configcontainer = $schema->createTable('ConfigContainer');

        $configcontainer->addColumn('id', 'integer')->setAutoincrement(true);

        $configcontainer->addColumn('name', 'string', array(
            'length' => 255,
        ));

        $configcontainer->addColumn('config', 'json_array');

        $configcontainer->setPrimaryKey(array('id'));
    }

    public function down(Schema $schema) {

    }
}

app/DoctrineMigrations/, (*9)

Add an entity ConfigContainer in a bundle of your project (ex: AppName/ModelBundle), (*10)

AppName/ModelBundle/Entity/ConfigContainer.php, (*11)

<?php

namespace AppName\ModelBundle\Entity;

class ConfigContainer {
    /**
     * @var integer
     */
    private $id;

    /**
     * @var string
     */
    private $name;

    /**
     * @var array
     */
    private $config;

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return HierarchicalConfig
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set config
     *
     * @param array $config
     * @return HierarchicalConfig
     */
    public function setConfig(array $config)
    {
        $this->config = $config;

        return $this;
    }

    /**
     * Get config
     *
     * @return array 
     */
    public function getConfig()
    {
        return $this->config;
    }

    public function has($prop) {
        return array_key_exists($prop, $this->config);
    }

    public function get($prop) {

        if(!$this->has($prop)) {
            throw new \RuntimeException('ConfigContainer: attempt to access undefined config property "' . $prop . '"');
        }

        return $this->config[$prop];
    }

    public function set($prop, $value) {
        $this->config[$prop] = $value;

        return $this;
    }
}

Create a bundle dedicated to initialization (ex: AppName/BootCampBundle) This bundle is not loaded in AppKernel.php, and is used only during initialization, (*12)

AppName/BootCampBundle/Resources/config/services.yml, (*13)

# These services will be available only during composer install

imports:
    - { resource: ../../../../../app/config/parameters.yml }

services:

    appname.bootcamp.configinithandler:
        class: AppName\BootCampBundle\InitHandler\ConfigInitHandler
        arguments:
            - @doctrine.orm.entity_manager

    appname.bootcamp.userinithandler:
        class: AppName\BootCampBundle\InitHandler\UserInitHandler
        arguments:
            - @doctrine.orm.entity_manager
            - @security.encoder_factory

    security.encoder_factory:
        class: Symfony\Component\Security\Core\Encoder\EncoderFactory
        public: false
        arguments:
            - []

AppName/BootCampBundle/InitHandler/ConfigInitHandler.php, (*14)

<?php

namespace AppName\BootCampBundle\InitHandler;

use Doctrine\ORM\EntityManager;

use Netgusto\BootCampBundle\InitHandler\ConfigInitHandlerInterface;

use AppName\ModelBundle\Entity\ConfigContainer;

class ConfigInitHandler implements ConfigInitHandlerInterface {

    protected $entityManager;

    public function __construct(EntityManager $entityManager) {
        $this->entityManager = $entityManager;
    }

    public function createAndPersistConfig() {

        $siteconfig = new ConfigContainer();
        $siteconfig->setName('main');
        $siteconfig->setConfig(array(
            'somevar' => 'Initialization value for somevar',
            'someothervar' => true,
        ));

        $this->entityManager->persist($siteconfig);
        $this->entityManager->flush();
    }
}

AppName/BootCampBundle/InitHandler/UserInitHandler.php, (*15)

<?php

namespace AppName\BootCampBundle\InitHandler;

use Doctrine\ORM\EntityManager;

use Netgusto\BootCampBundle\InitHandler\UserInitHandlerInterface;

use AppName\ModelBundle\Entity\User;

class UserInitHandler implements UserInitHandlerInterface {

    protected $entityManager;
    protected $passwordencoder_factory;

    public function __construct(
        EntityManager $entityManager,
        $passwordencoder_factory
    ) {
        $this->entityManager = $entityManager;
        $this->passwordencoder_factory = $passwordencoder_factory;
    }

    public function createAndPersistUser($username, $password) {

        # Persisting user
        $user = new User();
        $user->setUsername($username);
        $user->setSalt(md5(rand()));
        $user->setPassword(
            $this->passwordencoder_factory->getEncoder($user)->encodePassword(
                $password,
                $user->getSalt()
            )
        );

        $user->addRole('ROLE_ADMIN');

        $this->entityManager->persist($user);
        $this->entityManager->flush();

        return $user;
    }
}

app/BootCampKernel.php, (*16)

<?php

use Netgusto\BootCampBundle\Kernel\BootCampKernel as BaseBootCampKernel;

class BootCampKernel extends BaseBootCampKernel {

    public function registerBundles() {

        $bundles = parent::registerBundles();
        $bundles[] = new AppName\BootCampBundle\AppNameBootCampBundle();
        $bundles[] = new AppName\ModelBundle\AppNameModelBundle();

        return $bundles;
    }
}

app/config.yml, (*17)

imports:
    # BootCamp
    - { resource: config_bootcamp.yml }
    - { resource: @NetgustoBootCampBundle/ParameterProcessor/Environment.php }
    - { resource: @NetgustoBootCampBundle/ParameterProcessor/Database.php }
    # /BootCamp

    - { resource: security.yml }

doctrine:
    dbal:
        # [...]
        path:     "%database_path%"

The Versions

29/09 2015
29/09 2015
29/09 2015
29/09 2015
11/03 2015
11/03 2015
11/03 2015
11/03 2015
11/03 2015
11/03 2015
04/03 2015
02/09 2014