/** * Main method * * @param util.cmd.ParamString params * @return int */ public function run(ParamString $params) { // No arguments given - show our own usage if ($params->count < 1) { return self::usage(); } // Configure properties $pm = PropertyManager::getInstance(); // Separate runner options from class options for ($offset = 0, $i = 0; $i < $params->count; $i++) { switch ($params->list[$i]) { case '-c': if (0 == strncmp('res://', $params->list[$i + 1], 6)) { $pm->appendSource(new ResourcePropertySource(substr($params->list[$i + 1], 6))); } else { $pm->appendSource(new FilesystemPropertySource($params->list[$i + 1])); } $offset += 2; $i++; break; case '-cp': \lang\ClassLoader::registerPath($params->list[$i + 1], null); $offset += 2; $i++; break; case '-v': $this->verbose = true; $offset += 1; $i++; break; case '-?': return self::usage(); default: break 2; } } // Sanity check if (!$params->exists($offset)) { self::$err->writeLine('*** Missing classname'); return 1; } // Use default path for PropertyManager if no sources set if (!$pm->getSources()) { $pm->configure(self::DEFAULT_CONFIG_PATH); } unset($params->list[-1]); $classname = $params->value($offset); $classparams = new ParamString(array_slice($params->list, $offset + 1)); // Class file or class name if (strstr($classname, \xp::CLASS_FILE_EXT)) { $file = new \io\File($classname); if (!$file->exists()) { self::$err->writeLine('*** Cannot load class from non-existant file ', $classname); return 1; } try { $class = \lang\ClassLoader::getDefault()->loadUri($file->getURI()); } catch (\lang\ClassNotFoundException $e) { self::$err->writeLine('*** ', $this->verbose ? $e : $e->getMessage()); return 1; } } else { try { $class = \lang\XPClass::forName($classname); } catch (\lang\ClassNotFoundException $e) { self::$err->writeLine('*** ', $this->verbose ? $e : $e->getMessage()); return 1; } } // Check whether class is runnable if (!$class->isSubclassOf('lang.Runnable')) { self::$err->writeLine('*** ', $class->getName(), ' is not runnable'); return 1; } // Usage if ($classparams->exists('help', '?')) { self::showUsage($class); return 0; } // Load, instantiate and initialize $l = Logger::getInstance(); $pm->hasProperties('log') && $l->configure($pm->getProperties('log')); if (class_exists('rdbms\\DBConnection')) { // FIXME: Job of XPInjector? $cm = ConnectionManager::getInstance(); $pm->hasProperties('database') && $cm->configure($pm->getProperties('database')); } // Setup logger context for all registered log categories foreach (Logger::getInstance()->getCategories() as $category) { if (null === ($context = $category->getContext()) || !$context instanceof EnvironmentAware) { continue; } $context->setHostname(\lang\System::getProperty('host.name')); $context->setRunner(nameof($this)); $context->setInstance($class->getName()); $context->setResource(null); $context->setParams($params->string); } $instance = $class->newInstance(); $instance->in = self::$in; $instance->out = self::$out; $instance->err = self::$err; $methods = $class->getMethods(); // Injection foreach ($methods as $method) { if (!$method->hasAnnotation('inject')) { continue; } $inject = $method->getAnnotation('inject'); if (isset($inject['type'])) { $type = $inject['type']; } else { if ($restriction = $method->getParameter(0)->getTypeRestriction()) { $type = $restriction->getName(); } else { $type = $method->getParameter(0)->getType()->getName(); } } try { switch ($type) { case 'rdbms.DBConnection': $args = [$cm->getByHost($inject['name'], 0)]; break; case 'util.Properties': $p = $pm->getProperties($inject['name']); // If a PropertyAccess is retrieved which is not a util.Properties, // then, for BC sake, convert it into a util.Properties if ($p instanceof \util\PropertyAccess && !$p instanceof \util\Properties) { $convert = \util\Properties::fromString(''); $section = $p->getFirstSection(); while ($section) { // HACK: Properties::writeSection() would first attempts to // read the whole file, we cannot make use of it. $convert->_data[$section] = $p->readSection($section); $section = $p->getNextSection(); } $args = [$convert]; } else { $args = [$p]; } break; case 'util.log.LogCategory': $args = [$l->getCategory($inject['name'])]; break; default: self::$err->writeLine('*** Unknown injection type "' . $type . '" at method "' . $method->getName() . '"'); return 2; } $method->invoke($instance, $args); } catch (\lang\reflect\TargetInvocationException $e) { self::$err->writeLine('*** Error injecting ' . $type . ' ' . $inject['name'] . ': ' . $e->getCause()->compoundMessage()); return 2; } catch (\lang\Throwable $e) { self::$err->writeLine('*** Error injecting ' . $type . ' ' . $inject['name'] . ': ' . $e->compoundMessage()); return 2; } } // Arguments foreach ($methods as $method) { if ($method->hasAnnotation('args')) { // Pass all arguments if (!$method->hasAnnotation('args', 'select')) { $begin = 0; $end = $classparams->count; $pass = array_slice($classparams->list, 0, $end); } else { $pass = []; foreach (preg_split('/, ?/', $method->getAnnotation('args', 'select')) as $def) { if (is_numeric($def) || '-' == $def[0]) { $pass[] = $classparams->value((int) $def); } else { sscanf($def, '[%d..%d]', $begin, $end); isset($begin) || ($begin = 0); isset($end) || ($end = $classparams->count - 1); while ($begin <= $end) { $pass[] = $classparams->value($begin++); } } } } try { $method->invoke($instance, [$pass]); } catch (\lang\Throwable $e) { self::$err->writeLine('*** Error for arguments ' . $begin . '..' . $end . ': ', $this->verbose ? $e : $e->getMessage()); return 2; } } else { if ($method->hasAnnotation('arg')) { // Pass arguments $arg = $method->getAnnotation('arg'); if (isset($arg['position'])) { $name = '#' . ($arg['position'] + 1); $select = intval($arg['position']); $short = null; } else { if (isset($arg['name'])) { $name = $select = $arg['name']; $short = isset($arg['short']) ? $arg['short'] : null; } else { $name = $select = strtolower(preg_replace('/^set/', '', $method->getName())); $short = isset($arg['short']) ? $arg['short'] : null; } } if (0 == $method->numParameters()) { if (!$classparams->exists($select, $short)) { continue; } $args = []; } else { if (!$classparams->exists($select, $short)) { list($first, ) = $method->getParameters(); if (!$first->isOptional()) { self::$err->writeLine('*** Argument ' . $name . ' does not exist!'); return 2; } $args = []; } else { $args = [$classparams->value($select, $short)]; } } try { $method->invoke($instance, $args); } catch (\lang\reflect\TargetInvocationException $e) { self::$err->writeLine('*** Error for argument ' . $name . ': ', $this->verbose ? $e->getCause() : $e->getCause()->compoundMessage()); return 2; } } } } try { $instance->run(); } catch (\lang\Throwable $t) { self::$err->writeLine('*** ', $t->toString()); return 70; // EX_SOFTWARE according to sysexits.h } return 0; }
public function callingMain() { $temp = System::tempDir(); // Create web.ini in system's temp dir $ini = new \io\File($temp, 'web.ini'); $ini->open(FILE_MODE_WRITE); $ini->write("[app]\n" . "mappings=\"/:welcome\"\n" . "[app::welcome]\n" . "class=undefined\n" . "[app::welcome@dev]\n" . "class=\"" . self::$welcomeScriptlet->getName() . "\"\n"); $ini->close(); // Run ob_start(); Runner::main(array($temp, $temp, 'dev', '/')); $content = ob_get_contents(); ob_end_clean(); $ini->unlink(); // Assert $this->assertEquals('<h1>Welcome, we are open</h1>', $content); }