Category Archives: PHP

Symfony2, Assetic, Twitter Bootstrap + Bootswatch

This is a quick’n’dirty guide for newcomers to symfony’s assetic and twitter bootstrap in conjunction with bootswatch. This is not a complete guide and reflects my own opinion only.

Quick terms:

  • Assetic is automated asset management and compilation, in this example it’s used to compile less files
  • less is used to compile a templateish CSS language into real CSS files your browser understands
  • Your advantages:
    • Easy color changes
    • One command to update your css
  • First, install nodejs. 
  • Configure bootstrap and jquery packages:

 

  • Add the following block to your config.yml and change the paths as explained in the comments.

 

  • Create a new build.less file and add the following stuff:

  •  Choose your favourite theme at bootswatch.com and download both .less files per theme. Place them alongside in the same directory where you placed your build.less file.

Now you can run app/console assetic:dump to compile the less file into a css file, which you then can include using the usual twig fragment:

That’s it. If you’re getting any errors, I’m sorry: You need to find out yourself what’s going on. I’ve spend 3 hours to get the initial setup running. Guides I’ve followed or files I’ve looked at:

  • http://bootstrap.braincrafted.com/getting-started (I’m not using the bootstrap bundle, as I don’t currently see the need for it. This might change as soon as I’m using KnpPaginator, KnpMenu and symfony forms. I just left the bundle out because I had trouble getting it to work, the reason was that my nodejs setup was forked up).
  • The build scripts for bootswatch
  • The source for the Assetic Less filter (no, you can’t inject different less imports, hence the build.less encapsulation script above)

 

PHP and date.timezone

Almost everybody has seen the following line:

First, we need to know where this comes from. The reason is simple: The PHP developers don’t trust the system’s timezone settings. Why they don’t trust it – I honestly have no idea. So basically, whenever you set up a new system with PHP, you must set the date.timezone in your php.ini or set the TZ variable – otherwise PHP will spit out that warning each time any PHP script uses date() and related functions.

Some distributions automatically set the timezone; others include a patch which magically guesses the timezone (debian is such a distribution).

If you are an end-user, you are fine with setting date.timezone in your php.ini.

However, if you are a developer who releases PHP apps, you want to make sure that date.timezone is set, in order to help your users to avoid that nasty problem.

However, things aren’t that easy. The warning is triggered when no timezone is set; however, date.timezone isn’t the only location where timezone information can come from.

The timezone can be configured by:

  • date.timezone in your php.ini
  • The TZ environment variable (< PHP 5.4.0)
  • A call to date_default_timezone_set (in the application itself, or by prepending a script)
  • Distribution-specific patches which automatically set the timezone

So, if we want to check if the user has configured the timezone to avoid warnings, checking date.timezone is not enough. Additionally, even if the timezone is set, we don’t know if it is a valid one.

The first thing which came into my mind was: use date_default_timezone_get() to retrieve the timezone, then match it against a list of supported timezones. However, there isn’t a function to retrieve all supported timezones in < PHP 5.2, and I don’t feel manual matching is the way to go.

After quite some experiments and even committing a function which seemed to work in the first place, I realized: There is no good way to actually check if the timezone is set correctly.

So I make things easy and simply test if date.timezone is not empty, and forcefully set the timezone with date_default_timezone_set(). This is not a very good or complete approach, as it virtually skips distribution-related patches and the TZ environment variable.

There’s nothing much we can do; either invent a very fancy method which will probably not work in all environments, or complain to the PHP developers (so that they don’t do the same bullsh$#!! they have done again).