Exemple #1
1
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], '.', '\\');
    }
}
    public static function dummyConnectionClass()
    {
        self::$conn = \lang\ClassLoader::defineClass('RestClientExecutionTest_Connection', 'peer.http.HttpConnection', array(), '{
      protected $result= NULL;
      protected $exception= NULL;

      public function __construct($status, $body, $headers) {
        parent::__construct("http://test");
        if ($status instanceof Throwable) {
          $this->exception= $status;
        } else {
          $this->result= "HTTP/1.1 ".$status."\\r\\n";
          foreach ($headers as $name => $value) {
            $this->result.= $name.": ".$value."\\r\\n";
          }
          $this->result.= "\\r\\n".$body;
        }
      }
      
      public function send(HttpRequest $request) {
        if ($this->exception) {
          throw $this->exception;
        } else {
          return new HttpResponse(new MemoryInputStream($this->result));
        }
      }
    }');
    }
 public static function defineLayout()
 {
     self::$layout = ClassLoader::defineClass(self::class . '_Layout', Object::class, [WebLayout::class], '{
   public function mappedApplications($profile= null) { /* Intentionally empty */ }
   public function staticResources($profile= null) { /* Intentionally empty */ }
 }');
 }
 public static function defineExiterClass()
 {
     self::$exiterClass = \lang\ClassLoader::defineClass('net.xp_framework.unittest.core.Exiter', 'lang.Object', array(), '{
   public function __construct() { throw new SystemExit(0); }
   public static function doExit() { new self(); }
 }');
 }
Exemple #5
0
 /**
  * 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();
     }
 }
    static function __static()
    {
        self::$rewriter = \lang\ClassLoader::defineClass('InliningOptimization··Rewriter', 'xp.compiler.ast.Visitor', [], '{
      protected $replacements;
      protected $protect;

      public function __construct($replacements, $protect) {
        $this->replacements= $replacements;
        $this->protect= $protect;
      }

      protected function visitMethodCall(\\xp\\compiler\\ast\\MethodCallNode $node) {
        if ($node->name === $this->protect) $node->inlined= true;
        return parent::visitMethodCall($node);
      }

      protected function visitStaticMethodCall(\\xp\\compiler\\ast\\StaticMethodCallNode $node) {
        if ($node->name === $this->protect) $node->inlined= true;
        return parent::visitStaticMethodCall($node);
      }

      protected function visitVariable(\\xp\\compiler\\ast\\VariableNode $node) {
        return isset($this->replacements[$node->name])
          ? clone $this->replacements[$node->name]
          : $node
        ;
      } 
    }');
    }
    public static function dummyConnectionClass()
    {
        self::$conn = ClassLoader::defineClass('EndpointExecutionTest_Connection', 'peer.http.HttpConnection', [], '{
      private $result, $exception;

      public function __construct($status, $body, $headers) {
        parent::__construct("http://test");
        if ($status instanceof \\lang\\Throwable) {
          $this->exception= $status;
        } else {
          $this->result= "HTTP/1.1 ".$status."\\r\\n";
          foreach ($headers as $name => $value) {
            $this->result.= $name.": ".$value."\\r\\n";
          }
          $this->result.= "\\r\\n".$body;
        }
      }
      
      public function send(\\peer\\http\\HttpRequest $request) {
        if ($this->exception) {
          throw $this->exception;
        } else {
          return new \\peer\\http\\HttpResponse(new \\io\\streams\\MemoryInputStream($this->result));
        }
      }
    }');
    }
    static function __static()
    {
        // For singletonInstance test
        ClassLoader::defineClass('net.xp_framework.unittest.core.AnonymousSingleton', 'lang.Object', array(), '{
      protected static $instance= NULL;

      static function getInstance() {
        if (!isset(self::$instance)) self::$instance= new AnonymousSingleton();
        return self::$instance;
      }
    }');
        // For returnNewObject and returnNewObjectViaReflection tests
        ClassLoader::defineClass('net.xp_framework.unittest.core.AnonymousList', 'lang.Object', array(), '{
      public function __construct() {
        \\net\\xp_framework\\unittest\\core\\ReferencesTest::registry("list", $this);
      }
    }');
        ClassLoader::defineClass('net.xp_framework.unittest.core.AnonymousFactory', 'lang.Object', array(), '{
      static function factory() {
        return new AnonymousList();
      }
    }');
        ClassLoader::defineClass('net.xp_framework.unittest.core.AnonymousNewInstanceFactory', 'lang.Object', array(), '{
      static function factory() {
        return XPClass::forName("net.xp_framework.unittest.core.AnonymousList")->newInstance();
      }
    }');
    }
 /**
  * Creates a new instance creation fluent interface for a given class
  *
  * @param  lang.mirrors.TypeMirror|lang.XPClass|string $type
  * @return lang.XPClass
  */
 public static final function typeOf($type)
 {
     $mirror = $type instanceof TypeMirror ? $type : new TypeMirror($type);
     $type = $mirror->name();
     if (!isset(self::$creations[$type])) {
         if (!$mirror->kind()->isClass() || $mirror->modifiers()->isAbstract()) {
             throw new IllegalArgumentException('Class ' . $type . ' is not instantiable');
         }
         $constructor = $mirror->constructor();
         if (!$constructor->present()) {
             throw new IllegalArgumentException('Class ' . $type . ' does not have a constructor');
         }
         $setters = $args = '';
         foreach ($constructor->parameters() as $parameter) {
             $name = $parameter->name();
             if ($parameter->isOptional()) {
                 $setters .= 'public $' . $name . '= ' . var_export($parameter->defaultValue(), true) . ';';
             } else {
                 $setters .= 'public $' . $name . ';';
             }
             $setters .= "/**\n * @param " . $parameter->type() . "\n * @return self\n*/";
             $setters .= 'public function ' . $name . '($value) { $this->' . $name . '= $value; return $this; }';
             $args .= ', $this->' . $name;
         }
         self::$creations[$type] = ClassLoader::defineClass($type . 'Creation', 'lang.partial.InstanceCreation', [], '{
     /** @return ' . $mirror->name() . ' */
     public function create() { return new \\' . $mirror->reflect->name . '(' . substr($args, 2) . '); }
     ' . $setters . '
   }');
     }
     return self::$creations[$type];
 }
 public static function defineExiterClass()
 {
     self::$exiterClass = ClassLoader::defineClass('net.xp_framework.unittest.core.Exiter', Object::class, [], '{
   public function __construct() { throw new \\lang\\SystemExit(0); }
   public static function doExit() { new self(); }
 }');
 }
 public static function defineCommandClass()
 {
     self::$class = ClassLoader::defineClass('util.cmd.unittest.BatchImport', Command::class, [], ['run' => function () {
     }]);
     self::$global = ClassLoader::defineClass('BatchImport', Command::class, [], ['run' => function () {
     }]);
 }
 /**
  * Defines a type
  *
  * @param  string $declaration
  * @param  string[] $extends
  * @return lang.XPClass
  */
 protected function define($declaration, $extends = [Object::class])
 {
     if (!isset(self::$fixtures[$declaration])) {
         $definition = ['kind' => 'class', 'extends' => $extends, 'implements' => [], 'use' => [], 'imports' => [Identity::class => null]];
         self::$fixtures[$declaration] = ClassLoader::defineType(nameof($this) . sizeof(self::$fixtures), $definition, $declaration);
     }
     return self::$fixtures[$declaration];
 }
 /**
  * Defines an anonymous type
  *
  * @param  string $decl Type declaration
  * @param  int $modifiers
  * @return lang.XPClass
  */
 protected function type($decl = null, $modifiers = '')
 {
     if (!isset(self::$fixtures[$decl])) {
         $definition = ['modifiers' => $modifiers, 'kind' => 'class', 'extends' => [Object::class], 'implements' => [], 'use' => [CompareTo::class], 'imports' => [Value::class => null]];
         self::$fixtures[$decl] = ClassLoader::defineType(get_class($this) . sizeof(self::$fixtures), $definition, $decl);
     }
     return self::$fixtures[$decl];
 }
 /**
  * Constructor
  * 
  * @param   lang.ClassLoader classLoader
  */
 public function __construct($classLoader = null)
 {
     if (null === $classLoader) {
         $this->classLoader = ClassLoader::getDefault();
     } else {
         $this->classLoader = $classLoader;
     }
 }
 /**
  * Constructor
  *
  * @param   string bytes method sourcecode
  */
 public function __construct($bytes)
 {
     $name = 'xp.unittest.DynamicallyGeneratedTestCase·' . self::$uniqId++;
     $this->testClass = \lang\ClassLoader::defineClass($name, 'unittest.TestCase', array(), '{
   #[@test] 
   public function run() { ' . $bytes . ' }
 }');
 }
 /**
  * Get all test cases
  *
  * @param   var[] arguments
  * @return  unittest.TestCase[]
  */
 public function testCasesWith($arguments)
 {
     $uri = $this->file->getURI();
     $cl = \lang\ClassLoader::getDefault()->findUri($uri);
     if (is(null, $cl)) {
         throw new IllegalArgumentException('Cannot load class from ' . $this->file->toString());
     }
     return $this->testCasesInClass($cl->loadUri($uri), $arguments);
 }
 /**
  * Creates a fixrture
  *
  * @return  util.PropertyManager
  */
 private function fixture()
 {
     $class = ClassLoader::getDefault()->defineClass('NonSingletonPropertyManager', PropertyManager::class, [], '{
   public static function newInstance() {
     return new self();
   }
 }');
     return $class->getMethod('newInstance')->invoke(null);
 }
 public static function defineResult()
 {
     self::$result = \lang\ClassLoader::defineClass('FileManagerTestEmitterResult', 'lang.Object', ['xp.compiler.emit.EmitterResult'], '{
   protected $type= null;
   public function __construct($name) { $this->type= new \\xp\\compiler\\types\\TypeReference(new \\xp\\compiler\\types\\TypeName($name)); }
   public function type() { return $this->type; }
   public function extension() { return ".test"; }
 }');
 }
Exemple #19
0
 /**
  * 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;
 }
 /**
  * Creates a new class source
  *
  * @param  string $class Dotted fully qualified name
  * @throws lang.ClassFormatException
  */
 public function __construct($class)
 {
     $cl = ClassLoader::getDefault()->findClass($class);
     if ($cl instanceof IClassLoader) {
         $this->tokenize($cl->loadClassBytes($class), $class);
     } else {
         $this->tokens = null;
     }
 }
 /**
  * Find first classloader responsible for a given path
  *
  * @param   string path
  * @return  lang.IClassLoader
  */
 protected function findLoaderFor($path)
 {
     foreach (\lang\ClassLoader::getLoaders() as $cl) {
         if ($cl instanceof \lang\FileSystemClassLoader && 0 === strncmp($cl->path, $path, strlen($cl->path))) {
             return $cl;
         }
     }
     return null;
 }
 /**
  * Constructor
  *
  * @param   io.File file
  * @throws  lang.IllegalArgumentException if the given file does not exist
  */
 public function __construct(File $file)
 {
     $uri = $file->getURI();
     $cl = ClassLoader::getDefault()->findUri($uri);
     if ($cl === null) {
         throw new IllegalArgumentException('File "' . $uri . ($file->exists() ? '" is not in class path' : '" not found'));
     }
     $this->loader = $cl;
     $this->uri = $uri;
 }
Exemple #23
0
 public static function defineCloseableSubclasses()
 {
     self::$closes = ClassLoader::defineClass('_WithTest_C0', Object::class, [Closeable::class], '{
   public $closed= false;
   public function close() { $this->closed= true; }
 }');
     self::$raises = ClassLoader::defineClass('_WithTest_C1', Object::class, [Closeable::class], '{
   public function close() { throw new \\lang\\IllegalArgumentException("Cannot close"); }
 }');
 }
 static function __static()
 {
     self::$fileStreamAdapter = \lang\ClassLoader::defineClass('FileStreamAdapter', 'io.File', array(), '{
   protected $stream= NULL;
   public function __construct($stream) { $this->stream= $stream; }
   public function exists() { return NULL !== $this->stream; }
   public function getURI() { return Streams::readableUri($this->stream); }
   public function getInputStream() { return $this->stream; }
 }');
 }
 public static function defineVisitor()
 {
     self::$visitor = ClassLoader::defineClass('VisitorTest··Visitor', 'xp.compiler.ast.Visitor', [], '{
   public $visited= array();
   public function visitOne($node) {
     $this->visited[]= $node;
     return parent::visitOne($node);
   }
 }');
 }
 static function __static()
 {
     self::$fileStreamAdapter = ClassLoader::defineClass('FileStreamAdapter', File::class, [], '{
   protected $stream= null;
   public function __construct($stream) { $this->stream= $stream; }
   public function exists() { return null !== $this->stream; }
   public function getURI() { return \\io\\streams\\Streams::readableUri($this->stream); }
   public function getInputStream() { return $this->stream; }
 }');
 }
 /**
  * Constructor
  *
  * @param  io.Folder $folder
  * @throws lang.IllegalArgumentException if the given folder does not exist or isn't in class path
  */
 public function __construct(Folder $folder)
 {
     $path = $folder->getURI();
     foreach (ClassLoader::getLoaders() as $cl) {
         if ($cl instanceof FileSystemClassLoader && 0 === strncmp($cl->path, $path, strlen($cl->path))) {
             $this->loader = $cl;
             return;
         }
     }
     throw new IllegalArgumentException($folder->toString() . ($folder->exists() ? ' is not in class path' : ' does not exist'));
 }
    public function map_of_string_to_object()
    {
        \lang\ClassLoader::defineClass('GenericsBCTest_Map', 'lang.Object', array(), '{
      public $__generic;

      public function getClassName() {
        return "Map<".$this->__generic[0].", ".$this->__generic[1].">";
      }
    }');
        $this->assertEquals('Map<String, Object>', create('new GenericsBCTest_Map<String, Object>')->getClassName());
    }
 /**
  * Locate a class
  *
  * @param   string[] packages
  * @param   string name
  * @return  string qualified
  * @throws  lang.ElementNotFoundException
  */
 public function locateClass($packages, $local)
 {
     $cl = ClassLoader::getDefault();
     foreach ($packages as $package) {
         $qualified = $package . '.' . $local;
         if (!$cl->providesClass($qualified) && !$this->manager->findClass($qualified)) {
             continue;
         }
         return $qualified;
     }
     throw new ElementNotFoundException('Could not locate class ' . $local . ' in ' . \xp::stringOf($packages));
 }
Exemple #30
0
 public static function defineCloseableSubclasses()
 {
     self::$closes = ClassLoader::defineClass('_WithTest_C0', Object::class, [Closeable::class], '{
   public $closed= false;
   public function close() { $this->closed= true; }
 }');
     self::$raises = ClassLoader::defineClass('_WithTest_C1', Object::class, [Closeable::class], '{
   private $throwable;
   public function __construct($class) { $this->throwable= $class; }
   public function close() { throw new $this->throwable("Cannot close"); }
 }');
 }