Friday, February 11, 2011

How to free Symfony2 from the sandbox tutorial (with some help from Git)

Note

It's possible that this tutorial works no more, because of changes made to Symfony2 after release PR6.

----
The Symfony2 sandbox application is a great base to start digging into writing Symfony2 apps. When I tried to get my first overview of how to build an app with Symfony2 I just downloaded the symfony2-sandbox from https://github.com/symfony/symfony-sandbox/archives/master and extracted it somewhere onto my development machine, lets assume our folder is sven@dev:~/sandbox$

Prerequisites:
  1. Git installed, check

Hello Git, how are you :)

Create a new local git repository with git init:
(No need now for a remote repository)
sven@dev:~/sandbox$ git init

Goodbye vendors, Hello vendors!

SVN had svn-externals, Git has submodules, so we can make our lives a little bit easier and make use of git submodules to include all vendors as independent git repositories, called submodules. To achieve this we have to execute the command git submodule add <repository-url> <checkout-directory>.
Just execute the following list line by line on your command line:

sven@dev:~/sandbox$
git submodule add git://github.com/symfony/symfony.git           vendor/symfony
git submodule add git://github.com/fabpot/Twig.git               vendor/twig
git submodule add git://github.com/fabpot/Twig-extensions.git    vendor/twig-extensions
git submodule add git://github.com/doctrine/common.git           vendor/doctrine-common
git submodule add git://github.com/doctrine/doctrine2.git        vendor/doctrine
git submodule add git://github.com/doctrine/dbal.git             vendor/doctrine-dbal
git submodule add git://github.com/doctrine/data-fixtures.git    vendor/doctrine-data-fixtures
git submodule add git://github.com/doctrine/migrations.git       vendor/doctrine-migrations
git submodule add git://github.com/doctrine/mongodb.git          vendor/doctrine-mongodb
git submodule add git://github.com/doctrine/mongodb-odm.git      vendor/doctrine-mongodb-odm
git submodule add git://github.com/swiftmailer/swiftmailer.git   vendor/swiftmailer
git submodule add git://github.com/zendframework/zf2.git         vendor/zend

Now you have included every dependency to a vendor as a seperate git repository, now just call git submodule update --init to update everything.
sven@dev:~/sandbox$ git submodule update --init

You like it slow?

Symfony2 has a bootstrap file under ~/sandbox/vendor/symfony/src/Symfony/Component/HttpKernel/bootstrap.php which includes the whole framework for maximum performance, so it never loads the real classes under vendor/symfony/src/Symfony, for debugging and testing it can be useful to include the real classes and not the bootstrap file, for this you need to change two files:

1: sven@dev:~/sandbox/app/bootstrap.php

Comment out the line:
require_once __DIR__.'/../vendor/symfony/src/Symfony/Component/HttpKernel/bootstrap.php';

2: sven@dev:~/sandbox/app/autoload.php

Add at the beginning of the file the following line:
require_once(__DIR__ . '/../vendor/symfony/src/Symfony/Component/ClassLoader/UniversalClassLoader.php');

More to delete, just to be up to date

Delete the contents of sven@dev:~/sandbox/bin$ and create a new folder called ant-tasks and a new file called update-vendors.xml. Paste the following content into the file:

<project name="update-vendors" basedir="./../../">
    
    <target name="update-doctrine">
        <exec dir="${basedir}/vendor/doctrine" executable="git" failonerror="true">
            <arg line="pull" />
        </exec>
    </target>
    
    <target name="update-doctrine-common">
        <exec dir="${basedir}/vendor/doctrine-common" executable="git" failonerror="true">
            <arg line="pull" />
        </exec>
    </target>
    
    <target name="update-doctrine-dbal">
        <exec dir="${basedir}/vendor/doctrine-dbal" executable="git" failonerror="true">
            <arg line="pull" />
        </exec>
    </target>
    
    <target name="update-doctrine-data-fixtures">
        <exec dir="${basedir}/vendor/doctrine-data-fixtures" executable="git" failonerror="true">
            <arg line="pull" />
        </exec>
    </target>
    
    <target name="update-doctrine-migrations">
        <exec dir="${basedir}/vendor/doctrine-migrations" executable="git" failonerror="true">
            <arg line="pull" />
        </exec>
    </target>
    
    <target name="update-doctrine-mongodb">
        <exec dir="${basedir}/vendor/doctrine-mongodb" executable="git" failonerror="true">
            <arg line="pull" />
        </exec>
    </target>
    
    <target name="update-doctrine-mongodb-odm">
        <exec dir="${basedir}/vendor/doctrine-mongodb-odm" executable="git" failonerror="true">
            <arg line="pull" />
        </exec>
    </target>
    
    <target name="update-symfony">
        <exec dir="${basedir}/vendor/symfony" executable="git" failonerror="true">
            <arg line="pull" />
        </exec>
    </target>
    
    <target name="update-twig">
        <exec dir="${basedir}/vendor/twig" executable="git" failonerror="true">
            <arg line="pull" />
        </exec>
    </target>
    
    <target name="update-twig-extensions">
        <exec dir="${basedir}/vendor/twig-extensions" executable="git" failonerror="true">
            <arg line="pull" />
        </exec>
    </target>
    
    <target name="update-swiftmailer">
        <exec dir="${basedir}/vendor/swiftmailer" executable="git" failonerror="true">
            <arg line="pull" />
        </exec>
    </target>
    
    <target name="update-zend">
        <exec dir="${basedir}/vendor/zend" executable="git" failonerror="true">
            <arg line="pull" />
        </exec>
    </target>
</project> 

This file is an ant tasks file, you can execute the tasks described in there with ant to update your vendors.

Done

Now make your sandbox folder accessible by your webserver and navigate to the web folder and open the app.php or app_dev.php file in your browser, Symfony2 should running without any problems.

Another tip about git submodules

Sometimes it's a bit dangerous to get the most bleeding edge checkouts from the vendors, so you can update the sources/submodules and after that, navigate into the checkout folder and switch to a tag with git checkout <tag_name>.

2 comments:

  1. The way sandbox mannage bootstrap has changed, now full bootstrap is in app/bootsrap.php. Could you please reflect those changes in your post.
    Thanks a lot for your work!!

    ReplyDelete
  2. @amettema I'll have a look. Thanks for the hint :)

    ReplyDelete