Exemplo n.º 1
0
 function usage()
 {
     $f = FsObject::get(base::appPath());
     $usage = $f->getDiskUsage(true);
     $this->assertNotNull($usage);
     $this->assertEquals(typeof($usage), 'array');
 }
Exemplo n.º 2
0
 /**
  * Display information
  *
  * @param  io.StringWriter $out
  * @return void
  */
 public function display($out)
 {
     $out->write(self::declarationOf($this->mirror));
     $parent = $this->mirror->parent();
     if ('lang.Enum' !== $parent->name()) {
         $this->displayExtensions([$parent], $out, 'extends');
     }
     $this->displayExtensions($this->mirror->interfaces()->declared(), $out, 'implements');
     $separator = false;
     $out->writeLine(' {');
     $this->displayMembers($this->mirror->constants(), $out, $separator);
     foreach (Enum::valuesOf($this->mirror->type()) as $member) {
         $out->write('  ', $member->name(), '(', $member->ordinal(), ')');
         $mirror = new TypeMirror(typeof($member));
         if ($mirror->isSubtypeOf($this->mirror)) {
             $out->writeLine(' {');
             foreach ($mirror->methods()->declared() as $method) {
                 $out->writeLine('    ', (string) $method);
             }
             $separator = true;
             $out->writeLine('  }');
         } else {
             $out->writeLine();
             $separator = true;
         }
     }
     $constructor = $this->mirror->constructor();
     if ($constructor->present()) {
         $this->displayMembers([$constructor], $out, $separator);
     }
     $this->displayMembers($this->mirror->methods()->all(Methods::ofClass()), $out, $separator);
     $this->displayMembers($this->mirror->methods()->all(Methods::ofInstance()), $out, $separator);
     $out->writeLine('}');
 }
Exemplo n.º 3
0
 /**
  * Creates a new injector optionally given initial bindings
  *
  * @param  inject.Bindings... $initial
  */
 public function __construct(...$initial)
 {
     $this->bind(typeof($this), $this);
     foreach ($initial as $bindings) {
         $this->add($bindings);
     }
 }
Exemplo n.º 4
0
 public function creating_instances_invokes_constructor()
 {
     $fixture = newinstance(Object::class, [], '{
   public $passed= null;
   public function __construct(... $args) { $this->passed= $args; }
 }');
     $this->assertEquals([1, 2, 3], (new Constructor(new TypeMirror(typeof($fixture))))->newInstance(1, 2, 3)->passed);
 }
Exemplo n.º 5
0
 /**
  * Creates a new instance mirror
  *
  * @param  var $value
  */
 public function __construct($value)
 {
     if (is_object($value)) {
         // Parent constructor inlined
         $this->reflect = Sources::$REFLECTION->reflect(new \ReflectionObject($value));
     } else {
         throw new IllegalArgumentException('Given value is not an object, ' . typeof($value) . ' given');
     }
 }
Exemplo n.º 6
0
 public function __construct($streamuri, $mode = 'r', $context = null)
 {
     $this->uri = $streamuri;
     if (typeof($context) == 'StreamContext') {
         $ctx = $context->getContext();
     } else {
         $ctx = $context;
     }
     if ($ctx) {
         $this->fh = fopen($streamuri, $mode, null, $ctx);
     } else {
         $this->fh = fopen($streamuri, $mode);
     }
 }
Exemplo n.º 7
0
 /**
  * Constructor
  *
  * @param   string $arg Either a name or the file descriptor
  */
 public function __construct($arg)
 {
     if ('stdin' === $arg || 'input' === $arg) {
         if (!($this->fd = fopen('php://' . $arg, 'rb'))) {
             throw new IOException('Could not open ' . $arg . ' channel for reading');
         }
     } else {
         if (is_resource($arg)) {
             $this->fd = $arg;
             $this->name = '#' . (int) $arg;
         } else {
             throw new IOException('Expecting either stdin, input or a file descriptor ' . typeof($arg) . ' given');
         }
     }
 }
Exemplo n.º 8
0
 public static function toString($var)
 {
     $type = typeof($var);
     switch ($type) {
         case 'boolean':
             if ($var == true) {
                 return "true";
             } else {
                 return "false";
             }
             break;
         default:
             return strval($var);
             break;
     }
 }
Exemplo n.º 9
0
 /**
  * @brief Return the CSS code for the rule
  *
  * @return String The CSS code for the rule
  */
 function __toString()
 {
     $rules = array();
     foreach ($this->attributes as $key => $rule) {
         if (typeof($rule) == 'array') {
             foreach ($rule as $rulesub) {
                 $rules[] = $key . ':' . $rulesub . ';';
             }
         } else {
             $rules[] = $key . ':' . $rule . ';';
         }
     }
     $rulestr = '{' . join(' ', $rules) . '}';
     $ret = $this->selector . $rulestr;
     return $ret;
 }
Exemplo n.º 10
0
function string($n)
{
    if (js()) {
        if (typeof($n) === "number") {
            return Number($n) . toString();
        } else {
            if (typeof($n) === "undefined") {
                return "";
            } else {
                return $n . toString();
            }
        }
    } else {
        return "" . $n;
    }
}
Exemplo n.º 11
0
 /**
  * Helper method to retrieve a pointer inside a given data structure
  * using a given segment. Returns null if there is no such segment.
  *
  * @param  var $ptr
  * @param  string $segment
  * @return var
  */
 protected function pointer($ptr, $segment)
 {
     if ($ptr instanceof \ArrayAccess) {
         return $ptr->offsetExists($segment) ? $ptr->offsetGet($segment) : null;
     } else {
         if (is_object($ptr)) {
             $class = typeof($ptr);
             // 1. Try public field named <segment>
             if ($class->hasField($segment)) {
                 $field = $class->getField($segment);
                 if ($field->getModifiers() & MODIFIER_PUBLIC) {
                     return $field->get($ptr);
                 }
             }
             // 2. Try public method named <segment>
             if ($class->hasMethod($segment)) {
                 $method = $class->getMethod($segment);
                 if ($method->getModifiers() & MODIFIER_PUBLIC) {
                     return $method->invoke($ptr);
                 }
             }
             // 3. Try accessor named get<segment>()
             if ($class->hasMethod($getter = 'get' . $segment)) {
                 $method = $class->getMethod($getter);
                 if ($method->getModifiers() & MODIFIER_PUBLIC) {
                     return $method->invoke($ptr);
                 }
             }
             // Non applicable - give up
             return null;
         } else {
             if (isset($ptr[$segment])) {
                 return $ptr[$segment];
             } else {
                 if ('length' === $segment) {
                     return sizeof($ptr);
                 } else {
                     return null;
                 }
             }
         }
     }
 }
Exemplo n.º 12
0
 /**
  * Returns helper
  *
  * @param  var $ptr
  * @param  string $segment
  * @return var
  */
 protected function helper($ptr, $segment)
 {
     if ($ptr instanceof \Closure) {
         return $ptr;
     } else {
         if (is_object($ptr)) {
             $class = typeof($ptr);
             if ($class->hasMethod($segment)) {
                 $method = $class->getMethod($segment);
                 return function ($in, $ctx, $options) use($ptr, $method) {
                     return $method->invoke($ptr, [$in, $ctx, $options]);
                 };
             }
             return null;
         } else {
             return isset($ptr[$segment]) ? $ptr[$segment] : null;
         }
     }
 }
Exemplo n.º 13
0
 /**
  * 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)
 {
     $runtime = Runtime::getInstance();
     $startup = $runtime->startupOptions();
     $backing = typeof($startup)->getField('backing')->setAccessible(true)->get($startup);
     // PHP doesn't start with a nonexistant document root
     if (!$docroot->exists()) {
         $docroot = getcwd();
     }
     // Start `php -S`, the development webserver
     $arguments = ['-S', $this->host . ':' . $this->port, '-t', $docroot];
     $options = newinstance(RuntimeOptions::class, [$backing], ['asArguments' => function () use($arguments) {
         return array_merge($arguments, parent::asArguments());
     }]);
     $options->withSetting('user_dir', $source . PATH_SEPARATOR . implode(PATH_SEPARATOR, $config));
     // Pass classpath (TODO: This is fixed in XP 7.6.0, remove once
     // this becomes minimum dependency)
     $cp = [];
     foreach (ClassLoader::getLoaders() as $delegate) {
         if ($delegate instanceof FileSystemClassLoader || $delegate instanceof ArchiveClassLoader) {
             $cp[] = $delegate->path;
         }
     }
     set_include_path('');
     $options->withClassPath($cp);
     // Export environment
     putenv('DOCUMENT_ROOT=' . $docroot);
     putenv('WEB_ROOT=' . $webroot);
     putenv('SERVER_PROFILE=' . $profile);
     Console::writeLine("@", $this, "");
     Console::writeLine("Serving ", (new Source($source, new Config($config)))->layout());
     Console::writeLine("", str_repeat('═', 72), "");
     Console::writeLine();
     with($runtime->newInstance($options, 'web', '', []), function ($proc) {
         $proc->in->close();
         Console::writeLine("> Server started: ", $this->url, " (", date('r'), ')');
         Console::writeLine('  PID ', $proc->getProcessId(), '; press Ctrl+C to exit');
         Console::writeLine();
         while (is_string($line = $proc->err->readLine())) {
             Console::writeLine("  ", $line, "");
         }
     });
 }
Exemplo n.º 14
0
 protected function printValue($key, $val, $inst = 0)
 {
     $defs = $this->conf->getDefs($key);
     if (arr::hasKey($defs, 'vartype')) {
         $typeset = $defs['vartype'];
     } else {
         $typeset = typeof($val);
     }
     if ($inst == 0) {
         printf(__astr("'\\b{%s}' = \\g{%s(}"), $key, $typeset);
     }
     if (typeof($val) == 'array') {
         foreach ($val as $k => $v) {
             $this->printValue($k, $v, $inst + 1);
         }
     } else {
         switch ($typeset) {
             case 'boolean':
                 if ($val) {
                     printf(__astr("\\c{ltcyan true}"));
                 } else {
                     printf(__astr("(\\c{cyan false})"));
                 }
                 break;
             case 'NULL':
                 printf(__astr("\\c{red NULL}"));
                 break;
             case 'integer':
                 printf(__astr("\\c{cyan %d}"), $val);
                 break;
             case 'float':
                 printf(__astr("\\c{cyan %.2f}"), $val);
                 break;
             default:
                 printf(__astr("\\c{yellow '%s'}"), $val);
         }
         if ($inst == 0) {
             printf(__astr("\\g{)} "));
         }
         printf(__astr("  \\c{ltgreen //} \\c{green %s}"), $defs['description']);
         printf("\n");
     }
 }
Exemplo n.º 15
0
 function comment($comment, $entities = [], $args = array())
 {
     $_comment = '';
     if (is_array($comment)) {
         $entities = [];
         foreach ($comment as $element) {
             if (typeof($element) == 'string') {
                 $_comment .= $element;
             } else {
                 $entity = new \stdClass();
                 $entity->id = $element->id;
                 $entity->range = [strlen($_comment), strlen($_comment) + strlen($element->name)];
                 $entity->type = 'mention';
                 $entity->title = $element->name;
                 $_comment .= $element->name + ' ';
                 $entities[] = $entity;
             }
         }
     } else {
         $_comment = $comment;
     }
     $args['post_id'] = $this->id;
     $args['comment'] = $_comment;
     $args['entities'] = $entities;
     $post = $this->api->comment($args);
     $post->post = $this;
     return $post;
 }
Exemplo n.º 16
0
 public function object_type_union_isAssignableFrom_this_class()
 {
     $this->assertTrue(Type::$OBJECT->isAssignableFrom(typeof($this)));
 }
Exemplo n.º 17
0
 public function type()
 {
     $this->assertEquals(typeof($this), (new TypeMirror(self::class))->type());
 }
Exemplo n.º 18
0
 /**
  * Handle routing item
  *
  * @param  lang.Oject instance
  * @param  lang.reflect.Method method
  * @param  var[] args
  * @param  webservices.rest.srv.RestContext context
  * @return webservices.rest.srv.Response
  */
 public function handle($instance, $method, $args)
 {
     foreach ($args as $i => $arg) {
         $args[$i] = $this->unmarshal(typeof($arg), $arg);
     }
     // HACK: Ungeneric XML-related
     $properties = array();
     if ($method->hasAnnotation('xmlfactory', 'element')) {
         $properties['name'] = $method->getAnnotation('xmlfactory', 'element');
     } else {
         if (($class = $method->getDeclaringClass()) && $class->hasAnnotation('xmlfactory', 'element')) {
             $properties['name'] = $class->getAnnotation('xmlfactory', 'element');
         }
     }
     // Invoke the method
     try {
         $result = $method->invoke($instance, $args);
         $this->cat && $this->cat->debug('<-', $result);
     } catch (TargetInvocationException $e) {
         $this->cat && $this->cat->warn('<-', $e);
         return $this->mapException($e->getCause());
     }
     // For "VOID" methods, set status to "no content". If a response is returned,
     // use its status, headers and payload. For any other methods, set status to "OK".
     if (Type::$VOID->equals($method->getReturnType())) {
         return Response::status(HttpConstants::STATUS_NO_CONTENT);
     } else {
         if ($result instanceof webservices·rest·srv·Output) {
             $result->payload = $this->marshal($result->payload, $properties);
             return $result;
         } else {
             return Response::status(HttpConstants::STATUS_OK)->withPayload($this->marshal(new Payload($result), $properties));
         }
     }
 }
Exemplo n.º 19
0
 public function type()
 {
     $this->assertEquals(typeof($this), (new InstanceMirror($this))->type());
 }
Exemplo n.º 20
0
 /**
  * Convert value
  *
  * @param   var value
  * @return  var
  */
 public function marshal($value)
 {
     if ($value instanceof \util\Date) {
         return $value->toString('c');
         // ISO 8601, e.g. "2004-02-12T15:19:21+00:00"
     } else {
         if ($value instanceof \Traversable) {
             return new Iteration($value, [$this, 'marshal']);
         } else {
             if (is_object($value)) {
                 foreach ($this->marshallers->keys() as $t) {
                     // Specific class marshalling
                     if ($t->isInstance($value)) {
                         return $this->marshallers[$t]->marshal($value, $this);
                     }
                 }
                 $class = typeof($value);
                 $r = [];
                 foreach ($class->getFields() as $field) {
                     $m = $field->getModifiers();
                     if ($m & MODIFIER_STATIC) {
                         continue;
                     } else {
                         if ($field->getModifiers() & MODIFIER_PUBLIC) {
                             $r[$field->getName()] = $this->marshal($field->get($value));
                         } else {
                             foreach ($this->variantsOf($field->getName()) as $name) {
                                 if ($class->hasMethod($m = 'get' . $name)) {
                                     $r[$field->getName()] = $this->marshal($class->getMethod($m)->invoke($value));
                                     continue 2;
                                 }
                             }
                         }
                     }
                 }
                 return $r;
             } else {
                 if (is_array($value)) {
                     $r = [];
                     foreach ($value as $key => $val) {
                         $r[$key] = $this->marshal($val);
                     }
                     return $r;
                 }
             }
         }
     }
     return $value;
 }
Exemplo n.º 21
0
 public function thisClass()
 {
     $this->assertTrue($this === cast($this, typeof($this)));
 }
Exemplo n.º 22
0
 function __construct($font)
 {
     if (typeof($font) == "string" && intval($font) == 0) {
         $this->font = imageloadfont($font);
         $this->fontfile = $font;
     } else {
         $this->font = $font;
     }
 }
Exemplo n.º 23
0
 /**
  * Cast a value to this type
  *
  * @param   var value
  * @return  var
  * @throws  lang.ClassCastException
  */
 public function cast($value)
 {
     $t = typeof($value);
     if ($t instanceof XPClass && $this->assignableFromClass($t)) {
         return $value;
     }
     throw new ClassCastException('Cannot cast ' . \xp::typeOf($value) . ' to the ' . $this->getName() . ' type');
 }
Exemplo n.º 24
0
 /**
  * Add an extra arbitrary row onto the end of the browser.
  *
  * @var array $column_values Contains an array of named fields, hopefully matching the column names.
  */
 function AddRow($column_values)
 {
     if (!isset($this->ExtraRows) || typeof($this->ExtraRows) != 'array') {
         $this->ExtraRows = array();
     }
     $this->ExtraRows[] =& $column_values;
 }
Exemplo n.º 25
0
 public function andThen_optimized_for_identity()
 {
     $fixture = function ($val) {
         return 'test';
     };
     $closure = Closure::identity()->andThen($fixture);
     $this->assertEquals($fixture, typeof($closure)->getField('closure')->setAccessible(true)->get($closure));
 }
Exemplo n.º 26
0
 /**
  * Gets a line from the source.
  */
 public function get_line()
 {
     static $get_line;
     if (!$get_line) {
         if (is_array($this->source)) {
             $get_line = function (&$source) {
                 $line = each($source);
                 if (!$line) {
                     return false;
                 } else {
                     return $line[1];
                 }
             };
         } elseif (is_resource($this->source)) {
             $get_line = function (&$source) {
                 if (feof($source)) {
                     return false;
                 } else {
                     return rtrim(fgets($source), "\r\n");
                 }
             };
         } else {
             throw new Exception('Sanity error: unexpected source type - ' . typeof($this->source));
         }
     }
     return $get_line($this->source);
 }
Exemplo n.º 27
0
 public function setAlgorithm_throws_an_exception_for_non_algorithms()
 {
     PasswordStrength::setAlgorithm('object', typeof($this));
 }
Exemplo n.º 28
0
 /**
  * Reads an object
  *
  * @return [:var]
  * @throws lang.FormatException
  */
 protected function readObject($nesting)
 {
     $token = $this->nextToken();
     if ('}' === $token) {
         return [];
     } else {
         if (null !== $token) {
             $result = [];
             if ($nesting++ > $this->maximumNesting) {
                 throw new FormatException('Nesting level too deep');
             }
             do {
                 $key = $this->valueOf($token, $nesting);
                 if (!is_string($key)) {
                     throw new FormatException('Illegal key type ' . typeof($key) . ', expecting string');
                 }
                 if (':' === ($token = $this->nextToken())) {
                     $result[$key] = $this->valueOf($this->nextToken(), $nesting);
                 } else {
                     throw new FormatException('Unexpected token [' . \xp::stringOf($token) . '] reading object, expecting ":"');
                 }
                 $delim = $this->nextToken();
                 if (',' === $delim) {
                     continue;
                 } else {
                     if ('}' === $delim) {
                         return $result;
                     } else {
                         throw new FormatException('Unexpected ' . \xp::stringOf($delim) . ', expecting "," or "}"');
                     }
                 }
             } while ($token = $this->nextToken());
         }
     }
     throw new FormatException('Unclosed object');
 }
Exemplo n.º 29
0
 public function packages_in_package($package)
 {
     $loader = new ResourcesIn(typeof($this)->getClassLoader());
     $this->assertEquals(['com/github/mustache/unittest/'], $loader->listing()->package($package)->packages());
 }
Exemplo n.º 30
0
 /**
  * Get longest edge from boxes
  *
  * @access public
  * @param array $edges Edges to select the longest from
  * @returns array
  */
 function _calc_longest_edge($boxes, $edges = array('length', 'width', 'height'))
 {
     if (!isset($boxes) || !is_array($boxes)) {
         throw new InvalidArgumentException('_calc_longest_edge function requires an array of boxes, ' . typeof($boxes) . ' given');
     }
     // Longest edge
     $le = null;
     // Longest edge
     $lef = null;
     // Edge field (length | width | height) that is longest
     // Get longest edges
     foreach ($boxes as $k => $box) {
         foreach ($edges as $edge) {
             if (array_key_exists($edge, $box) && $box[$edge] > $le) {
                 $le = $box[$edge];
                 $lef = $edge;
             }
         }
     }
     return array('edge_size' => $le, 'edge_name' => $lef);
 }