/** * Register an algorithm * * @param string name * @param lang.XPClass<security.password.Algorithm> impl * @throws lang.IllegalArgumentException in case the given class is not an Algorithm */ public static function setAlgorithm($name, XPClass $impl) { if (!$impl->isSubclassOf('security.password.Algorithm')) { throw new IllegalArgumentException('Given argument is not an Algorithm class (' . xp::stringOf($impl) . ')'); } self::$algorithms[$name] = $impl; }
/** * Register transport implementation for a specific scheme * * @param string scheme * @param lang.XPClass<peer.http.HttpTransport> class */ public static function register($scheme, XPClass $class) { if (!$class->isSubclassOf('peer.http.HttpTransport')) { throw new IllegalArgumentException(sprintf('Given argument must be lang.XPClass<peer.http.HttpTransport>, %s given', $class->toString())); } self::$transports[$scheme] = $class; }
/** * Fetch a qname for a class. * * @param lang.XPClass class * @return var xml.QName or NULL if no mapping exists */ public function qnameFor(XPClass $class) { if (!isset($this->_c2q[$class->getName()])) { return NULL; } return $this->_qnames[$this->_c2q[$class->getName()]]; }
/** * Register an implementation * * @param string algorithm * @param lang.XPClass<security.checksum.MessageDigestImpl> class * @throws lang.IllegalArgumentException */ public static function register($algorithm, XPClass $impl) { if (!$impl->isSubclassOf('security.checksum.MessageDigestImpl')) { throw new IllegalArgumentException('Implementation class must be a security.checksum.MessageDigestImpl'); } self::$implementations[$algorithm] = $impl; }
/** * Main * * @param string[] $args * @return int */ public static function main(array $args) { $command = null; if (empty($args)) { $class = new XPClass(self::class); $source = $class->getClassLoader(); $markdown = $class->getComment(); } else { if ('@' === $args[0][0]) { $resource = substr($args[0], 1); if (null === ($source = ClassLoader::getDefault()->findResource($resource))) { Console::$err->writeLine('No help topic named ', $resource); return 2; } $markdown = $source->getResource($resource); } else { $class = $args[0]; if (null === ($source = ClassLoader::getDefault()->findClass($class))) { Console::$err->writeLine('No class named ', $class); return 2; } $markdown = $source->loadClass($class)->getComment(); } } self::render(Console::$out, $markdown, $source); return 1; }
/** * Tries to get class uri via reflection * * @param lang.XPClass class The class to return the URI for * @return string */ private function uriFor(XPClass $class) { try { $Urimethod = $class->getClassLoader()->getClass()->getMethod('classURI'); $Urimethod->setAccessible(TRUE); return $Urimethod->invoke($class->getClassLoader(), $class->getName()); } catch (Exception $ignored) { return $class->getClassName(); } }
/** * Returns the enumeration members for a given class * * @param lang.XPClass class class object * @return lang.Enum[] * @throws lang.IllegalArgumentException in case the given class is not an enum */ public static function valuesOf(XPClass $class) { if (!$class->isEnum()) { throw new IllegalArgumentException('Argument class must be lang.XPClass<? extends lang.Enum>'); } $r = []; foreach ($class->reflect()->getStaticProperties() as $prop) { $class->isInstance($prop) && ($r[] = $prop); } return $r; }
public static function methodsAnnotatedWith(XPClass $self, $annotation) { $name = substr($annotation, 1); $r = array(); foreach ($self->getMethods() as $method) { if ($method->hasAnnotation($name)) { $r[] = $method; } } return $r; }
/** * Get instance for class * * @param lang.XPClass class * @return remote.server.BeanContainer */ public static function forClass(XPClass $class) { $bc = new self(); $bc->instancePool = new Vector(); $bc->poolClass = $class; // Fetch class' classloader to check for resources configured // for the bean. $cl = $class->getClassLoader(); // Try loading the well known resources, and remember if it exists $bc->configuration['log.ini'] = $cl->providesResource('etc/log.ini'); $bc->configuration['database.ini'] = $cl->providesResource('etc/log.ini'); $bc->configuration['cl'] = $cl; return $bc; }
/** * Compile and then run sourcecode * * @param string source * @return lang.Runnable */ protected function compile($source) { $decl = ' import integrationtests.ArrayExtensions; class FixturePrimitiveExtensionMethodsIntegrationTest·%d implements Runnable { public var run() { %s } }'; $emitter = new V54Emitter(); $task = new CompilationTask(new StringSource(sprintf($decl, $this->counter++, $source), self::$syntax, $this->name), new NullDiagnosticListener(), newinstance(FileManager::class, [$this->getClass()->getClassLoader()], '{ protected $cl; public function __construct($cl) { $this->cl= $cl; } public function findClass($qualified) { return new FileSource($this->cl->getResourceAsStream("net/xp_lang/tests/integration/src/".strtr($qualified, ".", "/").".xp")); } public function write($r, File $target) { // DEBUG $r->writeTo(Console::$out->getStream()); $r->executeWith(array()); // Defines the class } }'), $emitter); $type = $task->run(); return XPClass::forName($type->name())->newInstance(); }
/** * Constructor * * @param string endpoint default 'http://api.google.com/search/beta2' */ public function __construct($endpoint = 'http://api.google.com/search/beta2') { $this->client = SoapDriver::getInstance()->forEndpoint($endpoint, 'urn:GoogleSearch'); $this->client->registerMapping(new QName('urn:GoogleSearch', 'GoogleSearchResult'), XPClass::forName('com.google.soap.search.GoogleSearchResult')); $this->client->registerMapping(new QName('urn:GoogleSearch', 'ResultElement'), XPClass::forName('com.google.soap.search.ResultElement')); $this->client->registerMapping(new QName('urn:GoogleSearch', 'DirectoryCategory'), XPClass::forName('com.google.soap.search.DirectoryCategory')); }
/** * 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 = ClassLoader::registerLoader(new 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 XPClass::forName($class, $cl)->getMethod('main')->invoke(NULL, array($args)); } catch (TargetInvocationException $e) { throw $e->getCause(); } }
/** * Add Portlets * * @param string classname * @param string layout * @return xml.portlet.Portlet */ public function addPortlet($classname, $layout = NULL) { with($portlet = XPClass::forName($classname)->newInstance()); $portlet->setLayout($layout); $this->portlets[] = $portlet; return $portlet; }
public function genericInterface() { $interfaces = XPClass::forName('lang.Object')->getInterfaces(); $this->assertEquals(1, sizeof($interfaces)); $this->assertInstanceOf('lang.XPClass', $interfaces[0]); $this->assertEquals('lang.Generic', $interfaces[0]->getName()); }
/** * Get a type instance for a given name * * @param string name * @return lang.ArrayType * @throws lang.IllegalArgumentException if the given name does not correspond to a wildcard type */ public static function forName($name) { if (false === strpos($name, '?') || false === ($p = strpos($name, '<'))) { throw new IllegalArgumentException('Not a wildcard type: ' . $name); } $base = substr($name, 0, $p); $components = []; for ($args = substr($name, $p + 1, -1) . ',', $o = 0, $brackets = 0, $i = 0, $s = strlen($args); $i < $s; $i++) { if (',' === $args[$i] && 0 === $brackets) { $component = ltrim(substr($args, $o, $i - $o)); if ('?' === $component) { $components[] = Wildcard::$ANY; } else { if (false === strpos($component, '?')) { $components[] = parent::forName($component); } else { $components[] = self::forName($component); } } $o = $i + 1; } else { if ('<' === $args[$i]) { $brackets++; } else { if ('>' === $args[$i]) { $brackets--; } } } } return new self(XPClass::forName($base), $components); }
/** * Matches implementation * * @param var value * @return bool */ public function matches($value) { if (NULL === $value && $this->matchNull) { return TRUE; } return xp::typeof($value) == XPClass::forName($this->type)->getName(); }
/** * Constructor * * @param rdbms.DSN dsn */ public function __construct($dsn) { $this->dsn = $dsn; $this->flags = $dsn->getFlags(); $this->setTimeout($dsn->getProperty('timeout', 0)); // 0 means no timeout // Keep this for BC reasons $observers = $dsn->getProperty('observer', array()); 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 if (XPClass::forName('util.log.BoundLogObserver')->isAssignableFrom($class)) { $this->addObserver($class->getMethod('instanceFor')->invoke(NULL, array($param))); // Otherwise, just use the constructor } else { $this->addObserver($class->newInstance($param)); } } // Time zone handling if ($tz = $dsn->getProperty('timezone', FALSE)) { $this->tz = new TimeZone($tz); } }
/** * 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 XPClass::forName('rdbms.mysqlx.NamedPipe')->newInstance($socket); } else { return XPClass::forName('rdbms.mysqlx.UnixSocket')->newInstance($socket); } }
/** * Get all test cases * * @param lang.XPClass class * @param var[] arguments * @return unittest.TestCase[] */ public function testCasesInClass(XPClass $class, $arguments = NULL) { // Verify we were actually given a testcase class if (!$class->isSubclassOf('unittest.TestCase')) { throw new IllegalArgumentException('Given argument is not a TestCase class (' . xp::stringOf($class) . ')'); } // Add all tests cases $r = array(); foreach ($class->getMethods() as $m) { $m->hasAnnotation('test') && ($r[] = $class->getConstructor()->newInstance(array_merge((array) $m->getName(TRUE), (array) $arguments))); } // Verify we actually added tests by doing this. if (empty($r)) { throw new NoSuchElementException('No tests found in ' . $class->getName()); } return $r; }
static function __static() { self::$handlers[REMOTE_MSG_INIT] = XPClass::forName('remote.server.message.EascInitMessage'); self::$handlers[REMOTE_MSG_LOOKUP] = XPClass::forName('remote.server.message.EascLookupMessage'); self::$handlers[REMOTE_MSG_CALL] = XPClass::forName('remote.server.message.EascCallMessage'); self::$handlers[REMOTE_MSG_VALUE] = XPClass::forName('remote.server.message.EascValueMessage'); self::$handlers[REMOTE_MSG_EXCEPTION] = XPClass::forName('remote.server.message.EascExceptionMessage'); }
/** * 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 XPClass::forName('img.io.IptcSegment')->newInstance($marker, $iptc); } else { return new self($marker, $bytes); } }
public static function initializeClasses() { if (version_compare(PHP_VERSION, '5.3.2', 'lt')) { throw new PrerequisitesNotMetError('Private not supported', NULL, array('PHP 5.3.2')); } self::$fixture = XPClass::forName('net.xp_framework.unittest.reflection.PrivateAccessibilityFixture'); self::$fixtureChild = XPClass::forName('net.xp_framework.unittest.reflection.PrivateAccessibilityFixtureChild'); self::$fixtureCtorChild = XPClass::forName('net.xp_framework.unittest.reflection.PrivateAccessibilityFixtureCtorChild'); }
/** * Returns the enumeration members for a given class * * @param lang.XPClass class class object * @return self[] * @throws lang.IllegalArgumentException in case the given class is not an enum */ public static function valuesOf(XPClass $class) { if (!$class->isEnum()) { throw new IllegalArgumentException('Argument class must be lang.XPClass<? extends lang.Enum>'); } $r = []; if ($class->isSubclassOf(self::class)) { foreach ($class->reflect()->getStaticProperties() as $prop) { $class->isInstance($prop) && ($r[] = $prop); } } else { $t = ClassLoader::defineClass($class->getName() . 'Enum', self::class, []); foreach ($class->reflect()->getMethod('getValues')->invoke(null) as $name => $ordinal) { $r[] = $t->newInstance($ordinal, $name); } } return $r; }
/** * Add a connection listener. Provided for BC reasons. * * @deprecated Use setProtocol() instead! * @param peer.server.ConnectionListener listener * @return peer.server.ConnectionListener the added listener */ public function addListener($listener) { if (!$this->protocol) { $c = XPClass::forName('peer.server.protocol.ListenerWrapperProtocol'); $this->protocol = $c->newInstance(); } $listener->server = $this; $this->protocol->addListener($listener); return $listener; }
/** * 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); // Check for REST API client v2 if (create(new String($u->getPath()))->contains('/rest/api/2')) { return XPClass::forName('com.atlassian.jira.api.protocol.JiraClientRest2Protocol')->newInstance($u); // No suitable protocol found } else { throw new IllegalArgumentException('No suitable client found for ' . $url); } }
/** * Factory method * * @param string name * @return lang.XPClass * @throws lang.IllegalArgumentException */ public static function forName($name) { switch ($name) { case 'dialog': return XPClass::forName('net.xp_framework.unittest.xml.DialogType'); case 'button': return XPClass::forName('net.xp_framework.unittest.xml.ButtonType'); default: throw new IllegalArgumentException('Unknown tag "' . $name . '"'); } }
/** * 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 XPClass::forName('img.io.ExifSegment')->getMethod('read')->invoke(NULL, array($marker, $bytes)); } else { if (0 === strncmp('http://ns.adobe.com/xap/1.0/', $bytes, 28)) { return XPClass::forName('img.io.XMPSegment')->getMethod('read')->invoke(NULL, array($marker, $bytes)); } else { return new self($marker, $bytes); } } }
/** * 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 = XPClass::forName($this->package . '.' . ('state.' . $name . 'State'))->newInstance(); } catch (ClassNotFoundException $e) { throw new 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(XPClass::forName($args[0])->newInstance()); $s->init(); Console::writeLinef('+ Service %s:%d', $s->socket->host, $s->socket->port); $s->service(); Console::writeLine('+ Done'); } catch (Throwable $e) { Console::writeLine('- ', $e->getMessage()); } }
/** * Returns the wrapper class for this primitive * * @deprecated Wrapper types will move to their own library * @see http://en.wikipedia.org/wiki/Wrapper_class * @return lang.XPClass */ public function wrapperClass() { switch ($this) { case self::$STRING: return XPClass::forName('lang.types.String'); case self::$INT: return XPClass::forName('lang.types.Integer'); case self::$DOUBLE: return XPClass::forName('lang.types.Double'); case self::$BOOL: return XPClass::forName('lang.types.Boolean'); } }