News Archive
PhpRiot Newsletter
Your Email Address:

More information

Creating A Skeleton Component

Note: This article was originally published at Planet PHP on 24 March 2011.
Planet PHP

In my Beyond Frameworks talk, I explained how a component-based architecture can help answer some of the important (i.e. expensive!) questions you might face when creating long-lived apps that rely on a PHP framework. In this series of blog posts, I'm going to look at how to go about creating and working with components.

In my last article, I posted a list of questions to consider when decomposing the design of an app, and I put together the first cut of the components that will make up my sentiment analysis app. Now it's time to get into creating the app's first component, using the ComponentManager that I wrote for phix.

Dev Environment Prep

If you haven't already, you'll need to install phix and ComponentManager onto your computer:

$ pear channel-discover $ pear install Gradwell/phix $ pear install Gradwell/ComponentManager

There are also a lot of very useful dev tools that our component can take full advantage of. Sadly, due to the limitations of the PEAR Installer, we currently can't install all of these with a single command, but bear with me, I promise they're worth the effort:

# phpunit sudo pear channel-discover sudo pear channel-discover sudo pear channel-discover sudo pear install --alldeps phpunit/PHPUnit # phing sudo pear channel-discover sudo pear install --alldeps phing/phing # pdepend sudo pear channel-discover sudo pear install --alldeps pdepend/PHP_Depend-beta # phpdoc sudo pear install --alldeps pear/PhpDocumentor # phpmd sudo apt-get install php5-imagick sudo pear channel-discover sudo pear install --alldeps phpmd/PHP_PMD-alpha # phpcpd sudo pear install --alldeps phpunit/phpcpd # phpcs sudo pear install --alldeps pear/PHP_CodeSniffer-beta # phpcb sudo pear install --alldeps phpunit/PHP_CodeBrowser

Thankfully, we only have to do that once, and it is something I'll make a tool to automate if no-one beats me to it (hint hint!). Now, with the development environment prepared, we can get our component under way.

Creating The Skeleton For The Component

Creating a skeleton component is as easy as:

stuart:~/Devel/sental$ mkdir repustateApi stuart:~/Devel/sental$ cd repustateApi stuart:~/Devel/sental$ git init . Initialized empty Git repository in /home/stuart/Devel/sental/repustateApi/.git stuart:~/Devel/sental/repustateApi$ phix php-library:init . Initialised empty php-library component in .

What have we created?

A Quick Tour Of The Skeleton

Here's a quick list of what the php-library:init command has created in the repustateApi folder:

stuart:~/Devel/sental/repustateApi$ git status # On branch master # # Initial commit # # Untracked files: # (use "git add ..." to include in what will be committed) # # .gitignore # .hgignore # LICENSE.txt # # # build.xml # package.xml # src/ nothing added to commit but untracked files present (use "git add" to track)

Each of the files serves a useful purpose:

  • .gitignore and .hgignore

    These are basic filters to tell Git and Mercurial what to ignore in here. As we develop the component, there will be a few temporary folders created which we'll never want to end up in version control. Filter files for other version control systems are welcome.

  • LICENSE.txt

    Every component needs to come with a clear statement about the rules for re-use, even if it just says All rights reserved.

    If you're looking to open-source your code, you'll find many well-tested licenses from the Open Source Initiative's website.


    Every component needs basic documentation to help a new user get started with it. By putting that into a file, GitHub will automatically pick that up and display it on the component's website.


    This is a file we're going to edit s

Truncated by Planet PHP, read more at the original (another 2765 bytes)