function entry(&$argv) { if (is_file($argv[0])) { if (0 === substr_compare($argv[0], '.class.php', -10)) { $uri = realpath($argv[0]); if (null === ($cl = \lang\ClassLoader::getDefault()->findUri($uri))) { throw new \Exception('Cannot load ' . $uri . ' - not in class path'); } return $cl->loadUri($uri)->literal(); } else { if (0 === substr_compare($argv[0], '.xar', -4)) { $cl = \lang\ClassLoader::registerPath($argv[0]); if (!$cl->providesResource('META-INF/manifest.ini')) { throw new \Exception($cl->toString() . ' does not provide a manifest'); } $manifest = parse_ini_string($cl->getResource('META-INF/manifest.ini')); return strtr($manifest['main-class'], '.', '\\'); } else { array_unshift($argv, 'eval'); return 'xp\\runtime\\Evaluate'; } } } else { return strtr($argv[0], '.', '\\'); } }
/** * Runner method * */ public static function main(array $args) { // Show command usage if invoked without arguments if (!$args) { exit(self::usage(\lang\XPClass::forName(\xp::nameOf(__CLASS__)))); } $root = new RootDoc(); for ($i = 0, $s = sizeof($args); $i < $s; $i++) { if ('-sp' === $args[$i]) { $root->setSourcePath(explode(PATH_SEPARATOR, $args[++$i])); } else { if ('-cp' === $args[$i]) { foreach (explode(PATH_SEPARATOR, $args[++$i]) as $element) { \lang\ClassLoader::registerPath($element); } } else { try { $class = \lang\XPClass::forName($args[$i]); } catch (\lang\ClassNotFoundException $e) { \util\cmd\Console::$err->writeLine('*** ', $e->getMessage()); exit(2); } if (!$class->isSubclassOf('text.doclet.Doclet')) { \util\cmd\Console::$err->writeLine('*** ', $class, ' is not a doclet'); exit(2); } $doclet = $class->newInstance(); $params = new ParamString(array_slice($args, $i)); // Show doclet usage if the command line contains "-?" (at any point). if ($params->exists('help', '?')) { self::usage($class); if ($valid = $doclet->validOptions()) { \util\cmd\Console::$err->writeLine(); \util\cmd\Console::$err->writeLine('Options:'); foreach ($valid as $name => $value) { \util\cmd\Console::$err->writeLine(' * --', $name, OPTION_ONLY == $value ? '' : '=<value>'); } } exit(3); } $root->start($doclet, $params); exit(0); } } } \util\cmd\Console::$err->writeLine('*** No doclet classname given'); exit(1); }
<?php namespace xp; foreach ($bootstrap['files'] as $file) { require $file; } if (class_exists('xp', false)) { foreach ($bootstrap['overlay'] as $path) { \lang\ClassLoader::registerPath($path, true); } foreach ($bootstrap['local'] as $path) { \lang\ClassLoader::registerPath($path); } } else { if (isset($bootstrap['base'])) { $paths = array_merge($bootstrap['overlay'], $bootstrap['core'], $bootstrap['local']); require $bootstrap['base']; } else { $parts = explode(PATH_SEPARATOR . PATH_SEPARATOR, get_include_path()); throw new \Exception(sprintf("Cannot locate xp-framework/core anywhere in {\n modules: %s\n classpath: %s\n}", ($p = rtrim($parts[0], PATH_SEPARATOR)) ? "[{$p}]" : '(empty)', ($p = rtrim($parts[1], PATH_SEPARATOR)) ? "[{$p}]" : '(empty)'), -1); } }
/** * 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; }
/** * Runs suite * * @param string[] args * @return int exitcode */ public function run(array $args) { if (!$args) { return $this->usage(); } // Setup suite $suite = new TestSuite(); // Parse arguments $sources = new Vector(); $listener = TestListeners::$DEFAULT; $arguments = []; $colors = null; try { for ($i = 0, $s = sizeof($args); $i < $s; $i++) { if ('-v' == $args[$i]) { $listener = TestListeners::$VERBOSE; } else { if ('-q' == $args[$i]) { $listener = TestListeners::$QUIET; } else { if ('-cp' == $args[$i]) { foreach (explode(PATH_SEPARATOR, $this->arg($args, ++$i, 'cp')) as $element) { ClassLoader::registerPath($element, null); } } else { if ('-e' == $args[$i]) { $arg = ++$i < $s ? $args[$i] : '-'; if ('-' === $arg) { $sources->add(new EvaluationSource(Streams::readAll($this->in->getStream()))); } else { $sources->add(new EvaluationSource($this->arg($args, $i, 'e'))); } } else { if ('-l' == $args[$i]) { $arg = $this->arg($args, ++$i, 'l'); $class = XPClass::forName(strstr($arg, '.') ? $arg : 'xp.unittest.' . ucfirst($arg) . 'Listener'); $arg = $this->arg($args, ++$i, 'l'); if ('-?' == $arg || '--help' == $arg) { return $this->listenerUsage($class); } $output = $this->streamWriter($arg); $instance = $suite->addListener($class->newInstance($output)); // Get all @arg-annotated methods $options = []; foreach ($class->getMethods() as $method) { if ($method->hasAnnotation('arg')) { $arg = $method->getAnnotation('arg'); if (isset($arg['position'])) { $options[$arg['position']] = $method; } else { $name = isset($arg['name']) ? $arg['name'] : strtolower(preg_replace('/^set/', '', $method->getName())); $short = isset($arg['short']) ? $arg['short'] : $name[0]; $options[$name] = $options[$short] = $method; } } } $option = 0; } else { if ('-o' == $args[$i]) { if (isset($options[$option])) { $name = '#' . ($option + 1); $method = $options[$option]; } else { $name = $this->arg($args, ++$i, 'o'); if (!isset($options[$name])) { $this->err->writeLine('*** Unknown listener argument ' . $name . ' to ' . nameof($instance)); return 2; } $method = $options[$name]; } $option++; if (0 == $method->numParameters()) { $pass = []; } else { $pass = $this->arg($args, ++$i, 'o ' . $name); } try { $method->invoke($instance, $pass); } catch (TargetInvocationException $e) { $this->err->writeLine('*** Error for argument ' . $name . ' to ' . $instance->getClassName() . ': ' . $e->getCause()->toString()); return 2; } } else { if ('-?' == $args[$i] || '--help' == $args[$i]) { return $this->usage(); } else { if ('-a' == $args[$i]) { $arguments[] = $this->arg($args, ++$i, 'a'); } else { if ('-w' == $args[$i]) { $this->arg($args, ++$i, 'w'); } else { if ('--color' == substr($args[$i], 0, 7)) { $remainder = (string) substr($args[$i], 7); if (!array_key_exists($remainder, self::$cmap)) { throw new IllegalArgumentException('Unsupported argument for --color (must be <empty>, "on", "off", "auto" (default))'); } $colors = self::$cmap[$remainder]; } else { if (strstr($args[$i], '.ini')) { $sources->add(new PropertySource(new Properties($args[$i]))); } else { if (strstr($args[$i], \xp::CLASS_FILE_EXT)) { $sources->add(new ClassFileSource(new File($args[$i]))); } else { if (strstr($args[$i], '.**')) { $sources->add(new PackageSource(Package::forName(substr($args[$i], 0, -3)), true)); } else { if (strstr($args[$i], '.*')) { $sources->add(new PackageSource(Package::forName(substr($args[$i], 0, -2)))); } else { if (false !== ($p = strpos($args[$i], '::'))) { $sources->add(new ClassSource(XPClass::forName(substr($args[$i], 0, $p)), substr($args[$i], $p + 2))); } else { if (is_dir($args[$i])) { $sources->add(new FolderSource(new Folder($args[$i]))); } else { $sources->add(new ClassSource(XPClass::forName($args[$i]))); } } } } } } } } } } } } } } } } } } catch (Throwable $e) { $this->err->writeLine('*** ', $e->getMessage()); \xp::gc(); return 2; } if ($sources->isEmpty()) { $this->err->writeLine('*** No tests specified'); return 2; } // Set up suite $l = $suite->addListener($listener->newInstance($this->out)); if ($l instanceof ColorizingListener) { $l->setColor($colors); } foreach ($sources as $source) { try { $tests = $source->testCasesWith($arguments); foreach ($tests as $test) { $suite->addTest($test); } } catch (NoSuchElementException $e) { $this->err->writeLine('*** Warning: ', $e->getMessage()); continue; } catch (IllegalArgumentException $e) { $this->err->writeLine('*** Error: ', $e->getMessage()); return 2; } catch (MethodNotImplementedException $e) { $this->err->writeLine('*** Error: ', $e->getMessage(), ': ', $e->method, '()'); return 2; } } // Run it! if (0 == $suite->numTests()) { return 3; } else { $r = $suite->run(); return $r->failureCount() > 0 ? 1 : 0; } }
public function non_existant() { ClassLoader::registerPath('@@non-existant@@'); }
/** * Main method * * @param util.cmd.ParamString params * @return int */ public function run(ParamString $params, Config $config = null) { // No arguments given - show our own usage if ($params->count < 1) { $this->selfUsage(); return 1; } // Configure properties $config || ($config = new Config()); // Separate runner options from class options for ($offset = 0, $i = 0; $i < $params->count; $i++) { switch ($params->list[$i]) { case '-c': $config->append($params->list[$i + 1]); $offset += 2; $i++; break; case '-cp': ClassLoader::registerPath($params->list[$i + 1], null); $offset += 2; $i++; break; case '-v': $this->verbose = true; $offset += 1; $i++; break; case '-?': $this->selfUsage(); return 1; case '-l': $this->listCommands(); return 1; default: break 2; } } // Sanity check if (!$params->exists($offset)) { self::$err->writeLine('*** Missing classname'); return 1; } // Use default path for config if no sources set if ($config->isEmpty()) { $config->append(is_dir(self::DEFAULT_CONFIG_PATH) ? self::DEFAULT_CONFIG_PATH : '.'); } unset($params->list[-1]); $classparams = new ParamString(array_slice($params->list, $offset + 1)); return $this->runCommand($params->value($offset), $classparams, $config); }
/** * Entry point method * * @param string[] args */ public static function main(array $args) { if (empty($args)) { return self::usage(); } foreach (ClassLoader::getLoaders() as $loader) { if ($loader instanceof JitClassLoader) { ClassLoader::removeLoader($loader); } } // Set up compiler $compiler = new Compiler(); $manager = new FileManager(); $manager->setSourcePaths(\xp::$classpath); // Handle arguments $profiles = ['default']; $emitter = 'php5.5'; $result = function ($success) { return $success ? 0 : 1; }; $files = []; $listener = new DefaultDiagnosticListener(Console::$out); for ($i = 0, $s = sizeof($args); $i < $s; $i++) { if ('-?' === $args[$i] || '--help' === $args[$i]) { return self::usage(); } else { if ('-cp' === $args[$i]) { \lang\ClassLoader::registerPath($args[++$i]); } else { if ('-sp' === $args[$i]) { $manager->addSourcePath($args[++$i]); } else { if ('-v' === $args[$i]) { $listener = new VerboseDiagnosticListener(Console::$out); } else { if ('-q' === $args[$i]) { $listener = new QuietDiagnosticListener(Console::$out); } else { if ('-t' === $args[$i]) { $levels = LogLevel::NONE; foreach (explode(',', $args[++$i]) as $level) { $levels |= LogLevel::named($level); } $compiler->setTrace(create(new LogCategory('xcc'))->withAppender(new ConsoleAppender(), $levels)); } else { if ('-E' === $args[$i]) { $emitter = $args[++$i]; } else { if ('-p' === $args[$i]) { $profiles = explode(',', $args[++$i]); } else { if ('-o' === $args[$i]) { $output = $args[++$i]; $folder = new Folder($output); $folder->exists() || $folder->create(); $manager->setOutput($folder); } else { if ('-N' === $args[$i]) { $dir = $args[++$i]; $manager->addSourcePath($dir); $files = array_merge($files, self::fromFolder($dir, false)); } else { if (is_dir($args[$i])) { $dir = $args[$i]; $manager->addSourcePath($dir); $files = array_merge($files, self::fromFolder($dir, true)); } else { $files[] = new FileSource(new File($args[$i])); } } } } } } } } } } } } // Check if (empty($files)) { Console::$err->writeLine('*** No files given (-? will show usage)'); return 2; } // Setup emitter sscanf($emitter, '%[^0-9]%d.%d', $language, $major, $minor); try { $emit = \lang\XPClass::forName('xp.compiler.emit.Emitter')->cast(Package::forName('xp.compiler.emit')->getPackage($language)->loadClass(($major ? 'V' . $major . $minor : '') . 'Emitter')->newInstance()); } catch (\lang\ClassCastException $e) { Console::$err->writeLine('*** Not an emitter implementation: ', $e->compoundMessage()); return 4; } catch (\lang\IllegalAccessException $e) { Console::$err->writeLine('*** Cannot use emitter named "', $emitter, '": ', $e->compoundMessage()); return 4; } catch (\lang\Throwable $e) { Console::$err->writeLine('*** No emitter named "', $emitter, '": ', $e->compoundMessage()); return 4; } // Load compiler profile configurations try { $reader = new CompilationProfileReader(); foreach ($profiles as $configuration) { $reader->addSource(new Properties('res://xp/compiler/' . $configuration . '.xcp.ini')); } $emit->setProfile($reader->getProfile()); } catch (\lang\Throwable $e) { Console::$err->writeLine('*** Cannot load profile configuration(s) ' . implode(',', $profiles) . ': ' . $e->getMessage()); return 3; } // Compile files and pass return value to result handler return $result($compiler->compile($files, $listener, $manager, $emit), array_slice($args, $i + 1)); }
<?php namespace xp; \lang\ClassLoader::registerPath(__DIR__);
/** * Creates a new class loader based template loader * * @param string|lang.IClassLoader $base A classloader path or instance * @param string[] $extensions File extensions to check, including leading "." */ public function __construct($arg, $extensions = ['.mustache']) { parent::__construct($arg instanceof IClassLoader ? $arg : ClassLoader::registerPath($arg), $extensions); }