David Caunt

PHP and Zend Framework 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: , , ,

8 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?

Leave a Reply

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

Top