Snippets

Create an account or login to be able to add, comment and rate snippets.

Navigation

Refine Tags

Snippets tagged "cli batch" Snippets tagged "cli batch"

CLI and Symfony with complex arguments

I create a script to use complex arguments while runing script from command line.

It also allow to switch SF_ENVIRONMENT, SF_APP and SF_DEBUG directly from command line.

Just create configBatch.php in your project's config directory and paste this :

<?php
define('SF_ROOT_DIR',    realpath(dirname(__file__).'/..'));
 
$argv = array();
for ($i = 1; $i < $_SERVER["argc"]; $i++) {
    if ($_SERVER["argv"][$i]{0} === '-') {
        // argument
        $value =  (
            isset($_SERVER["argv"][$i+1]) 
            && 
            $_SERVER["argv"][$i+1]{0} !== '-'
            ?
            $_SERVER["argv"][$i+1]
            :
            true
        );
 
        if ($_SERVER["argv"][$i]{1} === '-') {
            // long argument
            $argv[substr($_SERVER["argv"][$i], 2)] = $value;
        }
        else {
            foreach (str_split($_SERVER["argv"][$i]) as $arg) {
                if (ereg('[a-zA-Z0-9]', $arg)) {
                    $last_arg   = $arg;
                    $argv[$arg] = true;
                }
            }
            $argv[$last_arg] = $value;
        }
    }
}
 
define('SF_APP',         (isset($argv['app'])?$argv['app']:'www'));
define('SF_ENVIRONMENT', (isset($argv['env'])?$argv['env']:'prod'));
define('SF_DEBUG',       (isset($argv['debug'])?1:0));
 
require_once(SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.SF_APP.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php');
 

Then, start your batch file with :

<?php
 
require_once(realpath(dirname(__file__).'/..').DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'configBatch.php');
 
// Your script start here
 

Now, you can pass arguments to you script :

php -f /path/to/project/batch/my_batch.php -rf some_value --help --foo bar
 

Here is the generated $argv :

$argv = array (
  'r' => true,
  'f' => 'some_value',
  'help' => true,
  'foo' => 'bar',
);
 

You can also change the SF_ENVIRONMENT with the --env argument, the SF_APP with the --app argument and the debug flag with --debug argumnent

Enjoy

by Romain Cambien on 2007-11-22, tagged argv  batch  cli 

simple console options for batch scripts

I was looking for this type of functionality:

'php batch/mybatch.php app=batch env=dev'

The following allows me to use the same batch scripts for different environments and applications. It also keeps batch scripts lean by only requiring one line.

batch/run_me.php:

require_once('lib/batch.php');
 
//
// begin batch scripting here
//

batch/lib/batch.php:

set_time_limit(0);
 
$app = get_app($argv[1]);
$env = get_env($argv[2]);
$debug = get_debug($argv[3]);
 
//
// initialize symfony
//
 
define('SF_ROOT_DIR',         realpath(dirname(__FILE__).'/../..'));
define('SF_APP',              $app);
define('SF_ENVIRONMENT',      $env);
define('SF_DEBUG',            $debug);
 
require_once(SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.SF_APP.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php');
 
sfContext::getInstance();
 
//
// initialize doctrine (just an example)
//
 
$connection = sfDoctrine::connection();
 
//
// parse command line args
//
 
function get_app($argv)
{
  preg_match('@app=(\w+)@', $argv, $match);
 
  return isset($match[1]) ? $match[1] : 'batch';
}
 
function get_env($argv)
{
  preg_match('@env=(\w+)@', $argv, $match);
 
  return isset($match[1]) ? $match[1] : 'dev';
}
 
function get_debug($argv)
{
  preg_match('@(true|false)@', $argv, $match);
 
  return isset($match[1]) ? $match[1] : true;
 
}
by Roger Stanton on 2006-09-25, tagged batch  cli 

Sending email from a batch script

It can be tricky sending email from a batch script (e.g. for cron use), here's how to do it. Start with your usual batch setup:

define('SF_ROOT_DIR',    realpath(dirname(__FILE__).'/..'));
define('SF_APP',         'app_name');
define('SF_ENVIRONMENT', 'environment');
define('SF_DEBUG',       true);
 
require_once(SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.SF_APP.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php');
sfContext::getInstance();

You can set attributes directly from the batch script if you wish:

sfContext::getInstance()->getRequest()->setAttribute('key', $value);

Then forward on to another module/action to handle processing and forwarding to mail as usual:

sfContext::getInstance()->getController()->forward('action', 'module');
by James McGlinn on 2006-06-22, tagged batch  cli  email