})->must(function ($file) { Ensure::that(!is_dir($file), sprintf('Cannot dump config to %s. It is a directory', $file)); if (is_file($file)) { Ensure::that(is_writable($file), sprintf('Cannot dump config to %s. It is not writable', $file)); } return true; })->describedAs('Dump the config to the the specified file. Defaults to stdout.'); $args->option('k')->aka('new-key')->map(function ($file) { if (null === $file) { return 'php://stdout'; } return $file; })->must(function ($file) { Ensure::that(!is_dir($file), sprintf('Cannot dump config to %s. It is a directory', $file)); if (is_file($file)) { Ensure::that(is_writable($file), sprintf('Cannot dump config to %s. It is not writable', $file)); } return true; })->describedAs('Generate a new key and dump the config to the specified file. Defaults to stdout.'); $args->option('p')->aka('print-report')->boolean()->describedAs('Print the report to stdout instead of sending it to the remote server.'); $args->option('v')->aka('version')->boolean()->describedAs('Get the version number'); return $args; }, 'config.defaults' => require 'resources/config.default.php', 'config' => function ($app) { return Config::fromFileIfExists($app['args']['config'], $app['config.defaults']); }, 'logger' => function ($app) { return new SyslogLogger($app->cfg('logging.minLevel')); }]); $app['http'] = function ($app) { return new Http($app); }; $app['signer'] = function ($app) {
/** * Keep the app running as a daemon. * * The app sends a report every X seconds as defined in the daemon.interval config setting. * * @throws Exceptions\EnsureException if config settings are wrong */ protected function runAsDaemon() { // interval (in seconds) between sending reports. $interval = $this->cfg('daemon.interval'); Ensure::that($interval > $this->cfg('http.timeout'), 'daemon.interval must be larger than http.timeout in order to run as daemon'); // start of the reporting $start = microtime(true); // A practically endless loop // 64 bit signed integers gives us 292,471,208,678 years of runtime with 1-second intervals // 32 bit signed integers gives us 69 years of runtime with 1-second intervals for ($i = 0; true; ++$i) { // The ideal starting time of this iteration $this_iteration_start = $start + $interval * $i; // The ideal starting time of the next iteration $next_iteration_start = $this_iteration_start + $interval; // Send the report. This may take a few seconds. try { $this->sendReport(); } catch (Exception $e) { $this->warning('Could not send report to remote server: {message}', ['message' => $e->getMessage()]); } // Sleep until next iteration should start time_sleep_until($next_iteration_start); } }
/** * Create an instance from a file * * Merge the config data from the file with the defaults. * * @param string $file The filename * @param array $default * @return Config */ public static function fromFile($file, array $defaults = []) { Ensure::fileIsReadable($file); $config = (require $file); return static::fromArray($config, $defaults); }