// for example, you can set a DIC in the context that then becomes available to event handlers through $event->getContext()['container'] // i.e. prototypic to inject a symfony container and event dispatcher in the context: // <?php // $context = T24\Context\ExecutionContext::create(); // $context['container'] = $app->getContainer(); // $context->setEventDispatcher($app->getContainer()->get['event_dispatcher']); // require('run-agent.php'); // $context = isset($context) ? $context : ExecutionContext::create(); if (!$context instanceof ExecutionContext) { throw new RuntimeException('Execution context is not an instance of T24\\Handler\\ExecutionContext but ' . get_class($context)); } // php file.php ./assets/example.txt ini_set('display_errors', 1); $cmd = new Commando\Command(); $cmd->setHelp('sqs agent')->option('ttl')->describedAs('sets the ttl / runtime of the script. Defaults to 45 (in seconds)')->option('sleep')->defaultsTo(null)->describedAs('sets the time to rest between two message retrievals. Defaults to 3 (in seconds)')->option('config')->defaultsTo(null)->describedAs('configuration file'); $run = function () use($cmd, $context) { $_options = $cmd->getOptions(); $options = []; foreach ($_options as $key => $option) { $options[$key] = $option->getValue(); } $options['base_dir'] = __DIR__ . '/../'; // for this binary, subscribe to some configuration events to load config files etc. // first, load the configuration file $context->getEventDispatcher()->addListener(SqsEvents::EVENT_SQSAGENT_CONFIGURE, function (ConfigEvent $event) use($options) { if ($options['config']) { $newCfg = json_decode(file_get_contents($options['config']), true); if (!$newCfg) { throw new RuntimeException('could not load config file ' . $options['config']); }
<?php require_once __DIR__ . '/../vendor/autoload.php'; // Set Global Facade class Graylogger extends \Graylogger\Graylogger { } // Prevent CLI handling if loaded another way if (PHP_SAPI != 'cli') { return; } $cmd = new \Commando\Command(); $cmd->setHelp('A simple CLI executable for sending a message to a Graylog server using PHP.'); $cmd->option()->require()->referToAs('message')->description('The log message.'); $cmd->option('f')->aka('facility')->default(Graylogger::FACILITY)->description('The log facility. Default: ' . Graylogger::FACILITY); $cmd->option('l')->aka('level')->default(Graylogger::LEVEL)->description('The log level, per PSR-7. Default: ' . Graylogger::LEVEL); $cmd->option('h')->aka('host')->default(Graylogger::HOST)->description('The Graylog server host. Default: ' . Graylogger::HOST); $cmd->option('p')->aka('port')->default(Graylogger::PORT)->description('The Graylog server port. Default: ' . Graylogger::PORT); Graylogger::log($cmd[0], $cmd['host'], $cmd['port'], $cmd['facility'], $cmd['level']);
$cmd_options = new Commando\Command(); // Define option "--file" $cmd_options->option('file')->file()->describedAs('Input file with CSV data to be parced'); // Define flag "--create_table" $cmd_options->flag('create_table')->boolean()->needs('u', 'p', 'h')->describedAs('Instructs to create table in MySQL DB with name "users"'); // Define flag "--dry_run" $cmd_options->flag('dry_run')->boolean()->describedAs('No data will be added to DB. All other functions will be executed'); // Define option "u" $cmd_options->option('u')->needs('p', 'h')->describedAs('MySQL username'); // Define option "-p" $cmd_options->option('p')->needs('u', 'h')->describedAs('Password of MySQL user'); // Define option "-h" $cmd_options->option('h')->needs('u', 'p')->describedAs('MySQL hostname or IP'); //Compose help message $help = "Usage:\n\nCreating table:\n\tphp user_upload.php --create_table -u <MySQL user>\n\t\t-p <MySQL user password> -h <MySQL hostname>\n\t\t\nDry run, check CSV content and exit without importing to DB:\n\tphp user_upload.php --dru_run --file <CSV filename>\n\nImporting file:\n\tphp user_upload.php --file <CSV filename> -u <MySQL user> \n\t\t-p <MySQL user password> -h <MySQL hostname>\n\nIf invalid emails are found in CSV then no DB insert will be done.\n\t\t\nOnly above described sets of options could be used. E.g. you cannot use --create_table together with --dry_run \n"; $cmd_options->setHelp($help); //Aligning options and flags to be either set or unset. $csv_file = $cmd_options['file']; if ($cmd_options['create_table']) { $create_table = 1; } // otherwise it is unset if ($cmd_options['dry_run']) { $dry_run = 1; } // otherwise it is unset $mysql_user = $cmd_options['u']; $mysql_user_password = $cmd_options['p']; $mysql_host = $cmd_options['h']; echo "USER INPUT SUMMARY:\n", "Using input csv_file: ", isset($csv_file) ? $csv_file : "No", PHP_EOL, "Create table: ", isset($create_table) ? "Yes" : "No", PHP_EOL, "If dry_run: ", isset($dry_run) ? "Yes" : "No", PHP_EOL, "MySQL DB username: "******"Not specified", PHP_EOL, "MySQL user password: "******"Not specified", PHP_EOL, "MySQL DB host: ", isset($mysql_host) ? $mysql_host : "Not specified", PHP_EOL, PHP_EOL; function create_db_table($user, $password, $host)
#!/usr/bin/env php <?php $autoloads = array(__DIR__ . '/../vendor/autoload.php', __DIR__ . '/../../../autoload.php'); foreach ($autoloads as $autoload) { $autoload = str_replace('/', DIRECTORY_SEPARATOR, $autoload); if (file_exists($autoload)) { require_once $autoload; } } /* define cli options */ $command = new \Commando\Command(); $command->setHelp("Usage: {$argv[0]} [OPTIONS] src dest\n\nChange directory structure or class name to fit the standard. \nThis tool scan src dir for class definations and put them to a new dir, other files are left.\nIt does not support namespace yet"); $command->argument()->require()->expectsFile()->title('src')->describedAs('src directory of your code'); $command->argument()->require()->title('dest')->describedAs('dest directory to generate code'); $command->option('s')->aka('standard')->describedAs('naming standard, avalible standards are: psr0, yaf_controller')->must(function ($standard) { $standards = array('psr0', 'yaf_controller'); return in_array($standard, $standards); }); $command->option('i')->aka('input')->describedAs("Regex to match input files, default is '/\\.php\$/'"); $command->option('t')->aka('target')->describedAs('if you want to rename class or method, specify target here: class, method')->must(function ($target) { $targets = array('class', 'method'); return in_array($target, $targets); }); $command->option('p')->aka('pattern')->describedAs('pattern to match your class name or method name'); $command->option('r')->aka('replace')->describedAs('replacement to replace your class or method name'); if ($command['t']) { $command->option('p')->require(); $command->option('r')->require(); } $command->parse(); /* do process */
<?php ini_set('memory_limit', '256M'); date_default_timezone_set('Europe/Zagreb'); require 'vendor/autoload.php'; // FaceBook app config $fb = new Facebook\Facebook(['app_id' => '', 'app_secret' => '', 'default_graph_version' => 'v2.5', 'default_access_token' => '']); // === CLI TOOL SETUP === $cmd = new Commando\Command(); $cmd->setHelp('Gets comments for the latest posts on a Facebook page.' . PHP_EOL . 'Usage: CMD [getTargetPosts|processPosts] -i {FB page ID} -f {filename for results}'); $cmd->option()->require()->describedAs('Which function should I do')->must(function ($function) { $functions = array('getTargetPosts', 'processPosts'); return in_array($function, $functions); }); $cmd->option('i')->aka('fbpageid')->require()->describedAs('ID of the Facebook page'); $cmd->option('f')->aka('filename')->require()->describedAs('File name that the result will be saved in, no extension'); // === APP LOGIC === // Fetches post IDs that will be scraped for comments function getTargetPosts($app, $FBPageID) { $posts = []; $response = $app->get('/' . $FBPageID . '/posts?limit=100'); $response = $response->getGraphEdge(); for ($i = 0; $i < 50; $i++) { foreach ($response as $post) { $posts[] = $post->asArray()['id']; } $response = $app->next($response); } return $posts; }
<?php date_default_timezone_set('UTC'); foreach (array(__DIR__ . '/../../../autoload.php', __DIR__ . '/../../autoload.php', __DIR__ . '/../vendor/autoload.php', __DIR__ . '/vendor/autoload.php') as $file) { if (file_exists($file)) { define('COMPOSER_AUTOLOADER', $file); break; } } try { if (defined('COMPOSER_AUTOLOADER')) { require_once COMPOSER_AUTOLOADER; } else { throw new Exception('Composer autoloader not found!'); } $cmd = new Commando\Command(); $cmd->setHelp('uptize-php cli v0.1.1')->flag('c')->aka('config')->describedAs('Configuration file')->flag('d')->aka('dryRun')->describedAs('Dry run (do not send check data to uptize.me)')->boolean()->flag('k')->aka('key')->describedAs('Machine Key to send check data to uptize.me')->flag('o')->aka('output')->describedAs('Output check data to a file')->flag('v')->aka('verbose')->describedAs('Verbose output')->boolean()->flag('l')->aka('log')->describedAs('Log output')->default(__DIR__ . '/uptize.log'); if (empty($cmd['config'])) { $config = __DIR__ . '/uptize.json'; } else { $config = $cmd['config']; } if (!is_file($config)) { throw new Exception("Config file '{$config}' not found."); } $settings = uptize\Common\Config::loadFile($config); if (empty($settings['checks'])) { throw new Exception('Empty check list on config file.'); } $report = uptize\Agent::run($settings['checks']); if ($cmd['dryRun']) { if (empty($cmd['output'])) {