Пример #1
0
 /**
  * @depends testConstant
  */
 public function testService()
 {
     /**
      * @var Test $service
      */
     $config = $this->configSkeleton;
     $config[Manager::CFG_TITLE] = 'Dummy configuration for service test.';
     $config[Manager::CFG_SERVICE][Manager::COMMON][self::TEST_SERVICE] = [];
     $serviceCommonConfig =& $config[Manager::CFG_SERVICE][Manager::COMMON][self::TEST_SERVICE];
     // Test 1: Common, failed to provide enough params
     try {
         $manager = Manager::factory($config, $this->logger);
         $manager->getService(Manager::COMMON, self::TEST_SERVICE);
         $this->fail('There should be an exception on service initialization with missing required configuration item.');
     } catch (\RuntimeException $e) {
         $this->logger->debug('Service required key check OK.');
     }
     // Test 2: Common, default values
     $valueRequired = 'some-value';
     $serviceCommonConfig[Test::CFG_TEST_REQUIRED_KEY] = $valueRequired;
     $manager = Manager::factory($config, $this->logger);
     $service = $manager->getService(Manager::COMMON, self::TEST_SERVICE);
     $fullServiceClass = $manager->nsService . self::TEST_SERVICE;
     $this->assertInstanceOf($fullServiceClass, $service);
     $this->assertSame($valueRequired, $service->testRequired);
     $this->assertSame(Test::DEFAULT_TEST_VALUE, $service->test);
     $this->assertSame(Test::STUB_METHOD_CLOSURE, $service->testProvider());
     // Test 3: Special with overriding Common, static callback provider
     $valueAppRequired = 'app-value-required';
     $valueApp = 'app-value';
     $config[Manager::CFG_SERVICE][self::NS_APP][self::TEST_SERVICE] = [];
     $serviceAppConfig =& $config[Manager::CFG_SERVICE][self::NS_APP][self::TEST_SERVICE];
     $serviceAppConfig[Test::CFG_TEST_REQUIRED_KEY] = $valueAppRequired;
     $serviceAppConfig[Test::CFG_TEST_KEY] = $valueApp;
     $serviceAppConfig[Test::CFG_TEST_PROVIDER] = [$fullServiceClass, 'methodStatic'];
     $manager = Manager::factory($config, $this->logger);
     $service = $manager->getService(self::NS_APP, self::TEST_SERVICE);
     $this->assertSame($valueAppRequired, $service->testRequired);
     $this->assertSame($valueApp, $service->test);
     $this->assertSame(Test::STUB_METHOD_STATIC, $service->testProvider());
     // Test 4: Object invoke callback
     $valueInvokeRequired = 'invoke-value-required';
     $serviceAppConfig[Test::CFG_TEST_PROVIDER] = [Manager::RES_CONFIG_CLASS => $fullServiceClass, Test::CFG_TEST_REQUIRED_KEY => $valueInvokeRequired];
     $manager = Manager::factory($config, $this->logger);
     $service = $manager->getService(self::NS_APP, self::TEST_SERVICE);
     $this->assertSame(Test::STUB_METHOD_INVOKE . $valueInvokeRequired, $service->testProvider());
     // Test 5: Object method callback
     $valueCallbackRequired = 'callback-value-required';
     $serviceAppConfig[Test::CFG_TEST_PROVIDER][Manager::RES_CONFIG_METHOD] = 'method';
     $serviceAppConfig[Test::CFG_TEST_PROVIDER][Test::CFG_TEST_REQUIRED_KEY] = $valueCallbackRequired;
     $manager = Manager::factory($config, $this->logger);
     $service = $manager->getService(self::NS_APP, self::TEST_SERVICE);
     $this->assertSame(Test::STUB_METHOD . $valueCallbackRequired, $service->testProvider());
 }
Пример #2
0
 public static function factory(array $config, Logger $logger)
 {
     foreach ([self::CFG_ACTIONS, self::CFG_SERVICE, self::CFG_CONST, self::CFG_COMPONENTS, self::CFG_TITLE, self::CFG_VERSION] as $cfg_key) {
         if (!isset($config[$cfg_key])) {
             throw new \InvalidArgumentException("Error: Missing mandatory configuration item '{$cfg_key}'.");
         }
     }
     $logger->info("Loading script '%s' version %s.", [$config[self::CFG_TITLE], $config[self::CFG_VERSION]]);
     return new self($config, $logger);
 }
Пример #3
0
 public function execFormat($cmd, ...$params)
 {
     $this->logger->debug("Building command %s with %s", [$cmd, join(',', $params)]);
     $cmd = vsprintf($cmd, array_map('escapeshellarg', $params));
     $this->exec($cmd);
 }
Пример #4
0
if (!isset($options['v'])) {
    $options['v'] = null;
}
if (!isset($options['verbose'])) {
    $options['verbose'] = null;
}
if ($options['v'] === 'vv' || strcasecmp($options['verbose'], 'debug') === 0) {
    $log_level = Logger::DEBUG;
} elseif ($options['v'] === 'v' || strcasecmp($options['verbose'], 'info') === 0) {
    $log_level = Logger::INFO;
} elseif ($options['v'] === false || $options['verbose'] === false) {
    $log_level = Logger::NOTICE;
} else {
    $log_level = Logger::WARNING;
}
$logger = new Logger('main');
$logger->pushHandler(new StreamHandler(sprintf('%s/log/%s.log', __DIR__, date('Ymd-His')), min($log_level, Logger::INFO)));
$logger->pushHandler(new StreamHandler('php://output', $log_level));
try {
    if (!isset($argv[++$opt_num])) {
        throw new \InvalidArgumentException(sprintf("Usage: %s [-v[v|vv]|--verbose [debug|info]] CONFIG.json", $argv[0]));
    }
    $config_file = $argv[$opt_num];
    if (!is_readable($config_file)) {
        throw new \RuntimeException("Config file is not readable.");
    }
    $config = json_decode(file_get_contents($config_file), true);
    if (!is_array($config)) {
        throw new \InvalidArgumentException('Config file is not valid JSON: ' . json_last_error_msg());
    }
    exit(Manager::run($config, $logger));