SaltStack HowTo: Installing, Setting up and Provisioning

The problem and the solution

I work at a company that creates web sites and applications. I joined them in 2008 when there were 2 front-end developers and 3 back-end, including me. Company slowly grew to 7 back-end and 6 front-end developers. The following problem started to get in our way more and more: we were working on one single development server, which means that from time to time we would step on each other toes. What happened is, one guy would open a file, the other guy would open the same file and whoever saves and closes first, is a loser. Also, that server had to have every possible service, program and PHP extension imaginable because it needed to support a lot of diverse projects.

Continue reading “SaltStack HowTo: Installing, Setting up and Provisioning” »

Using Google API to get Google Analytics data with PHP

You might have a need to display data from Google Analytics on your website like number of product page views in your webshop CMS. To achieve that, you would request the page views and maybe sessions (unique visits) for a certain URL from Google Analytics. If you’re using PHP, Google created Google API PHP client to help you do that. In this tutorial we’re going to use Server to Server Application authentication mechanism called two-legged OAuth.

Continue reading “Using Google API to get Google Analytics data with PHP” »

Comment driven development

There is quite a handful of programming techniques out there; TDD, BDD, YAGNI, DRY to name a few. This post will be about something many people might already be doing but don’t know it has a name: Comment-driven development or comment programming.

CDD is helpful for:

  • prototyping,
  • spitting out your thoughts in code editor, so you don’t forget anything later (good for brainstorming sessions),
  • explaining what needs to be done if someone else is going to be writing the code itself,
  • commenting the code :). Comments could remain, so your code is documented from the get-go

I often start the new PHP file or class or even method with the layout in comments. Here’s an example:

// Get the needed models
// Collect todo items
// Get lists that the todo items belong to
// Send to View

Ok, everything is clear. After the real code sets in it looks like this:

public function someAction()
    // Get the needed models
    $todoTable  = new TodoTable();
    $listTable  = new ListTable();
    // Collect todo items
    $todos      = $todoTable->findByAuthor($author);
    // Get lists that the todo items belong to
    foreach ($todos as &$todo) {
        $todo['lists'] = $listTable->find($todo['list_id']);
    // Send to View
    DIC::get('View')->todos = $todos;

As you can see, the comments can stay in place. Even for this simple example, it is good practice to document your code.

The Wikipedia article states:

In comment programming the comment tags are not used to describe what a certain piece of code is doing, but rather to stop some parts of the code from being executed. The aim is to have the commented code at the developer’s disposal at any time he might need it.

And later on:

However, comment programming is used instead of a real implementation. The idea is that many functions can be written like this, and then the design can be reworked and revisited without having to refactor a lot of source code.

So, Wikipedia article is somewhat contradictory to itself. The aspect of comment programming I am writing about here is the “comments instead of a real implementation” part.

Do you write your code with comments first approach? Do you use some other technique?

Test projects viewer

I have a couple of test projects in my test directory. This is where I usually put the latest wordpress, phpBB or any other script or web software I would like to test out or develop and play with. Until recently I had to access those by writing the virtual host path (www.test.local) and then append the directory name ( /drupal). I got tired of it, and coded a nifty little “browser” which displays all of the directories and files. Combined with the DNS wildcards, you can have unlimited virtual domains without having to configure them in vhosts, setting the /etc/hosts and restarting apache server. I have included this in the zip file found at the end of this post.

We are using bind dns server to resolve everything that comes to the “test” domain to your machines IP address. After that, Apache takes care of the rest. And what he does is kinda cool. The .htaccess file has a set of rules to test weather the index.php exists in the requested directory (via subdomain), and if it does, he redirects us to that directory. If the index.php doesn’t exist, our “main” index.php shows the contents of that directory, so you can select any other file and run it. Let’s start.

Continue reading “Test projects viewer” »

PHP and template engines (and some other stuff)

I’m developing web sites for quite some time now, started when i was… Young? Yellow? Well, started a long time a go in a galaxy… AARGH! Can’t get Star Wars out of my life! Anyway. In the beginning i was using the good old php mess approach:

echo 'ul';
foreach($array as $item) {
    echo '<li>Some $item here</li>';
echo '<ul>';

And so on. For onepagers this was enough. So i stumbled accross mambo (former Joomla) and saw the raw power of cms and, well for me, good coding. K, let’s make ourselves a content management system. But how? What do we need? The neccesary ingridients where these:

  • Apache with php (obvious)
  • MySQL database (oh, what’s this database thing for?)
  • foresight

What’s the most important? No, wrong, not coffee. Foresight! Without analyzing and planning the outcome can’t be good. I started to code. Heavily. Took a good number of different approaches, all ended bit dull and like they couldn’t take on serious tasks.

Continue reading “PHP and template engines (and some other stuff)” »