/** * Start server * * @param string[] args */ public static function main(array $args) { $stor = new TestingStorage(); $stor->add(new TestingCollection('/', $stor)); $stor->add(new TestingCollection('/.trash', $stor)); $stor->add(new TestingElement('/.trash/do-not-remove.txt', $stor)); $stor->add(new TestingCollection('/htdocs', $stor)); $stor->add(new TestingElement('/htdocs/file with whitespaces.html', $stor)); $stor->add(new TestingElement('/htdocs/index.html', $stor, "<html/>\n")); $stor->add(new TestingCollection('/outer', $stor)); $stor->add(new TestingCollection('/outer/inner', $stor)); $stor->add(new TestingElement('/outer/inner/index.html', $stor)); $auth = newinstance('lang.Object', array(), '{ public function authenticate($user, $password) { return ("testtest" == $user.$password); } }'); $protocol = newinstance('peer.ftp.server.FtpProtocol', array($stor, $auth), '{ public function onShutdown($socket, $params) { $this->answer($socket, 200, "Shutting down"); $this->server->terminate= TRUE; } }'); isset($args[0]) && $protocol->setTrace(Logger::getInstance()->getCategory()->withAppender(new FileAppender($args[0]))); $s = new Server('127.0.0.1', 0); try { $s->setProtocol($protocol); $s->init(); Console::writeLinef('+ Service %s:%d', $s->socket->host, $s->socket->port); $s->service(); Console::writeLine('+ Done'); } catch (\lang\Throwable $e) { Console::writeLine('- ', $e->getMessage()); } }
/** * Performs measurement * * @param util.profiling.Measurement $m */ public function run($m) { $m->perform(newinstance('util.profiling.Run', [], ['before' => function ($iteration) { Console::write($iteration->name(), ': '); }, 'after' => function ($result) { Console::writeLinef('%d iteration(s), %.3f seconds, result= %s', $result->iteration()->times(), $result->elapsed(), Objects::stringOf($result->result())); }])); }
/** * Main * * @param string[] args * @return int */ public static function main(array $args) { Console::writeLinef('XP %s { PHP %s & ZE %s } @ %s', \xp::version(), phpversion(), zend_version(), php_uname()); Console::writeLine('Copyright (c) 2001-2015 the XP group'); foreach (\lang\ClassLoader::getLoaders() as $delegate) { Console::writeLine($delegate->toString()); } return 1; }
/** * Start server * * @param string[] args */ public static function main(array $args) { $s = new Server('127.0.0.1', 0); try { $s->setProtocol(\lang\XPClass::forName($args[0])->newInstance()); $s->init(); Console::writeLinef('+ Service %s:%d', $s->socket->host, $s->socket->port); $s->service(); Console::writeLine('+ Done'); } catch (\lang\Throwable $e) { Console::writeLine('- ', $e->getMessage()); } }
/** * Start server * * @param string[] args */ public static function main(array $args) { // Add shutdown message handler \remote\server\message\EascMessageFactory::setHandler(61, newinstance('remote.server.message.EascMessage', array(), '{ public function getType() { return 61; } public function handle($protocol, $data) { Logger::getInstance()->getCategory()->debug("Shutting down"); $protocol->server->terminate= TRUE; } }')->getClass()); $s = new Server('127.0.0.1', 0); try { $protocol = new EascProtocol(newinstance('remote.server.deploy.scan.DeploymentScanner', array(), '{ private $changed= TRUE; public function scanDeployments() { $changed= $this->changed; $this->changed= FALSE; return $changed; } public function getDeployments() { $res= "net/xp_framework/unittest/remote/deploy/beans.test.CalculatorBean.xar"; with ($d= new Deployment($res)); { $d->setClassLoader(new \\lang\\archive\\ArchiveClassLoader(new \\lang\\archive\\Archive(\\lang\\ClassLoader::getDefault()->getResourceAsStream($res)))); $d->setImplementation("beans.test.CalculatorBeanImpl"); $d->setInterface("beans.test.Calculator"); $d->setDirectoryName("xp/test/Calculator"); return array($d); } } }')); $protocol->initialize(); $s->setProtocol($protocol); $s->init(); Console::writeLinef('+ Service %s:%d', $s->socket->host, $s->socket->port); $s->service(); Console::writeLine('+ Done'); } catch (\lang\Throwable $e) { Console::writeLine('- ', $e->getMessage()); } }
/** * Serve requests * * @param string $source * @param string $profile * @param io.Path $webroot * @param io.Path $docroot * @param string[] $config */ public function serve($source, $profile, $webroot, $docroot, $config) { $this->server->init(); $protocol = $this->server->setProtocol(new HttpProtocol(function ($host, $method, $query, $status, $error) { Console::writeLinef(" [33m[%s %d %.3fkB][0m %d %s %s", date('Y-m-d H:i:s'), getmypid(), memory_get_usage() / 1024, $status, $method, $query, $error); })); $pm = PropertyManager::getInstance(); $expand = function ($in) use($webroot, $profile, $docroot) { return is_string($in) ? strtr($in, ['{WEBROOT}' => $webroot, '{PROFILE}' => $profile, '{DOCUMENT_ROOT}' => $docroot]) : $in; }; $layout = (new Source($source, new Config($config, $expand)))->layout(); Console::writeLine("[33m@", $this, "[0m"); Console::writeLine("[1mServing ", $layout); Console::writeLine("[36m", str_repeat('═', 72), "[0m"); Console::writeLine(); $resources = $layout->staticResources($profile); if (null === $resources) { $protocol->setUrlHandler('default', '#^/#', new FileHandler($docroot, $notFound = function () { return null; })); } else { foreach ($resources as $pattern => $location) { $protocol->setUrlHandler('default', '#' . strtr($pattern, ['#' => '\\#']) . '#', new FileHandler($expand($location))); } } foreach ($layout->mappedApplications($profile) as $url => $application) { $protocol->setUrlHandler('default', '/' === $url ? '##' : '#^(' . preg_quote($url, '#') . ')($|/.+)#', new ScriptletHandler($application->scriptlet(), array_map($expand, $application->arguments()), array_map($expand, array_merge($application->environment(), ['DOCUMENT_ROOT' => $docroot])), $application->filters())); foreach ($application->config()->sources() as $s) { $pm->appendSource($s); } } $l = Logger::getInstance(); $pm->hasProperties('log') && $l->configure($pm->getProperties('log')); $cm = ConnectionManager::getInstance(); $pm->hasProperties('database') && $cm->configure($pm->getProperties('database')); Console::writeLine("[33;1m>[0m Server started: [35;4m", $this->url, "[0m (", date('r'), ')'); Console::writeLine(' PID ', getmypid(), '; press Ctrl+C to exit'); Console::writeLine(); $this->server->service(); $this->server->shutdown(); }
/** * Main * * @param string[] $args * @return int */ public static function main(array $args) { if (empty($args)) { Console::writeLinef('XP %s { PHP %s & ZE %s } @ %s', \xp::version(), phpversion(), zend_version(), php_uname()); Console::writeLine('Copyright (c) 2001-2016 the XP group'); foreach (\lang\ClassLoader::getLoaders() as $delegate) { Console::writeLine($delegate->toString()); } return 1; } else { foreach ($args as $arg) { $method = $arg . 'Version'; if (is_callable(['self', $method])) { Console::writeLine(self::$method()); } else { Console::$err->writeLinef('Unkown version argument `%s\'', $arg); } } return 0; } }
/** * Start server * * @param string[] args */ public static function main(array $args) { $protocol = newinstance('peer.server.ServerProtocol', array(), '{ public function initialize() { } public function handleDisconnect($socket) { } public function handleError($socket, $e) { } public function handleConnect($socket) { } public function handleData($socket) { $cmd= $socket->readLine(); switch (substr($cmd, 0, 4)) { case "ECHO": { $socket->write("+ECHO ".substr($cmd, 5)."\\n"); break; } case "LINE": { sscanf(substr($cmd, 5), "%d %s", $l, $sep); for ($i= 0, $sbytes= urldecode($sep); $i < $l; $i++) { $socket->write("+LINE ".$i.$sbytes); } $socket->write("+LINE .\\n"); break; } case "CLOS": { $socket->close(); break; } case "HALT": { $socket->write("+HALT\\n"); $this->server->terminate= TRUE; break; } } } }'); $s = new Server('127.0.0.1', 0); try { $s->setProtocol($protocol); $s->init(); Console::writeLinef('+ Service %s:%d', $s->socket->host, $s->socket->port); $s->service(); Console::writeLine('+ Done'); } catch (\lang\Throwable $e) { Console::writeLine('- ', $e->getMessage()); } }
public function writeLinef_argument_with_end_tag() { $this->assertWritten("[31;1m</>[22;39m\n", function () { Console::writeLinef('<red>%s</>', '</>'); }); }
public function writeLinef() { Console::writeLinef('Hello %s', 'World'); $this->assertEquals("Hello World\n", $this->streams[1]->getBytes()); }