Update ImageMagick on Travis-CI

I have an open source software project called Imbo that I have hooked up to Travis-CI for testing of pull requests and pushes on GitHub. I recently came across an issue that made some tests fail on Travis-CI that passed on all other machines I run tests on. The issue seemed to be because of an old version of ImageMagick (6.6.9-7). Travis-CI use this version since they base their VM’s on Ubuntu-12.04-LTS, and that version of ImageMagick is the one currently available on 12.04.

I solved it by compiling my own version of ImageMagick via the .travis.yml file:

before_install:
  - wget http://www.imagemagick.org/download/ImageMagick.tar.gz
  - tar xf ImageMagick.tar.gz
  - cd ImageMagick*
  - ./configure --prefix=/usr
  - make
  - sudo make install
  - cd ..
  - printf "\n" | pecl install imagick-beta

This made the tests pass, but the build went from taking about 2 minutes to 10, so it was not really a good fix. After asking around on the #travis-ci channel on IRC (freenode) @henrikhodne said that they might upgrade the package if they found a decent PPA. I looked around myself and found a PPA for Ubuntu-12.04 that had a newer version of ImageMagick available, so I decided to try and use that instead of compiling my own to see if that might speed up the build. I replaced the above snippet with the following in the .travis.yml file:

before_install:
  - sudo add-apt-repository -y ppa:moti-p/cc
  - sudo apt-get update
  - sudo apt-get -y --reinstall install imagemagick
  - printf "\n" | pecl install imagick-beta

This reduced the build time to around 3 minutes, and all tests are passing! Travis-CI, I LOVE YOU!

Posted in PHP, Technology | Tagged , , , , , , , , | 5 Comments

PHP-based BitTorrent tracker

Some of you might know that I have a library for encoding PHP variables to their BitTorrent counterparts, and back to PHP, called PHP BitTorrent. Some days back I pushed another library to GitHub, namely a standalone BitTorrent tracker.

The tracker is pretty extensible and comes with a couple of back end implementations for you to choose from, the default being MongoDB. The tracker is not a complete tracker system with user authentication and torrent downloads and such, but a standalone “announcer” (like for instance the official BitTorrent tracker: bttrack). PHP BitTorrent Tracker can easily be extended to do user statistics and so forth though. An event listener doing this is shipped with the package.

More information (installation, configuration and more) is available at the project page on GitHub. Feel free to play around with it, and please use the issue tracker at GitHub if you find any issues. Happy torrenting!

Posted in PHP, Technology | Tagged , , , , , , , | Leave a comment

IRC notifications on GitHub

I host my open source projects over at GitHub, and most of them do notifications to IRC on different events. Recently I saw that imbo/imbo and imbo/imboclient-php did not notify new pull requests and new issues in the issue tracker to IRC. These options are not visible in the admin GUI of the hook either. You can however use the API to enable these events.

First, figure out the id of your IRC hook by doing:

curl -u $user https://api.github.com/repos/$account/$repos/hooks

Find the id of the IRC hook, then issue a POST:

curl -u $user -XPOST https://api.github.com/repos/$account/$repos/hooks/$hookId -d '{"events":["push", "pull_request", "issues"]}'

These are currently all the events available for this hook. The hook itself can be found here.

Posted in Technology | Tagged , , , , | 1 Comment

SysInfo-0.1.0 released

The other day I released the first version of a small Open Source (MIT) PHP library that can be used to fetch system information; like for instance CPU usage, load average and memory usage (amongst other information). The library is available at GitHub and can be installed using composer (support for PEAR and phar will be added later).

Installation using composer is very simple:

curl -s http://getcomposer.org/installer | php
php composer.phar create-project christeredvartsen/sysinfo sysinfo 0.1.0

and you are good to go. If you have a project with a composer.json file, simply add a dependency:

{
  "require": {
    "christeredvartsen/sysinfo": "0.1.0"
  }
}

and update your dependencies using composer.phar:

php composer.phar update

After installation, fire up your favorite editor (Vim, right?) and paste the following to get started using the library:

<?php
// Edit to match the directory where you installed SysInfo
require 'sysinfo/vendor/autoload.php';

// Fetch an instance based on the PHP_OS constant. Currently only Linux is supported.
$sysInfo = SysInfo\SysInfo::factory();
// or
$sysInfo = new SysInfo\Linux();

// Get snapshot of CPU
$cpu = $sysInfo->getCPU();

// Get snapshot of load
$load = $sysInfo->getLoad();

// Get snapshot of memory
$memory = $sysInfo->getMemory();

// Get snapshot of disk info
$disk = $sysInfo->getDisk();

// Get snapshot of uptime
$uptime = $sysInfo->getUptime();

The snapshots above are implementations of different interfaces. Read the documentation over at GitHub to see which methods you can call, and what they return:

If you want to add some features or find any bugs, file an issue and I’ll see what I can do.

Posted in PHP | Tagged , , , | Leave a comment

Elastica + elasticsearch

I just published a post on how to query elasticsearch using the Elastica client over at our company blog:

Using Elastica to query elasticsearch

Posted in PHP | Tagged , , | Leave a comment

How to make GitHub host your PEAR channel

GitHub does probably not need an introduction, but their “user pages” might.

Each user on GitHub can have their own pages by creating a repository called <user>.github.com. These pages will be accessible via, yup, you guessed it: http://<user&gt;.github.com. My username is christeredvartsen, so my GitHub pages are located over at christeredvartsen.github.com.

You can read all about these pages over at pages.github.com.

Last year I wrote a post on how to set up your own PEAR channel using Pirum, and now we can use this together with the GitHub pages to have GitHub host our channel.

First, create an empty directory on your machine and initialize a git repository:

mkdir <user>.github.com
cd <user>.github.com
git init

Now you need to add a pirum.xml file in the directory you just created so you can build your channel. The pirum.xml file for my own channel looks like this:

<?xml version="1.0" encoding="UTF-8" ?>
<server>
  <name>pear.starzinger.net</name>
  <summary>Starzinger PEAR channel</summary>
  <alias>stz</alias>
  <url>http://pear.starzinger.net/</url>
</server>

Create the file with your favorite editor and change the values so they fit your own channel, save it, then run the following commands inside your newly created directory:

pirum build .
git add --all
git commit -a -m "Initial build"

Now you can head over to GitHub to create your repository. Once you have done that, enter the following commands in your terminal (in the directory you just created):

git remote add origin git@github.com:<user>/<user>.github.com.git
git push origin master

Now your (empty) channel has been pushed to GitHub, and you can browse it at your user page. Next step is to add some PEAR packages to your channel, re-build, then simply run:

git add --all
git commit -a -m "Added some package"
git push

in the directory where your channel files are located. That’s that, easy as pie!

You can also use a different domain name for your user pages if you want to. Read more about that feature over at GitHub.

Oh, and if you haven’t already you might want to beef up your billing plan from Free to something more awesome. I for one think GitHub is totally worth it.

Posted in PHP, Technology | Tagged , , , , | 1 Comment

PHP_BitTorrent-0.2.0 released

During my Christmas vacation I received a pull request on GitHub for my PHP_BitTorrent project which added proper namespaces to the classes. I decided to merge the pull request and do some other changes as well. I removed the static methods and added instance methods instead which, IMHO, makes the API cleaner, and more important, made the code easier to test.

There are some example usage and instructions on how to install PHP_BitTorrent over at the project page on GitHub.

I have also done some more work on the BitTorrent tracker part of the project which is available in a separate branch (not a part of the 0.2.0 release). Feel free to play around with it and don’t hesitate to send me a pull request if you have improvements for it.

Posted in PHP, Technology | Tagged , , | Leave a comment

Fat Models, Chubby Routes, Super-Skinny Controllers

We have just started a blog at work where we will be writing about some of the technologies we use daily. I just wrote a post about beefing up the routes in Zend Framework applications.

Read all about it over at VG Tech.

I will continue to write on this blog, but if you are interested in web technologies be sure to checkout VG Tech as well.

Posted in PHP, Technology, Work related | Tagged , , | Leave a comment

Playing with traits in PHP 5.4alpha1

PHP 5.4alpha1 was released the other day with some cool new features. One of them is an implementation of traits. From the RFC:

Traits is a mechanism for code reuse in single inheritance languages such as PHP. A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies. The semantics of the combination of Traits and classes is defined in a way, which reduces complexity and avoids the typical problems associated with multiple inheritance and Mixins.

There are some nice examples over at the RFC, and I will provide another example in this post.

But first, to be able to play with this we need to build PHP. It’s the usual ./configure; make; make test; dance so it should not pose too many problems. The make test step is optional, but every time you skip that step, God creates another Perl programmer, so please, make test. If you run into any problems building PHP you will have to look elsewhere for answers. I won’t cover that in this post.

Now that you have built PHP, lets start writing some code.

When working on a framework (or something else for that matter), you will most likely need to introduce parameters for some of your classes, be it database adapters or a storage adapter. All these classes will most likely have the following methods:

  • setParams(array $params)
  • getParams()
  • setParam($key, $value)
  • getParam($key)

The implementation of these methods will most likely be the same. If you put the actual implementation of these methods in many classes you’re not DRY anymore. Since PHP does not support multiple inheritance you will have to make a base class with the implementation of these methods, and have other classes ultimately extend this base class. Now you are relatively DRY, but this solution is not optimal(and sometimes not possible at all).

Enter traits, the newest superhero on the PHP street, or something like that.

Now, I’ll create two classes that needs to support parameters; Cogo\Database\MySQL and Cogo\Storage\Filesystem. They will each extend Cogo\Database and Cogo\Storage respectively that will have the parameter-related methods.

<?php
namespace Cogo;

abstract class Database {
    private $params;

    public function __construct(array $params) {
        $this->setParams($params);
    }

    private function setParams(array $params) {
        $this->params = $params;
    }

    public function getParams() {
        return $this->params;
    }

    protected function setParam($key, $value) {
        $this->params[$key] = $value;
    }

    public function getParam($key) {
        return (isset($this->params[$key]) ? $this->params[$key] : null);
    }

    protected function someDatabaseRelatedMethod() {
        // ...
    }

    abstract protected function someOtherDatabaseRelatedMethod();
}
<?php
namespace Cogo\Database;
use Cogo\Database;

class MySQL extends Database {
    protected function someOtherDatabaseRelatedMethod() {
        // ...
    }
}
<?php
namespace Cogo;

abstract class Storage {
    private $params;

    public function __construct(array $params) {
        $this->setParams($params);
    }

    public function setParams(array $params) {
        $this->params = $params;
    }

    public function getParams() {
        return $this->params;
    }

    public function setParam($key, $value) {
        $this->params[$key] = $value;
    }

    public function getParam($key) {
        return (isset($this->params[$key]) ? $this->params[$key] : null);
    }

    protected function someFilesystemRelatedMethod() {
        // ...
    }

    abstract protected function someOtherFilesystemRelatedMethod();
}
<?php
namespace Cogo\Storage;
use Cogo\Storage;

class Filesystem extends Storage {
    protected function someOtherFilesystemRelatedMethod() {
        // ...
    }
}

As you can see the parameter-related methods are almost the same. The difference is that the Cogo\Database class wants to have its set methods private and protected.

One possibility would be to create a base class for the parameter handling, and have the two abstract classes extend that one, but because multiple inheritance is not possible in PHP it’s easy to grind to a halt using this solution. In the end you might end up with a huge base class with loads of logic simply because it has some logic that you want to share across many other classes.

The base class would also have to have private on the set methods since you can’t override a method with a stricter visibility, only the other way around.

Using traits, this can be solved in a more elegant fashion. Traits also support changing visibility of the methods defined in the trait, so Cogo\Database can keep its private and protected implementations. Here is how the trait can look like:

<?php
namespace Cogo;

trait ParamsHandler {
    private $params;

    public function __construct(array $params) {
        $this->setParams($params);
    }

    public function setParams(array $params) {
        $this->params = $params;
    }

    public function getParams() {
        return $this->params;
    }

    public function setParam($key, $value) {
        $this->params[$key] = $value;
    }

    public function getParam($key) {
        return (isset($this->params[$key]) ? $this->params[$key] : null);
    }
}

Notice that the trait now contains all logic regarding parameters (including the $params property and the constructor). The RFC states that:

Since Traits do not contain any state/properties …

This seems to work after all. The RFC was created a while ago and it no longer matches the actual implementation 100% obviously.

Now that we have a trait with the logic we want in both our abstract classes, let’s use it:

<?php
namespace Cogo;

abstract class Database {
    use ParamsHandler {
        setParams as private;
        setParam as protected;
    }

    protected function someDatabaseRelatedMethod() {
        // ...
    }

    abstract protected function someOtherDatabaseRelatedMethod();
}
<?php
namespace Cogo;

abstract class Storage {
    use ParamsHandler;

    protected function someFilesystemRelatedMethod() {
        // ...
    }

    abstract protected function someOtherFilesystemRelatedMethod();
}

As you can see we have replaced the implementation of the parameter methods from both abstract classes with a couple of use statements. In the Cogo\Storage class we have used the methods from the trait as-is, which means they are all public. In the Cogo\Database class we have changed the visibility of two methods to match the initial implementation. We have now managed to keep ourselves DRY, as well as not creating a base class that the two abstract classes extends.

This is just a simple example of how to use traits. They can do loads more, and there are issues like for instance conflict resolution which is not mentioned in this post at all. I will probably post more examples of traits (and perhaps other 5.4 features) later on. In the meantime you should read the RFC, and try it out for yourself.

You will find all the code mentioned in this post over at my php-traits-examples repository at GitHub. Feel free to play around with it, and if you have other examples on how to use traits please fork the project and send me a pull request so I can include it (and learn from it).

Posted in PHP, Technology | Tagged , , | 7 Comments

Mocking MongoCollection with Mockery

I have played around with the mongo extension to PHP lately and came across an issue when I was trying to write unit tests with PHPUnit for some methods using MongoCollection::insert().

Consider a class method looking like this:

class Database {
    public function insertData($hash, Image $image) {
        $data = array(
            'hash' => $hash,
            'name' => $image->getName(),
        );

        $this->getCollection()->insert($data, array('safe' => true));
        $image->setId((string) $data['_id']);

        return true;
    }

    // ...
}

The Image class is just some custom class, and the getCollection() method returns a property set in the class (so I can inject mocked versions of the collection and not depend on MongoCollection).

As you see from the code above the insertData() method changes the first parameter (it adds an _id element that is set to an instance of MongoId). The problem with testing the above method is that when mocking MongoCollection you can’t make it add the _id element to the passed array, so the test fails because of an E_NOTICE that says:

Undefined index: _id.

I tried to solve the problem by using Mockery (a simple but flexible (and awesome) mock object framework by Pádraic Brady) like this:

public function testInsertData() {
    $id = MongoId();
    $idAsString = (string) $id;
    $image = \Mockery::mock('Image');
    $image->shouldReceive('setId')
          ->once()
          ->with($idAsString)
          ->andReturn($image);
    $image->shouldReceive('getName')
          ->once()
          ->andReturn('some name');

    $collection = \Mockery::mock('MongoCollection');
    $collection->shouldReceive('insert')
               ->once()
               ->with(\Mockery::on(
                   function(&$data) use($id) {
                       $data['_id'] = $id;
                       return true;
                   }), \Mockery::type('array'));

    // The object we want to test with
    $database = new Database;
    $database->setCollection($collection);
    $database->insertData($hash, $image);
}

When running the above test I get the following error:

Parameter 1 to {closure}() expected to be a reference, value given

This is because Mockery uses the call_user_func_array function to call the closure given to \Mockery::on(), and that function does not support references. PHPUnit also uses this function internally for its mock objects.

The description of MongoCollection::insert() in the PHP manual looks like this:

public mixed MongoCollection::insert ( array $a [, array $options = array() ] )

Feeling a bit bold I tried to change the insertData() method above to pass an instance of stdClass instead of an array to MongoCollection::insert(). Why? Objects in PHP-5 are all references, so I might be able to get Mockery to work without having to specify &$data in the closure. It’s not documented, but hey, you never know without trying right? Here is the new version of insertData():

public function insertData($hash, Image $image) {
    $data = new stdClass;
    $data->hash = $hash;
    $data->name = $image->getName();

    $this->getCollection()->insert($data, array('safe' => true));
    $image->setId((string) $data->_id);

    return true;
}

The only change I made in the test method was to change the closure I give to \Mockery::on() from:

function(&$data) use ($id) {
    $data['_id'] = $id;
    return true;
}

to

function($data) use ($id) {
    $data->_id = $id;
    return true;
}

and voila, the test works! The part about giving an instance of stdClass to MongoCollection::insert() is not documented, and I’m not sure if it’s a feature that’s going to stay implemented.

At first I tried to solve this using the built in mock objects in PHPUnit, but since they clone the objects used in the mocks the trick above would not work anyway as the change would not be made on the correct reference.

And thats that!

Posted in PHP, Technology | Tagged , , , , , , | Leave a comment