function usage() { $f = FsObject::get(base::appPath()); $usage = $f->getDiskUsage(true); $this->assertNotNull($usage); $this->assertEquals(typeof($usage), 'array'); }
/** * 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('}'); }
/** * 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); } }
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); }
/** * 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'); } }
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); } }
/** * 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'); } } }
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; } }
/** * @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; }
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; } }
/** * 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; } } } } }
/** * 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; } } }
/** * 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("[33m@", $this, "[0m"); Console::writeLine("[1mServing ", (new Source($source, new Config($config)))->layout()); Console::writeLine("[36m", str_repeat('═', 72), "[0m"); Console::writeLine(); with($runtime->newInstance($options, 'web', '', []), function ($proc) { $proc->in->close(); Console::writeLine("[33;1m>[0m Server started: [35;4m", $this->url, "[0m (", date('r'), ')'); Console::writeLine(' PID ', $proc->getProcessId(), '; press Ctrl+C to exit'); Console::writeLine(); while (is_string($line = $proc->err->readLine())) { Console::writeLine(" [36m", $line, "[0m"); } }); }
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"); } }
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; }
public function object_type_union_isAssignableFrom_this_class() { $this->assertTrue(Type::$OBJECT->isAssignableFrom(typeof($this))); }
public function type() { $this->assertEquals(typeof($this), (new TypeMirror(self::class))->type()); }
/** * 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)); } } }
public function type() { $this->assertEquals(typeof($this), (new InstanceMirror($this))->type()); }
/** * 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; }
public function thisClass() { $this->assertTrue($this === cast($this, typeof($this))); }
function __construct($font) { if (typeof($font) == "string" && intval($font) == 0) { $this->font = imageloadfont($font); $this->fontfile = $font; } else { $this->font = $font; } }
/** * 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'); }
/** * 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; }
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)); }
/** * 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); }
public function setAlgorithm_throws_an_exception_for_non_algorithms() { PasswordStrength::setAlgorithm('object', typeof($this)); }
/** * 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'); }
public function packages_in_package($package) { $loader = new ResourcesIn(typeof($this)->getClassLoader()); $this->assertEquals(['com/github/mustache/unittest/'], $loader->listing()->package($package)->packages()); }
/** * 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); }