public function genericInterface() { $interfaces = \lang\XPClass::forName('lang.Object')->getInterfaces(); $this->assertEquals(1, sizeof($interfaces)); $this->assertInstanceOf('lang.XPClass', $interfaces[0]); $this->assertEquals('lang.Generic', $interfaces[0]->getName()); }
public function putParameters() { $params = $this->fixture->getClass()->getMethod('put')->getParameters(); $this->assertEquals(2, sizeof($params)); $this->assertEquals(\lang\XPClass::forName('lang.types.String'), $params[0]->getType()); $this->assertEquals(\lang\XPClass::forName('unittest.TestCase'), $params[1]->getType()); }
/** * Compile class from source and return compiled type * * @param string src * @return xp.compiler.types.TypeReflection */ protected function compile($src) { $unique = 'FixtureClassFor' . $this->getClass()->getSimpleName() . ucfirst($this->name); $r = $this->emitter->emit(Syntax::forName('xp')->parse(new MemoryInputStream(sprintf($src, $unique))), $this->scope); $r->executeWith([]); return new TypeReflection(\lang\XPClass::forName($r->type()->name())); }
/** * Constructor * * @param rdbms.DSN dsn */ public function __construct($dsn) { $this->dsn = $dsn; $this->flags = $dsn->getFlags(); if (!$this->dsn->url->hasParam('autoconnect')) { $this->flags |= DB_AUTOCONNECT; } $this->setTimeout($dsn->getProperty('timeout', 0)); // 0 means no timeout // Keep this for BC reasons $observers = $dsn->getProperty('observer', []); if (null !== ($cat = $dsn->getProperty('log'))) { $observers['util.log.LogObserver'] = $cat; } // Add observers foreach ($observers as $observer => $param) { $class = XPClass::forName($observer); // Check if class implements BoundLogObserver: in that case use factory method to acquire // instance. Otherwise, just use the constructor if (XPClass::forName('util.log.BoundLogObserver')->isAssignableFrom($class)) { $this->addObserver($class->getMethod('instanceFor')->invoke(null, [$param])); } else { $this->addObserver($class->newInstance($param)); } } // Time zone handling if ($tz = $dsn->getProperty('timezone', false)) { $this->tz = new TimeZone($tz); } }
public function putParameters() { $params = $this->fixture->getClass()->getMethod('put')->getParameters(); $this->assertEquals(2, sizeof($params)); $this->assertEquals(Primitive::$STRING, $params[0]->getType()); $this->assertEquals(XPClass::forName('unittest.TestCase'), $params[1]->getType()); }
public function given_interface_class_is_implemented() { $class = $this->define(['interfaces' => [XPClass::forName(Runnable::class)]], '{ public function run() { } }'); $this->assertTrue($class->isSubclassOf(Runnable::class)); }
/** * Add Portlets * * @param string classname * @param string layout * @return xml.portlet.Portlet */ public function addPortlet($classname, $layout = null) { with($portlet = \lang\XPClass::forName($classname)->newInstance()); $portlet->setLayout($layout); $this->portlets[] = $portlet; return $portlet; }
/** * Main * * Exitcodes used: * <ul> * <li>127: Archive referenced in -xar [...] does not exist</li> * <li>126: No manifest or manifest does not have a main-class</li> * </ul> * * @see http://tldp.org/LDP/abs/html/exitcodes.html * @param string[] args * @return int */ public static function main(array $args) { // Open archive $f = new File(array_shift($args)); if (!$f->exists()) { Console::$err->writeLine('*** Cannot find archive ' . $f->getURI()); return 127; } // Register class loader $cl = \lang\ClassLoader::registerLoader(new \lang\archive\ArchiveClassLoader(new Archive($f))); if (!$cl->providesResource(self::MANIFEST)) { Console::$err->writeLine('*** Archive ' . $f->getURI() . ' does not have a manifest'); return 126; } // Load manifest $pr = Properties::fromString($cl->getResource(self::MANIFEST)); if (null === ($class = $pr->readString('archive', 'main-class', null))) { Console::$err->writeLine('*** Archive ' . $f->getURI() . '\'s manifest does not have a main class'); return 126; } // Run main() try { return \lang\XPClass::forName($class, $cl)->getMethod('main')->invoke(null, [$args]); } catch (\lang\reflect\TargetInvocationException $e) { throw $e->getCause(); } }
/** * Creates a new instance * * @param string $source * @param xp.scriptlet.Config $config * @throws lang.IllegalArgumentException */ public function __construct($source, Config $config = null) { if ('-' === $source) { $this->layout = new ServeDocumentRootStatically(); } else { if (is_file($source)) { $this->layout = new WebConfiguration(new Properties($source), $config); } else { if (is_dir($source)) { $this->layout = new BasedOnWebroot($source, $config); } else { $name = ltrim($source, ':'); try { $class = XPClass::forName($name); } catch (ClassLoadingException $e) { throw new IllegalArgumentException('Cannot load ' . $name, $e); } if ($class->isSubclassOf('xp.scriptlet.WebLayout')) { if ($class->hasConstructor()) { $this->layout = $class->getConstructor()->newInstance([$config]); } else { $this->layout = $class->newInstance(); } } else { if ($class->isSubclassOf('scriptlet.HttpScriptlet')) { $this->layout = new SingleScriptlet($class->getName(), $config); } else { throw new IllegalArgumentException('Expecting either a scriptlet or a weblayout, ' . $class->getName() . ' given'); } } } } } }
public function type_bound_to_type_provider() { $inject = new Injector(); $provider = new TypeProvider(XPClass::forName(FileSystem::class), $inject); $inject->bind(Storage::class, $provider); $this->assertInstanceOf(FileSystem::class, $inject->get(Storage::class)); }
static function __static() { self::$byName['if'] = XPClass::forName('com.handlebarsjs.IfBlockHelper'); self::$byName['unless'] = XPClass::forName('com.handlebarsjs.UnlessBlockHelper'); self::$byName['with'] = XPClass::forName('com.handlebarsjs.WithBlockHelper'); self::$byName['each'] = XPClass::forName('com.handlebarsjs.EachBlockHelper'); }
public function object_return_type() { $fixture = $this->define('{ public function fixture(): \\lang\\Object { return new \\lang\\Object(); } }'); $this->assertEquals(XPClass::forName('lang.Object'), $this->newFixture($fixture)->methods()->named('fixture')->returns()); }
/** * Matches implementation * * @param var value * @return bool */ public function matches($value) { if (null === $value && $this->matchNull) { return true; } return \xp::typeof($value) == \lang\XPClass::forName($this->type)->getName(); }
/** * Creates a segment instance * * @param string $marker * @param string $bytes * @return self */ public static function read($marker, $bytes) { if (is_array($iptc = iptcparse($bytes))) { return \lang\XPClass::forName('img.io.IptcSegment')->newInstance($marker, $iptc); } else { return new self($marker, $bytes); } }
public function interfaceImplemented() { $class = \lang\XPClass::forName('de.thekid.util.ObjectComparator'); $interfaces = $class->getInterfaces(); $this->assertEquals(2, sizeof($interfaces)); $this->assertEquals('lang.Generic', $interfaces[0]->getName()); $this->assertEquals('de.thekid.util.Comparator', $interfaces[1]->getName()); }
public function returnNewObjectViaMethodInvoke() { $class = \lang\XPClass::forName('net.xp_framework.unittest.core.AnonymousFactory'); $factory = $class->getMethod('factory'); $object = $factory->invoke($instance = NULL); $value = ReferencesTest::registry('list', $r = NULL); $this->assertReference($object, $value); }
/** * Returns the implementation for the given operating system. * * @param string os operating system name, e.g. PHP_OS * @param string socket default NULL * @return rdbms.mysqlx.LocalSocket */ public static function forName($os, $socket = null) { if (0 === strncasecmp($os, 'Win', 3)) { return \lang\XPClass::forName('rdbms.mysqlx.NamedPipe')->newInstance($socket); } else { return \lang\XPClass::forName('rdbms.mysqlx.UnixSocket')->newInstance($socket); } }
public static function initializeClasses() { if (version_compare(PHP_VERSION, '5.3.2', 'lt')) { throw new \unittest\PrerequisitesNotMetError('Private not supported', null, array('PHP 5.3.2')); } self::$fixture = \lang\XPClass::forName('net.xp_framework.unittest.reflection.PrivateAccessibilityFixture'); self::$fixtureChild = \lang\XPClass::forName('net.xp_framework.unittest.reflection.PrivateAccessibilityFixtureChild'); self::$fixtureCtorChild = \lang\XPClass::forName('net.xp_framework.unittest.reflection.PrivateAccessibilityFixtureCtorChild'); }
/** * Create client by inspecting the URL * * @param string url The API url * @return com.atlassian.jira.api.JiraClientProtocol */ public static function forURL($url) { $u = new URL($url); if (strstr($u->getPath(), '/rest/api/2')) { return XPClass::forName('com.atlassian.jira.api.protocol.JiraClientRest2Protocol')->newInstance($u); } else { throw new IllegalArgumentException('No suitable client found for ' . $url); } }
public function searchImplementation() { // Should not be found $this->register('tests', \lang\XPClass::forName('net.xp_framework.unittest.rdbms.mock.MockConnection')); // Should choose the "a" implementation $this->register('test+a', \lang\ClassLoader::defineClass('net.xp_framework.unittest.rdbms.mock.AMockConnection', 'net.xp_framework.unittest.rdbms.mock.MockConnection', array(), '{}')); $this->register('test+b', \lang\ClassLoader::defineClass('net.xp_framework.unittest.rdbms.mock.BMockConnection', 'net.xp_framework.unittest.rdbms.mock.MockConnection', array(), '{}')); $this->assertInstanceOf('net.xp_framework.unittest.rdbms.mock.AMockConnection', DriverManager::getConnection('test://localhost')); }
public function invocation_order_with_class_annotation() { $this->suite->addTestClass(XPClass::forName('unittest.tests.TestWithAction')); $r = $this->suite->run(); $result = []; foreach ($r->succeeded as $outcome) { $result = array_merge($result, $outcome->test->run); } $this->assertEquals(['before', 'one', 'after', 'before', 'two', 'after'], $result); }
/** * Factory method * * @param string $id * @return lang.XPClass * @throws lang.IllegalArgumentException */ public static function forName($id) { switch ($id) { case 'dialog': return \lang\XPClass::forName('xml.unittest.DialogType'); case 'button': return \lang\XPClass::forName('xml.unittest.ButtonType'); default: throw new \lang\IllegalArgumentException('Unknown attribute "' . $id . '"'); } }
/** * Factory method * * @param string $name * @return lang.XPClass * @throws lang.IllegalArgumentException */ public static function forName($name) { switch ($name) { case 'dialog': return \lang\XPClass::forName('net.xp_framework.unittest.xml.DialogType'); case 'button': return \lang\XPClass::forName('net.xp_framework.unittest.xml.ButtonType'); default: throw new \lang\IllegalArgumentException('Unknown tag "' . $name . '"'); } }
public function complex() { $class = self::define('class', $this->name, null, '{ public static Generic newInstance(XPClass $class= Object::class) { return $class.newInstance(); } }'); with($i = $class->getMethod('newInstance')); $this->assertInstanceOf(Object::class, $i->invoke(null, [])); $this->assertInstanceOf(Date::class, $i->invoke(null, [XPClass::forName('util.Date')])); }
/** * Provide tests to test suite * * @param unittest.TestSuite $suite * @param var[] $arguments * @return void */ public function provideTo($suite, $arguments) { $section = $this->properties->getFirstSection(); do { if ('this' === $section) { continue; } // Ignore special section $suite->addTestClass(XPClass::forName($this->properties->readString($section, 'class')), $arguments ?: $this->properties->readArray($section, 'args')); } while ($section = $this->properties->getNextSection()); }
/** * Sets a context * * @param string|webservices.rest.srv.RestContext context */ public function setContext($context) { if ($context instanceof RestContext) { $this->context = $context; } else { if ('' === (string) $context) { $this->context = new RestContext(); } else { $this->context = XPClass::forName($context)->newInstance(); } } }
/** * Creates a segment instance * * @param string $marker * @param string $bytes * @return self */ public static function read($marker, $bytes) { if (0 === strncmp('Exif', $bytes, 4)) { return \lang\XPClass::forName('img.io.ExifSegment')->getMethod('read')->invoke(null, [$marker, $bytes]); } else { if (0 === strncmp('http://ns.adobe.com/xap/1.0/', $bytes, 28)) { return \lang\XPClass::forName('img.io.XMPSegment')->getMethod('read')->invoke(null, [$marker, $bytes]); } else { return new self($marker, $bytes); } } }
/** * Main runner method * * @param string[] args */ public static function main(array $args) { // Parse args $api = new RestClient('http://builds.planet-xp.net/'); $action = null; $cat = null; for ($i = 0, $s = sizeof($args); $i < $s; $i++) { if ('-?' === $args[$i] || '--help' === $args[$i]) { break; } else { if ('-a' === $args[$i]) { $api->setBase($args[++$i]); } else { if ('-v' === $args[$i]) { $cat = create(new LogCategory('console'))->withAppender(new ColoredConsoleAppender()); } else { if ('-' === $args[$i][0]) { Console::$err->writeLine('*** Unknown argument ', $args[$i]); return 128; } else { $action = $args[$i]; // First non-option is the action name break; } } } } } if (null === $action) { Console::$err->writeLine(self::textOf(\lang\XPClass::forName(\xp::nameOf(__CLASS__))->getComment())); return 1; } try { $class = \lang\reflect\Package::forName('xp.install')->loadClass(ucfirst($action) . 'Action'); } catch (\lang\ClassNotFoundException $e) { Console::$err->writeLine('*** No such action "' . $action . '"'); return 2; } // Show help if (in_array('-?', $args) || in_array('--help', $args)) { Console::$out->writeLine(self::textOf($class->getComment())); return 3; } // Perform action $instance = $class->newInstance($api); $instance->setTrace($cat); try { return $instance->perform(array_slice($args, $i + 1)); } catch (\lang\Throwable $e) { Console::$err->writeLine('*** Error performing action ~ ', $e); return 1; } }
/** * Initialize this request object - overridden from base class. * * @see xp://scriptlet.xml.XMLScriptletRequest#initialize */ public function initialize() { parent::initialize(); if ($this->stateName) { $name = implode('', array_map('ucfirst', array_reverse(explode('/', $this->stateName)))); try { $this->state = \lang\XPClass::forName($this->package . '.' . ('state.' . $name . 'State'))->newInstance(); } catch (\lang\ClassNotFoundException $e) { throw new \scriptlet\ScriptletException('Cannot find ' . $this->stateName, HttpConstants::STATUS_NOT_FOUND, $e); } } }
/** * 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()); } }