開発メモ

開発用のメモです。

Silex / DB 接続 その2

  • TestApp/Application.php
  • TestApp/Controller/Provider/HomeControllerProvider.php
  • TestApp/Controller/HomeController.php
  • TestApp/Entity/Display.php
  • TestApp/Templates/index.twig

TestApp/Application.php

<?php

namespace TestApp;

    use TestApp\Controller\Provider\HomeControllerProvider;
    use Symfony\Component\Yaml\Yaml;
    use Silex\Provider\ValidatorServiceProvider;
    use Silex\Provider\FormServiceProvider;
    use Silex\Provider\DoctrineServiceProvider;
    use Doctrine\ORM\Tools\Setup;
    use Doctrine\ORM\EntityManager;

    class Application extends \Silex\Application {

        /**
        * @var \TestApp\Application
        */
        var $app;

        public function __construct( ) {
            parent::__construct( );

            $app = &$this;

            // load config
            $app[ 'config' ] = $app->share( function( ) {
                $config = Yaml::parse( dirname( $_SERVER[ "CONTEXT_DOCUMENT_ROOT" ] ) . '/config/config.yml' );

                function change( &$config ) {
                    // ループを再帰的に行い、 文字列 : __DIR__ を 定数 dirname( __FILE__ ) へ 展開する
                    foreach( $config as $keyname => $value ) {
                        if( empty( $config[ $keyname ] ) ) {
                        } elseif( is_numeric( $config[ $keyname ] ) ) {
                        } elseif( is_bool( $config[ $keyname ] ) ) {
                        } elseif( is_array( $config[ $keyname ] ) ) {
                            change( $config[ $keyname ] );
                        } elseif( is_string( $config[ $keyname ] ) && strpos( '__DIR__', $config[ $keyname ] ) == 0 ) {
                            $config[ $keyname ] = str_replace( '\\', '/', str_replace( '__DIR__', dirname( __FILE__ ), $value ) );
                        }
                    }
                }

                change( $config );
                return $config;
            } );

            // twig 対応 start
            $app->register( new \Silex\Provider\TwigServiceProvider( ), array(
            // @formatter:off 
                'debug'     => $app[ 'config' ]['debug'], 
                'twig.path' => $app[ 'config' ]['twig.path']
            // @formatter:on
            ) );
            // twig 対応 end

            // form configuret start
            $app->register( new FormServiceProvider( ) );
            $app->register( new ValidatorServiceProvider( ) );
            // form configuret end

            // database configuret start
            $app->register( new DoctrineServiceProvider( ), array( 'db.options' => $app[ 'config' ][ 'database' ], ) );
            $app[ "orm.em" ] = $app->share( function( ) use ( $app ) {
                $entitiesPath = array( dirname( __FILE__ ) . DIRECTORY_SEPARATOR . "Entiry" );
                $config = Setup::createAnnotationMetadataConfiguration( $entitiesPath, false );
                $em = EntityManager::create( $app[ 'config' ][ 'database' ], $config );
                return $em;
            } );
            // database configuret end

            # /*
            $app->mount( '', new HomeControllerProvider( ) );

        }

    }

TestApp/Controller/HomeController.php

<?php

namespace TestApp\Controller\Provider;

    use Silex\Application;
    use Silex\ControllerProviderInterface;

    class HomeControllerProvider implements ControllerProviderInterface {

        public function connect( Application $app ) {

            $controllers = $app[ 'controllers_factory' ];
            $controllers->match( '/', "\\TestApp\\Controller\\HomeController::index" )->bind( 'index' );

            return $controllers;
        }

    }

TestApp/Controller/Provider/HomeControllerProvider.php

<?php

namespace TestApp\Controller;

    use TestApp\Application;
    use TestApp\Entity\Display;

    class HomeController {
        public function index( Application $app ) {

            $config = $app[ "config" ];
            $twig = $app[ 'twig' ];

            $em = $app[ 'orm.em' ];

            $entity = new Display( );
            
            $entity->setId( 1 );
            $entity->setName( date( "Y-m-d" ) );
            $entity->setRank( 1 ) );

            $em->persist( $entity );
            $em->flush( );

            return $app[ 'twig' ]->render( 'index.twig', compact( "app", "config", "twig", "em" ) );
        }

    }

TestApp/Entity/Display.php

<?php

namespace TestApp\Entity;

    /**
    * @Entity @Table(name="mtb_display")
    */
    class Display {

        function __constractor( ) {

        }

        /**
        * @var int
        * @Id @Column(type="integer")
        */
        protected $id;

        /**
        * @var string
        * @Column(type="string")
        */
        protected $name;

        /**
        * @var int
        * @Column(type="integer")
        */
        protected $rank;

        public function getId( ) {
            return $this->id;
        }

        public function setId( $id ) {
            $this->id = $id;
        }

        public function getName( ) {
            return $this->name;
        }

        public function setName( $name ) {
            $this->name = $name;
        }

        public function getRank( ) {
            return $this->rank;
        }

        public function setRank( $rank ) {
            $this->rank = $rank;
        }

    }
Twitter: @asahina_alice