Reputation: 733
I have a problem now with building a mechanism that allows to build console scripts with zend for app. For example like:
--scripts
----index.php
----basecmd.php
When basecmd contains main class for other scripts, and file structure is
include index.php
....
MyClass extends Zend_Console_Getopt{
but in index.php i need to setup APPLICATION_ENVOIRMENT with param send as --application_env to script My problem is that I can set it when parsing params with getopt, but how to make it in index.php? Info: I need to show error like: 'application_env must me always set when running script' I'd appreciate any guides for doing it.
Upvotes: 2
Views: 802
Reputation: 3503
If I've understood correctly, you are trying to run your application from CLI/CMD, by calling basecmd.php that will setup variables/constants for index.php to work correctly
Your basecmd.php should look something like this:
#!/usr/bin/env php
<?php
// basecmd.php
require_once 'path/to/Zend/Console/Getopt.php';
try {
$opts = new Zend_Console_Getopt(
array(
'app-env|e=s' => 'Application environment',
'app-path|ap=s' => 'Path to application folder',
'lib-path|lp=s' => 'Path to library',
// more options
)
);
$opts->parse();
if (!($path = $opts->getOption('ap'))) { // cli param is missing
throw new Exception("You must specify application path");
}
define('APPLICATION_PATH', $path);
// process other params and setup more constants/variables
} catch (Zend_Console_Getopt_Exception $e) {
echo $e->getUsageMessage();
exit;
} catch (Exception $e) {
echo $e->getMessage() . "\n";
exit;
}
// it is wise to setup another constant so application can determine is it a web or cli call
define('RUN_VIA', 'cli');
// if all done correctly include application loader script
include 'index.php';
And in your index.php you should just test if constants or variables are already defined:
<?php
// index.php
defined('APPLICATION_PATH') // is it defined
or define('APPLICATION_PATH', '../application'); // no? then define it
defined('RUN_VIA')
or define('RUN_VIA', 'web');
// ... rest of the code
I hope this helps you get on the track ;)
Upvotes: 4