David Caunt

Mobile Software Engineer

Subscribe to my RSS feed

Easy command line scripts with Zend Application

February 25th, 2010

As PHP developers, it is convenient to be able to write command line scripts in PHP. In doing so, you will almost certainly want access to Zend Framework components and their configurations as if you are writing a normal MVC app, but without invoking the MVC stack and without loading unnecessary resources. I’ve seen solutions where actions are exposed as controller actions and called by wget – these are counter-intuitive, inefficient, and will suffer from max execution timeouts and other problems.

Zend_Applicaton to the rescue

Lets start with our website’s public/index.php – the script which sets up and launches an application. It looks roughly like this:

<?php
 
// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH',
              realpath(dirname(__FILE__) . '/../application'));
 
// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV',
              (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV')
                                         : 'production'));
 
require_once 'Zend/Application.php';
$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/config.php'
);
 
$application->bootstrap()->run();

One feature of Zend Application is that you can choose to bootstrap only certain resources, rather than all of those referenced in your application config and bootstrap (_init methods.) The following assumes that our script needs to use the database, and send an email.

<?php
 
// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH',
              realpath(dirname(__FILE__) . '/../application'));
 
// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV',
              (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV')
                                         : 'production'));
 
require_once 'Zend/Application.php';
$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/config.php'
);
 
//only load resources we need for script, in this case db and mail
$application->bootstrap(array('db', 'mail'));

Having bootstrapped the resources, we can retrieve them from the application as follows:

$db = $application->getBootstrap()->getResource('db');
 
//do something!
$row = $db->fetchRow('SELECT * FROM something');

Tags: , , ,

12 Responses to “Easy command line scripts with Zend Application”

  1. [...] Caunt has posted an article on how to access your Zend_Application resources from a command line application: As PHP developers, it is convenient to be able to write command line scripts in PHP. In doing so, [...]

  2. Hm… I expected some Zend_Console_Getopt example…

  3. David says:

    Hi Alexander,

    I’m planning to extend this guide to include a Zend_Console_Getopt example. It should be up in a day or two.

    David

  4. Dmitriy Gerasimenko says:

    David, thx!
    It’s very simple and graceful solution!

  5. [...] Caunt has posted an article on how to access your Zend_Application resources from a command line application: As PHP developers, it is convenient to be able to write command line scripts in PHP. In doing so, [...]

  6. It’s been a few days did you forget about the Zend_Console_Getopt example?

  7. Tobias says:

    Reminder: I’m following your great post but I would also like to read about Zend_Console_Getopt ;)

  8. david says:

    Hi there,
    How can i get APPLICATION_ENV in a command line batch process ?

    echo getenv(‘APPLICATION_ENV’); return FALSE

    Best regards

    David

  9. David says:

    Hi David,

    In the case of command line processes, getenv retrieves variables from the shell. You can set a variable in your bash initialisation file as follows:

    export APPLICATION_ENV=development

    The location of this file varies but in OS X you can use ~/.bash_profile and on Linux look for ~/.bashrc or similar. On Windows it is possible to set environment variables by pressing WIN + BREAK and the clicking the relevant link.

  10. [...] is critical to keep the purpose of the website you’re building at the forefront of your mind. Close early and often. Website builder builds the website based on templates. It may seem like an easy-to-answer question [...]

Leave a Reply

You may wrap code in <code> tags or use <pre lang="php">code</pre> for full highlighting.

Top