Phing executing multiple commands

Posted March 26th, 2011 in Zanimivosti by Metod

A little tip. If you deploy your PHP projects with Phing, you are familiar with the file build.xml. If you want to execute multiple commands, that depend on each other to be executed, you have to use && in between the commands. For example, clearing the cache:

ssh user@example.com 'cd cache && rm *'

The line in phing build.xml would look like this:


						
						

New project: BoredTODO

Posted February 16th, 2011 in Projects by Metod

I managed to find some free time and finish my quick project – BoredTODO. It is essentially a TODO list which you can use when you get bored, so you have something to do. It takes advantage of some libraries and services:

Some facts:

Doctrine2 DBAL is a database abstraction layer, which makes it easy to work with the database on a smaller projects like this.

It is written in PHP 5.3.

jQuery was used to make all the shortcuts and ajaxy stuff work.

Reset CSS is in my opinion a must tool for every project, since it gives you a nice clean foundation to style on.

Google Fonts API was used for making the Ubuntu font family available in CSS across platforms and browsers.

The app is HTML5, just for fun.

It uses some CSS3 features for making rounded borders and gradients without pictures work. Therefore only few browsers are supported. Of course IE is not one of them.

You can see and try out the demo here

or

You can grab the source code on GitHub.

Requirements

PHP >= 5.3.0

Tested on UNIX system. Not tested on Windows.

Installation

In config/config.php change $connectionParams to your configuration.

$connectionParams = array(
    "dbname"   => "CHANGEME",
    "user"     => "CHANGEME",
    "password" => "CHANGEME",
    "host"     => "localhost",
    "driver"   => "pdo_mysql"
);

and also change the basedomain configuration value.

Config::$basedomain = "http://CHANGEME";

Usage

BoredTODO is all about keyboard shortcuts.

First off you will want to type in a short description of the task at the top. If you wish to add this task without any longer descriptions, press ENTER and the task is inserted.

BoredTODO offers you three priorities – low, medium and high. If you wish to add a priority to your TODO, type one of the keywords “low”, “med” or “high” followed by a whitespace in the short description. Examples:

  • low This is a task with low priority.
  • med This is a task with medium priority.
  • high This is a task with high priority.

But if you would like to add some detailed description to the task, press TAB and the cursor will be focued on a textarea in which you can type the description.

For insertion of the task press TAB again.

Your TODO is ready for usage.

If there is a long description, a pointer cursor will appear when you hover over a TODO item. If you click on it, the description will show up. If you click on it again, it will disappear.

When you want to close the task, you have to click on the most-left side of the TODO – the priority color. When you do that, the TODO is checked (deleted). But if you made a mistake, you can easily undo the action by clicking on the priority color again. The TODO is back.

Browser Support

Since BoredTODO uses some CSS3 features it supports only the latest and non-IE browsers:

  • Firefox
  • Chrome

Untested:

  • IE9
  • Safari

Opera 11 does not seem to have support for CSS gradients, so it is not fully supported. BoredTODO might function on other browsers as well, but was not tested.

Code License

MIT License

Copyright © Metod

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Code snippet 4 – echo and return

Posted February 9th, 2011 in Snippets by Metod

This one is in PHP. I stumbled upon a case, where I wanted to echo something and then return, so that execution of the script would stop. One way to do this was:

echo $sth;
return;

But this was a 2-liner. I wanted to do this in a nice 1-line way. So one option to do this could be:

exit($sth);

Which still feels wrong because in my opinion this is something in the line of the abuse of the exit construct. Besides, since PHP 4.2.0, exit construct does not print out the contents if the contents is actually an integer. So I found a solution to my problem:

return print $sth;

This line does exactly what I wanted. Outputs the contents (even if it is an integer) and breaks current script’s execution.

Porting PHPEclipse syntax coloring to Eclipse PDT

Posted February 1st, 2011 in Razno by Metod

I was pushed into the “eclipse php world” with Eclipse + PHPEclipse setup. Although I knew about Eclipse PDT I never bothered to install it since PHPEclipse worked fine. Up untill I wanted to work with the newest PHP 5.3 features. PHPEclipse just doesn’t support that. So I had to move to PDT. It supports PHP 5.3 and is indeed a better tool. But what bothered me was the syntax coloring which is a bit different from PHPEclipse. So I’m publishing this “migration” post for anyone who has or will have the same problem. You can also use this guide to migrate to Zend Studio, since it is based on Eclipse PDT. Including me at some future time.

Eclipse PDT / Zend Studio:

  • Multi-line comment: #3F7F5F
  • Single-line comment: #3F7F5F
  • PHP tags: #FF0080
  • Keyword: #7F0055
  • Functions: #7F7F9F (no italic)
  • Variable: #7F7F9F
  • Parameter variables: #7F7F9F
  • Fields: #7F7F9F
  • Static fields: #7F7F9F
  • Superglobal variables: #7F7F9F
  • Internal constants: #7F0055
  • Constants: #7F0055
  • String: #2A00FF

Also in Zend Studio, line highlight is not the same as in PHPEclipse or Eclipse PDT.

  • Current line highlight: #E8F2FE

Code snippet 3

Posted January 15th, 2011 in Snippets by Metod

At the moment I am doing some java programming. And since being primarily a php guy, I was wondering how to easily return an array from a method like you can in php:

public function returnArray() {
    return array(1, 2, 3);
}

And I found a way which is quite nice:

public int[] returnArray() {
    return new int[]{1, 2, 3};
}

Access component variables in it’s controller in symfony 1.0

Posted December 16th, 2010 in Snippets by Metod

Today I was wondering how to access variables in a symfony component controller that were passed there through third parameter in a include_component() call. So I found a solution and am posting this as a reminder to me or anyone else who is stuck with symfony 1.0 and is wondering the same thing. You can call this method inside your class someComponents extends sfComponents.

$vars = $this->getVarHolder()->getAll();

Returns an associative array of variables.

Multiple PHP versions with Apache 2, FastCGI, PHPFarm on Ubuntu

Posted December 2nd, 2010 in Tehnikalije by Metod

This tutorial is a part of the “Setting up your local development environment” series. It is based on this article while keeping in mind that in previous apache tutorial we compiled apache from source. So I’m definitely not taking all the credit for it. But there are some modifications.

Starting with PHPFarm, we need Git installed in order to check it out. If you don’t have it installed:

sudo apt-get install git-core

Now create a directory in which PHP versions will reside. The directory of my liking is /usr/local/php, so I will use that. You can of course change that.

cd /usr/local

Clone the phpfarm repository.

git clone git://git.code.sf.net/p/phpfarm/code php

Now we have to edit the configuration options which phpfarm will use to compile php.

cd php/src

We can use more general configuration options or very strict ones. Default configuration options.sh can be overridden by creating a file called custom-options.sh or a bit more specific like custom-options-5.2.sh or very strict like custom-options-5.3.18.sh. Since the configurations for 5.2 and 5.3 are different and I like to be a strict person, I will create two configurations:

  • custom-options-5.2.17.sh
  • custom-options-5.3.18.sh

custom-options-5.2.17.sh contents (these config values were also mentioned in previous tutorial):

configoptions="
	--enable-cli \
	--with-pear \
	--with-openssl=/usr \
	--with-iconv \
	--with-curl \
	--with-mysqli \
	--enable-mbstring \
	--enable-exif \
	--with-jpeg-dir=/usr \
	--with-zlib \
	--with-zlib-dir \
	--with-png-dir=/usr \
	--with-gd \
	--with-gettext \
	--enable-gd-native-ttf \
	--with-mhash \
	--enable-ftp \
	--with-pspell \
	--with-mcrypt \
	--enable-bcmath \
	--with-mime-magic \
	--with-pdo-mysql \
	--enable-sockets \
	--enable-soap \
        --enable-calendar \
	--enable-fastcgi \
	--enable-force-cgi-redirect \
"

custom-options-5.3.18.sh contents:

configoptions="
	--with-mysqli=mysqlnd \
	--with-pdo-mysql=mysqlnd \
	--enable-cli \
	--with-pear \
	--with-openssl=/usr \
	--with-iconv \
	--with-curl \
	--enable-mbstring \
	--enable-exif \
	--with-zlib \
	--with-zlib-dir \
	--with-gd \
	--with-gettext \
	--enable-gd-native-ttf \
	--with-mhash \
	--enable-ftp \
	--with-pspell \
	--with-mcrypt \
	--enable-bcmath \
	--enable-sockets \
	--enable-soap \
	--enable-calendar \
	--with-png-dir=/usr \
	--with-jpeg-dir=/usr
"

For PHP 5.3 we don’t need the cgi parameters, since these are already enabled by default.

On Ubuntu 11.04, the configuration might fail with error messages about libjpeg and/or libpng. What you can do is the following:

sudo apt-get install libjpeg8-dev

Install the libjpeg8-dev package. This will solve the libjpeg error message.

sudo ln -s /usr/lib/x86_64-linux-gnu/libpng.so /usr/lib/libpng.so

Create a symbolic link to the libpng.so in the /usr/lib directory. This will solve the libpng error message.

Next step – compiling PHP.

sudo ./compile.sh 5.2.17
sudo ./compile.sh 5.3.18

Where the number after the ./compile.sh is the PHP version you wish to compile.

Now that we have compiled PHP, we can move on to set up apache. If you followed the apache tutorial, first of we need to recompile it.

./configure \
	--prefix=/usr/local/apache2.2.22 \
	--with-included-apr \
	--enable-mods-shared=all \
	--enable-so \
	--enable-mod-rewrite \
	--enable-suexec \
	--with-suexec-caller=daemon

These are just my configuration options. The options you have to pay attention to for this topic are:

  • –enable-so
  • –enable-suexec
  • –enable-suexec-caller=daemon – You can change this according to which user your apache runs under.
make
sudo make install

Apache should be up and running. Next stop – FastCGI module.

Download and extract the latest (current) source from http://www.fastcgi.com/dist/.

tar xzf mod_fastcgi-current.tar.gz
cd mod_fastcgi-2.4.6

Next thing to do is rename Makefile.AP2 to Makefile, so we can compile it for apache 2.

cp Makefile.AP2 Makefile

Compile and install the module.

make top_dir=/usr/local/apache2.2.22
sudo make install top_dir=/usr/local/apache2.2.22

The top_dir parameter is optional. If you compiled apache to the more usual location like /usr/local/apache2, you do not need to set this option.

Now that we have mod_fastcgi module compiled, we can load it as a shared object. Add the following line to your httpd.conf:

LoadModule fastcgi_module modules/mod_fastcgi.so

If you had previously ran PHP as an apache module, you must disable it. You can only run PHP either as CGI or an apache module. So in httpd.conf, comment out this line:

# LoadModule php5_module modules/libphp5.so

Create a .conf file (you can call it what you want) – i.e. php-fcgi.conf and put the following content inside:

<IfModule mod_fastcgi>
    FastCgiWrapper /usr/lib/apache2.2.22/bin/suexec
    FastCgiConfig -idle-timeout 110 -killInterval 120 -pass-header
        HTTP_AUTHORIZATION -autoUpdate

    ScriptAlias /php-fcgi/ /var/www/cgi-bin/
</IfModule>

And include it in your main httpd.conf:

# Include FastCGI configuration
Include conf/php-fcgi.conf

Now create a directory for CGI scripts.

sudo mkdir /var/www/cgi-bin

And create scripts.

sudo nano /var/www/cgi-bin/php-cgi-5.2.17
sudo nano /var/www/cgi-bin/php-cgi-5.3.18

And put something like the following in. Just change the version of PHP.

#!/bin/sh
version="5.3.18"

PHPRC=/usr/local/php/phpfarm/inst/php-${version}/lib/php.ini
export PHPRC

PHP_FCGI_CHILDREN=3
export PHP_FCGI_CHILDREN

PHP_FCGI_MAX_REQUESTS=5000
export PHP_FCGI_MAX_REQUESTS

# which php-cgi binary to execute
exec /usr/local/php/phpfarm/inst/php-${version}/bin/php-cgi

These scripts should be executable. So we set permissions to 0755.

cd /var/www/cgi-bin
sudo chmod +x *

Now we configure apache virtualhosts. Each virtualhost will have it’s own subdomain and PHP version.

NameVirtualHost localhost.53:80
<VirtualHost localhost.53:80>
  ServerName localhost.53
  DocumentRoot /var/www
  <Directory "/var/www">
    AddHandler php-cgi .php
    Action php-cgi /php-fcgi/php-cgi-5.3.18
  </Directory>
</VirtualHost>

Do the same for all versions of PHP.

We are almost there. Since these subdomains (asumingly) do not exist yet, we must point them to your localhost IP.

sudo nano /etc/hosts

Just add your subdomains after the localhost definition. It should look something like this:

127.0.0.1 localhost localhost.52 localhost.53

Everything is set, so just restart the apache.

sudo /usr/local/apache2.2.22/bin/apachectl -k restart

Put a file phpinfo.php in /var/www directory with the following line:

And test it. If everything went well, you should see different PHP versions on different subdomains.

  • http://localhost.52/phpinfo.php – PHP 5.2.17
  • http://localhost.53/phpinfo.php – PHP 5.3.18

Enjoy!

Post updated on November 12th, 2012th to reflect latest updates.

Doctrine connection error – symfony 1.4 – php 5.3

Posted November 20th, 2010 in Tehnikalije by Metod

I just got in the sandbox with symfony 1.4 & php 5.3.3. In the beginning everything went well, untill I tried to pull some data from a database. I got this error:

PDO Connection Error: SQLSTATE[HY000] [2002] No such file or directory

Error log said:

PHP Warning:  PDO::__construct() [<a href='pdo.--construct'>pdo.--construct</a>]: [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock)

It made it clear that mysql.sock was missing or I had some configuration problems. Turns out my php.ini file had to be changed a bit.

pdo_mysql.default_socket=/var/run/mysqld/mysqld.sock

Solution?
Find out where mysqld.sock or mysql.sock is residing and change the default value in your php.ini. That should solve the problem.

Neat PHP trick for alternate background display

Posted September 27th, 2010 in Tehnikalije by Metod

Many is the time we have to use alternate background for displaying some data. The usual solution to this problem would look something like:

Which in my opinion is somewhat a dirty solution. So I did a little research and found a bit neater solution:

It might even be a bit faster but I have not done any benchmarking.

Setting up your local development environment – PHP

Posted July 3rd, 2010 in Tehnikalije by Metod

Last time we went through steps needed for setting up apache. Now we will set up PHP. I encourage you to read the article on multiple PHP versions, which takes use of PHPFarm that makes things a lot easier. Plus you get to have multiple PHP versions installed.

Download and prepare

First few steps are practically the same as for the apache. Download the source and check md5.

$ md5sum php-5.4.8.tar.gz | grep 'b25b735f342efbfdcdaf00b83189f183'

Extract the archive.

$ tar xzf php-5.4.8.tar.gz

Go into the extracted directory.

$ cd php-5.4.8

Configure for compiling

$ ./configure \
--prefix=/usr/local/php5.4.8 \
--with-apxs2=/usr/local/apache2.2.22/bin/apxs \
--with-config-file-path=/usr/local/php5.4.8/conf \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--enable-cli \
--with-pear \
--with-openssl \
--with-iconv \
--with-curl \
--enable-intl \
--enable-mbstring \
--enable-exif \
--with-zlib \
--with-gd \
--with-gettext \
--enable-gd-native-ttf \
--with-mhash \
--with-pspell \
--with-mcrypt \
--enable-bcmath \
--enable-sockets \
--enable-soap \
--enable-calendar \
--with-png-dir=/usr \
--with-jpeg-dir=/usr

You don’t need all of these extras, they just might be useful to you. Some of the more important ones are:

  • –prefix=… sets the location of the PHP installation.
  • –with-apxs2=… sets the location of the apxs binary of the apache. Adjust accordingly.
  • –with-config-file-path=… sets the location of the php.ini configuration file. I just set it so it is more clear where the .ini file will be.
  • –enable-cli whether or not you want to use php from the command line.

You can read more about the configuration options in the PHP manual.

On Ubuntu 11.04, the configuration might fail with error messages about libjpeg and/or libpng. What you can do is the following:

# apt-get install libjpeg8-dev

Install the libjpeg8-dev package. This will solve the libjpeg error message.

# ln -s /usr/lib/x86_64-linux-gnu/libpng.so /usr/lib/libpng.so

Create a symbolic link to the libpng.so in the /usr/lib directory. This will solve the libpng error message.

Compile

$ make

If this error occures while doing “make”:

/usr/bin/ld: cannot find -lltdl collect2: ld returned 1 exit  status

Just install the libltdl-dev package. Should be something similar for other linux distributions.

# apt-get install libltdl-dev

It is a good practice to run tests and report any possible bugs by running this command. It however is not mandatory to do it.

$ make test

This might take a while. After tests are through, there might be a fail or two, so you can decide if you want to report those or not.

Let’s go on and install it. This command must be run as super user.

# make install

Configure

Now we have to copy the php.ini to the appropriate directory.

# cp php.ini-recommended /usr/local/php5.4.8/conf/php.ini

Next, we configure apache to handle our PHP scripts.

# gedit /usr/local/apache2.2.22/conf/httpd.conf

Find a line which starts with AddType and add this below.

AddType application/x-httpd-php .php

Than in line which starts with DirectoryIndex add this.

index.php

Save the file and restart apache.

# /usr/local/apache2.2.22/bin/apachectl -k restart

Test

Go to your documents root directory. In previous tutorial we used /var/www directory for this.

In this directory create a file named test.php and put this in it:

Now go to http://localhost/test.php

If you see a lot of data about your PHP installation, you have succeeded.

Enjoy!

Post updated on November 12th, 2012 to reflect newest updates.