diff --git a/src/aphront/writeguard/AphrontScopedUnguardedWriteCapability.php b/src/aphront/writeguard/AphrontScopedUnguardedWriteCapability.php index 2121df4..8f01368 100644 --- a/src/aphront/writeguard/AphrontScopedUnguardedWriteCapability.php +++ b/src/aphront/writeguard/AphrontScopedUnguardedWriteCapability.php @@ -1,9 +1,9 @@ getPrevious(); } if (method_exists($ex, 'getPreviousException')) { return $ex->getPreviousException(); } return null; } /** * Find the most deeply nested exception from a possibly-nested exception. * * @param Exception A possibly-nested exception. * @return Exception Deepest exception in the nest. * @task exutil */ public static function getRootException(Exception $ex) { $root = $ex; while (self::getPreviousException($root)) { $root = self::getPreviousException($root); } return $root; } /* -( Trapping Errors )---------------------------------------------------- */ /** * Adds an error trap. Normally you should not invoke this directly; * @{class:PhutilErrorTrap} registers itself on construction. * * @param PhutilErrorTrap Trap to add. * @return void * @task trap */ public static function addErrorTrap(PhutilErrorTrap $trap) { $key = $trap->getTrapKey(); self::$traps[$key] = $trap; } /** * Removes an error trap. Normally you should not invoke this directly; * @{class:PhutilErrorTrap} deregisters itself on destruction. * * @param PhutilErrorTrap Trap to remove. * @return void * @task trap */ public static function removeErrorTrap(PhutilErrorTrap $trap) { $key = $trap->getTrapKey(); unset(self::$traps[$key]); } /* -( Internals )---------------------------------------------------------- */ /** * Determine if PhutilErrorHandler has been initialized. * * @return bool True if initialized. * @task internal */ public static function hasInitialized() { return self::$initialized; } /** * Handles PHP errors and dispatches them forward. This is a callback for * ##set_error_handler()##. You should not call this function directly; use * @{function:phlog} to print debugging messages or ##trigger_error()## to * trigger PHP errors. * * This handler converts E_RECOVERABLE_ERROR messages from violated typehints * into @{class:InvalidArgumentException}s. * * This handler converts other E_RECOVERABLE_ERRORs into * @{class:RuntimeException}s. * * This handler converts E_NOTICE messages from uses of undefined variables * into @{class:RuntimeException}s. * * @param int Error code. * @param string Error message. * @param string File where the error occurred. * @param int Line on which the error occurred. * @param wild Error context information. * @return void * @task internal */ public static function handleError($num, $str, $file, $line, $ctx) { foreach (self::$traps as $trap) { $trap->addError($num, $str, $file, $line, $ctx); } if ((error_reporting() & $num) == 0) { // Respect the use of "@" to silence warnings: if this error was // emitted from a context where "@" was in effect, the // value returned by error_reporting() will be 0. This is the // recommended way to check for this, see set_error_handler() docs // on php.net. return false; } // Convert typehint failures into exceptions. if (preg_match('/^Argument (\d+) passed to (\S+) must be/', $str)) { throw new InvalidArgumentException($str); } // Convert other E_RECOVERABLE_ERRORs into generic runtime exceptions. if ($num == E_RECOVERABLE_ERROR) { throw new RuntimeException($str); } // Convert uses of undefined variables into exceptions. if (preg_match('/^Undefined variable: /', $str)) { throw new RuntimeException($str); } // Convert uses of undefined properties into exceptions. if (preg_match('/^Undefined property: /', $str)) { throw new RuntimeException($str); } // Convert undefined constants into exceptions. Usually this means there // is a missing `$` and the program is horribly broken. if (preg_match('/^Use of undefined constant /', $str)) { throw new RuntimeException($str); } $trace = debug_backtrace(); array_shift($trace); self::dispatchErrorMessage( self::ERROR, $str, array( 'file' => $file, 'line' => $line, 'context' => $ctx, 'error_code' => $num, 'trace' => $trace, )); } /** * Handles PHP exceptions and dispatches them forward. This is a callback for * ##set_exception_handler()##. You should not call this function directly; * to print exceptions, pass the exception object to @{function:phlog}. * * @param Exception Uncaught exception object. * @return void * @task internal */ public static function handleException(Exception $ex) { self::dispatchErrorMessage( self::EXCEPTION, $ex, array( 'file' => $ex->getFile(), 'line' => $ex->getLine(), 'trace' => self::getRootException($ex)->getTrace(), 'catch_trace' => debug_backtrace(), )); // Normally, PHP exits with code 255 after an uncaught exception is thrown. // However, if we install an exception handler (as we have here), it exits // with code 0 instead. Script execution terminates after this function // exits in either case, so exit explicitly with the correct exit code. exit(255); } /** * Output a stacktrace to the PHP error log. * * @param trace A stacktrace, e.g. from debug_backtrace(); * @return void * @task internal */ public static function outputStacktrace($trace) { $lines = explode("\n", self::formatStacktrace($trace)); foreach ($lines as $line) { error_log($line); } } /** * Format a stacktrace for output. * * @param trace A stacktrace, e.g. from debug_backtrace(); * @return string Human-readable trace. * @task internal */ public static function formatStacktrace($trace) { $result = array(); $libinfo = self::getLibraryVersions(); if ($libinfo) { foreach ($libinfo as $key => $dict) { $info = array(); foreach ($dict as $dkey => $dval) { $info[] = $dkey.'='.$dval; } $libinfo[$key] = $key.'('.implode(', ', $info).')'; } $result[] = implode(', ', $libinfo); } foreach ($trace as $key => $entry) { $line = ' #'.$key.' '; if (isset($entry['class'])) { $line .= $entry['class'].'::'; } $line .= idx($entry, 'function', ''); if (isset($entry['args'])) { $args = array(); foreach ($entry['args'] as $arg) { // NOTE: Print out object types, not values. Values sometimes contain // sensitive information and are usually not particularly helpful // for debugging. $type = (gettype($arg) == 'object') ? get_class($arg) : gettype($arg); $args[] = $type; } $line .= '('.implode(', ', $args).')'; } if (isset($entry['file'])) { $file = self::adjustFilePath($entry['file']); $line .= ' called at ['.$file.':'.$entry['line'].']'; } $result[] = $line; } return implode("\n", $result); } /** * All different types of error messages come here before they are * dispatched to the listener; this method also prints them to the PHP error * log. * * @param const Event type constant. * @param wild Event value. * @param dict Event metadata. * @return void * @task internal */ public static function dispatchErrorMessage($event, $value, $metadata) { $timestamp = strftime('%Y-%m-%d %H:%M:%S'); switch ($event) { - case PhutilErrorHandler::ERROR: + case self::ERROR: $default_message = sprintf( '[%s] ERROR %d: %s at [%s:%d]', $timestamp, $metadata['error_code'], $value, $metadata['file'], $metadata['line']); $metadata['default_message'] = $default_message; error_log($default_message); self::outputStacktrace($metadata['trace']); break; - case PhutilErrorHandler::EXCEPTION: + case self::EXCEPTION: $messages = array(); $current = $value; do { $messages[] = '('.get_class($current).') '.$current->getMessage(); } while ($current = self::getPreviousException($current)); $messages = implode(' {>} ', $messages); if (strlen($messages) > 4096) { $messages = substr($messages, 0, 4096).'...'; } $default_message = sprintf( '[%s] EXCEPTION: %s at [%s:%d]', $timestamp, $messages, self::adjustFilePath(self::getRootException($value)->getFile()), self::getRootException($value)->getLine()); $metadata['default_message'] = $default_message; error_log($default_message); self::outputStacktrace(self::getRootException($value)->getTrace()); break; - case PhutilErrorHandler::PHLOG: + case self::PHLOG: $default_message = sprintf( '[%s] PHLOG: %s at [%s:%d]', $timestamp, PhutilReadableSerializer::printShort($value), $metadata['file'], $metadata['line']); $metadata['default_message'] = $default_message; error_log($default_message); break; - case PhutilErrorHandler::DEPRECATED: + case self::DEPRECATED: $default_message = sprintf( '[%s] DEPRECATED: %s is deprecated; %s', $timestamp, $value, $metadata['why']); $metadata['default_message'] = $default_message; error_log($default_message); break; default: error_log('Unknown event '.$event); break; } if (self::$errorListener) { static $handling_error; if ($handling_error) { error_log( 'Error handler was reentered, some errors were not passed to the '. 'listener.'); return; } $handling_error = true; call_user_func(self::$errorListener, $event, $value, $metadata); $handling_error = false; } } public static function adjustFilePath($path) { // Compute known library locations so we can emit relative paths if the // file resides inside a known library. This is a little cleaner to read, // and limits the number of false positives we get about full path // disclosure via HackerOne. $bootloader = PhutilBootloader::getInstance(); $libraries = $bootloader->getAllLibraries(); $roots = array(); foreach ($libraries as $library) { $root = $bootloader->getLibraryRoot($library); // For these libraries, the effective root is one level up. switch ($library) { case 'phutil': case 'arcanist': case 'phabricator': $root = dirname($root); break; } if (!strncmp($root, $path, strlen($root))) { return '<'.$library.'>'.substr($path, strlen($root)); } } return $path; } public static function getLibraryVersions() { $libinfo = array(); $bootloader = PhutilBootloader::getInstance(); foreach ($bootloader->getAllLibraries() as $library) { $root = phutil_get_library_root($library); $try_paths = array( $root, dirname($root), ); $libinfo[$library] = array(); $get_refs = array('master'); foreach ($try_paths as $try_path) { // Try to read what the HEAD of the repository is pointed at. This is // normally the name of a branch ("ref"). $try_file = $try_path.'/.git/HEAD'; if (@file_exists($try_file)) { $head = @file_get_contents($try_file); $matches = null; if (preg_match('(^ref: refs/heads/(.*)$)', trim($head), $matches)) { $libinfo[$library]['head'] = trim($matches[1]); $get_refs[] = trim($matches[1]); } else { $libinfo[$library]['head'] = trim($head); } break; } } // Try to read which commit relevant branch heads are at. foreach (array_unique($get_refs) as $ref) { foreach ($try_paths as $try_path) { $try_file = $try_path.'/.git/refs/heads/'.$ref; if (@file_exists($try_file)) { $hash = @file_get_contents($try_file); if ($hash) { $libinfo[$library]['ref.'.$ref] = substr(trim($hash), 0, 12); break; } } } } // Look for extension files. $custom = @scandir($root.'/extensions/'); if ($custom) { $count = 0; foreach ($custom as $custom_path) { if (preg_match('/\.php$/', $custom_path)) { $count++; } } if ($count) { $libinfo[$library]['custom'] = $count; } } } ksort($libinfo); return $libinfo; } } diff --git a/src/filesystem/Filesystem.php b/src/filesystem/Filesystem.php index 9612089..624501a 100644 --- a/src/filesystem/Filesystem.php +++ b/src/filesystem/Filesystem.php @@ -1,1066 +1,1066 @@ > 3]; } return $result; } /** * Identify the MIME type of a file. This returns only the MIME type (like * text/plain), not the encoding (like charset=utf-8). * * @param string Path to the file to examine. * @param string Optional default mime type to return if the file's mime * type can not be identified. * @return string File mime type. * * @task file * * @phutil-external-symbol function mime_content_type * @phutil-external-symbol function finfo_open * @phutil-external-symbol function finfo_file */ public static function getMimeType( $path, $default = 'application/octet-stream') { $path = self::resolvePath($path); self::assertExists($path); self::assertIsFile($path); self::assertReadable($path); $mime_type = null; // Fileinfo is the best approach since it doesn't rely on `file`, but // it isn't builtin for older versions of PHP. if (function_exists('finfo_open')) { $finfo = finfo_open(FILEINFO_MIME); if ($finfo) { $result = finfo_file($finfo, $path); if ($result !== false) { $mime_type = $result; } } } // If we failed Fileinfo, try `file`. This works well but not all systems // have the binary. if ($mime_type === null) { list($err, $stdout) = exec_manual( 'file --brief --mime %s', $path); if (!$err) { $mime_type = trim($stdout); } } // If we didn't get anywhere, try the deprecated mime_content_type() // function. if ($mime_type === null) { if (function_exists('mime_content_type')) { $result = mime_content_type($path); if ($result !== false) { $mime_type = $result; } } } // If we come back with an encoding, strip it off. if (strpos($mime_type, ';') !== false) { list($type, $encoding) = explode(';', $mime_type, 2); $mime_type = $type; } if ($mime_type === null) { $mime_type = $default; } return $mime_type; } /* -( Directories )-------------------------------------------------------- */ /** * Create a directory in a manner similar to mkdir(), but throw detailed * exceptions on failure. * * @param string Path to directory. The parent directory must exist and * be writable. * @param int Permission umask. Note that umask is in octal, so you * should specify it as, e.g., `0777', not `777'. * @param boolean Recursively create directories. Default to false. * @return string Path to the created directory. * * @task directory */ public static function createDirectory( $path, $umask = 0755, $recursive = false) { $path = self::resolvePath($path); if (is_dir($path)) { if ($umask) { - Filesystem::changePermissions($path, $umask); + self::changePermissions($path, $umask); } return $path; } $dir = dirname($path); if ($recursive && !file_exists($dir)) { // Note: We could do this with the recursive third parameter of mkdir(), // but then we loose the helpful FilesystemExceptions we normally get. self::createDirectory($dir, $umask, true); } self::assertIsDirectory($dir); self::assertExists($dir); self::assertWritable($dir); self::assertNotExists($path); if (!mkdir($path, $umask)) { throw new FilesystemException( $path, "Failed to create directory `{$path}'."); } // Need to change permissions explicitly because mkdir does something // slightly different. mkdir(2) man page: // 'The parameter mode specifies the permissions to use. It is modified by // the process's umask in the usual way: the permissions of the created // directory are (mode & ~umask & 0777)."' if ($umask) { - Filesystem::changePermissions($path, $umask); + self::changePermissions($path, $umask); } return $path; } /** * Create a temporary directory and return the path to it. You are * responsible for removing it (e.g., with Filesystem::remove()) * when you are done with it. * * @param string Optional directory prefix. * @param int Permissions to create the directory with. By default, * these permissions are very restrictive (0700). * @return string Path to newly created temporary directory. * * @task directory */ public static function createTemporaryDirectory($prefix = '', $umask = 0700) { $prefix = preg_replace('/[^A-Z0-9._-]+/i', '', $prefix); $tmp = sys_get_temp_dir(); if (!$tmp) { throw new FilesystemException( $tmp, 'Unable to determine system temporary directory.'); } $base = $tmp.DIRECTORY_SEPARATOR.$prefix; $tries = 3; do { $dir = $base.substr(base_convert(md5(mt_rand()), 16, 36), 0, 16); try { self::createDirectory($dir, $umask); break; } catch (FilesystemException $ex) { // Ignore. } } while (--$tries); if (!$tries) { $df = disk_free_space($tmp); if ($df !== false && $df < 1024 * 1024) { throw new FilesystemException( $dir, pht('Failed to create a temporary directory: the disk is full.')); } throw new FilesystemException( $dir, pht("Failed to create a temporary directory in '%s'.", $tmp)); } return $dir; } /** * List files in a directory. * * @param string Path, absolute or relative to PWD. * @param bool If false, exclude files beginning with a ".". * * @return array List of files and directories in the specified * directory, excluding `.' and `..'. * * @task directory */ public static function listDirectory($path, $include_hidden = true) { $path = self::resolvePath($path); self::assertExists($path); self::assertIsDirectory($path); self::assertReadable($path); $list = @scandir($path); if ($list === false) { throw new FilesystemException( $path, "Unable to list contents of directory `{$path}'."); } foreach ($list as $k => $v) { if ($v == '.' || $v == '..' || (!$include_hidden && $v[0] == '.')) { unset($list[$k]); } } return array_values($list); } /** * Return all directories between a path and "/". Iterating over them walks * from the path to the root. * * @param string Path, absolute or relative to PWD. * @return list List of parent paths, including the provided path. * @task directory */ public static function walkToRoot($path) { $path = self::resolvePath($path); if (is_link($path)) { $path = realpath($path); } $walk = array(); $parts = explode(DIRECTORY_SEPARATOR, $path); foreach ($parts as $k => $part) { if (!strlen($part)) { unset($parts[$k]); } } do { if (phutil_is_windows()) { $walk[] = implode(DIRECTORY_SEPARATOR, $parts); } else { $walk[] = DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR, $parts); } if (empty($parts)) { break; } array_pop($parts); } while (true); return $walk; } /* -( Paths )-------------------------------------------------------------- */ /** * Canonicalize a path by resolving it relative to some directory (by * default PWD), following parent symlinks and removing artifacts. If the * path is itself a symlink it is left unresolved. * * @param string Path, absolute or relative to PWD. * @return string Canonical, absolute path. * * @task path */ public static function resolvePath($path, $relative_to = null) { if (phutil_is_windows()) { $is_absolute = preg_match('/^[A-Za-z]+:/', $path); } else { $is_absolute = !strncmp($path, DIRECTORY_SEPARATOR, 1); } if (!$is_absolute) { if (!$relative_to) { $relative_to = getcwd(); } $path = $relative_to.DIRECTORY_SEPARATOR.$path; } if (is_link($path)) { $parent_realpath = realpath(dirname($path)); if ($parent_realpath !== false) { return $parent_realpath.DIRECTORY_SEPARATOR.basename($path); } } $realpath = realpath($path); if ($realpath !== false) { return $realpath; } // This won't work if the file doesn't exist or is on an unreadable mount // or something crazy like that. Try to resolve a parent so we at least // cover the nonexistent file case. $parts = explode(DIRECTORY_SEPARATOR, trim($path, DIRECTORY_SEPARATOR)); while (end($parts) !== false) { array_pop($parts); if (phutil_is_windows()) { $attempt = implode(DIRECTORY_SEPARATOR, $parts); } else { $attempt = DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR, $parts); } $realpath = realpath($attempt); if ($realpath !== false) { $path = $realpath.substr($path, strlen($attempt)); break; } } return $path; } /** * Test whether a path is descendant from some root path after resolving all * symlinks and removing artifacts. Both paths must exists for the relation * to obtain. A path is always a descendant of itself as long as it exists. * * @param string Child path, absolute or relative to PWD. * @param string Root path, absolute or relative to PWD. * @return bool True if resolved child path is in fact a descendant of * resolved root path and both exist. * @task path */ public static function isDescendant($path, $root) { try { self::assertExists($path); self::assertExists($root); } catch (FilesystemException $e) { return false; } $fs = new FileList(array($root)); return $fs->contains($path); } /** * Convert a canonical path to its most human-readable format. It is * guaranteed that you can use resolvePath() to restore a path to its * canonical format. * * @param string Path, absolute or relative to PWD. * @param string Optionally, working directory to make files readable * relative to. * @return string Human-readable path. * * @task path */ public static function readablePath($path, $pwd = null) { if ($pwd === null) { $pwd = getcwd(); } foreach (array($pwd, self::resolvePath($pwd)) as $parent) { $parent = rtrim($parent, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR; $len = strlen($parent); if (!strncmp($parent, $path, $len)) { $path = substr($path, $len); return $path; } } return $path; } /** * Determine whether or not a path exists in the filesystem. This differs from * file_exists() in that it returns true for symlinks. This method does not * attempt to resolve paths before testing them. * * @param string Test for the existence of this path. * @return bool True if the path exists in the filesystem. * @task path */ public static function pathExists($path) { return file_exists($path) || is_link($path); } /** * Determine if an executable binary (like `git` or `svn`) exists within * the configured `$PATH`. * * @param string Binary name, like `'git'` or `'svn'`. * @return bool True if the binary exists and is executable. * @task exec */ public static function binaryExists($binary) { return self::resolveBinary($binary) !== null; } /** * Locates the full path that an executable binary (like `git` or `svn`) is at * the configured `$PATH`. * * @param string Binary name, like `'git'` or `'svn'`. * @return string The full binary path if it is present, or null. * @task exec */ public static function resolveBinary($binary) { if (phutil_is_windows()) { list($err, $stdout) = exec_manual('where %s', $binary); $stdout = phutil_split_lines($stdout); // If `where %s` could not find anything, check for relative binary if ($err) { - $path = Filesystem::resolvePath($binary); - if (Filesystem::pathExists($path)) { + $path = self::resolvePath($binary); + if (self::pathExists($path)) { return $path; } return null; } $stdout = head($stdout); } else { list($err, $stdout) = exec_manual('which %s', $binary); } return $err === 0 ? trim($stdout) : null; } /** * Determine if two paths are equivalent by resolving symlinks. This is * different from resolving both paths and comparing them because * resolvePath() only resolves symlinks in parent directories, not the * path itself. * * @param string First path to test for equivalence. * @param string Second path to test for equivalence. * @return bool True if both paths are equivalent, i.e. reference the same * entity in the filesystem. * @task path */ public static function pathsAreEquivalent($u, $v) { - $u = Filesystem::resolvePath($u); - $v = Filesystem::resolvePath($v); + $u = self::resolvePath($u); + $v = self::resolvePath($v); $real_u = realpath($u); $real_v = realpath($v); if ($real_u) { $u = $real_u; } if ($real_v) { $v = $real_v; } return ($u == $v); } /* -( Assert )------------------------------------------------------------- */ /** * Assert that something (e.g., a file, directory, or symlink) exists at a * specified location. * * @param string Assert that this path exists. * @return void * * @task assert */ public static function assertExists($path) { if (!self::pathExists($path)) { throw new FilesystemException( $path, "Filesystem entity `{$path}' does not exist."); } } /** * Assert that nothing exists at a specified location. * * @param string Assert that this path does not exist. * @return void * * @task assert */ public static function assertNotExists($path) { if (file_exists($path) || is_link($path)) { throw new FilesystemException( $path, "Path `{$path}' already exists!"); } } /** * Assert that a path represents a file, strictly (i.e., not a directory). * * @param string Assert that this path is a file. * @return void * * @task assert */ public static function assertIsFile($path) { if (!is_file($path)) { throw new FilesystemException( $path, "Requested path `{$path}' is not a file."); } } /** * Assert that a path represents a directory, strictly (i.e., not a file). * * @param string Assert that this path is a directory. * @return void * * @task assert */ public static function assertIsDirectory($path) { if (!is_dir($path)) { throw new FilesystemException( $path, "Requested path `{$path}' is not a directory."); } } /** * Assert that a file or directory exists and is writable. * * @param string Assert that this path is writable. * @return void * * @task assert */ public static function assertWritable($path) { if (!is_writable($path)) { throw new FilesystemException( $path, "Requested path `{$path}' is not writable."); } } /** * Assert that a file or directory exists and is readable. * * @param string Assert that this path is readable. * @return void * * @task assert */ public static function assertReadable($path) { if (!is_readable($path)) { throw new FilesystemException( $path, "Path `{$path}' is not readable."); } } } diff --git a/src/parser/aast/api/AASTNodeList.php b/src/parser/aast/api/AASTNodeList.php index 52c0b10..d8689e0 100644 --- a/src/parser/aast/api/AASTNodeList.php +++ b/src/parser/aast/api/AASTNodeList.php @@ -1,107 +1,107 @@ tree, $nodes); + return self::newFromTreeAndNodes($this->tree, $nodes); } public function selectDescendantsOfType($type_name) { return $this->selectDescendantsOfTypes(array($type_name)); } public function selectDescendantsOfTypes(array $type_names) { $results = array(); foreach ($type_names as $type_name) { foreach ($this->list as $id => $node) { $results += $node->selectDescendantsOfType($type_name)->getRawNodes(); } } return $this->newList($results); } public function getChildrenByIndex($index) { $results = array(); foreach ($this->list as $id => $node) { $child = $node->getChildByIndex($index); $results[$child->getID()] = $child; } return $this->newList($results); } public function add(AASTNodeList $list) { foreach ($list->list as $id => $node) { $this->list[$id] = $node; } $this->ids = array_keys($this->list); return $this; } public function getTokens() { $tokens = array(); foreach ($this->list as $node) { $tokens += $node->getTokens(); } return $tokens; } public function getRawNodes() { return $this->list; } public static function newFromTreeAndNodes(AASTTree $tree, array $nodes) { assert_instances_of($nodes, 'AASTNode'); $obj = new AASTNodeList(); $obj->tree = $tree; $obj->list = $nodes; $obj->ids = array_keys($nodes); return $obj; } public static function newFromTree(AASTTree $tree) { $obj = new AASTNodeList(); $obj->tree = $tree; $obj->list = array(0 => $tree->getRootNode()); $obj->ids = array(0 => 0); return $obj; } /* -( Countable )---------------------------------------------------------- */ public function count() { return count($this->ids); } /* -( Iterator )----------------------------------------------------------- */ public function current() { return $this->list[$this->key()]; } public function key() { return $this->ids[$this->pos]; } public function next() { $this->pos++; } public function rewind() { $this->pos = 0; } public function valid() { return $this->pos < count($this->ids); } } diff --git a/src/parser/argument/PhutilArgumentSpecification.php b/src/parser/argument/PhutilArgumentSpecification.php index 0e6b2f1..d192855 100644 --- a/src/parser/argument/PhutilArgumentSpecification.php +++ b/src/parser/argument/PhutilArgumentSpecification.php @@ -1,258 +1,258 @@ 'verbose', * 'short' => 'v', * )); * * Recognized keys and equivalent verbose methods are: * * name setName() * help setHelp() * short setShortAlias() * param setParamName() * default setDefault() * conflicts setConflicts() * wildcard setWildcard() * repeat setRepeatable() * * @param dict Dictionary of quick parameter definitions. * @return PhutilArgumentSpecification Constructed argument specification. */ public static function newQuickSpec(array $spec) { $recognized_keys = array( 'name', 'help', 'short', 'param', 'default', 'conflicts', 'wildcard', 'repeat', 'standard', ); $unrecognized = array_diff_key( $spec, array_fill_keys($recognized_keys, true)); foreach ($unrecognized as $key => $ignored) { throw new PhutilArgumentSpecificationException( "Unrecognized key '{$key}' in argument specification. Recognized keys ". "are: ".implode(', ', $recognized_keys).'.'); } $obj = new PhutilArgumentSpecification(); foreach ($spec as $key => $value) { switch ($key) { case 'name': $obj->setName($value); break; case 'help': $obj->setHelp($value); break; case 'short': $obj->setShortAlias($value); break; case 'param': $obj->setParamName($value); break; case 'default': $obj->setDefault($value); break; case 'conflicts': $obj->setConflicts($value); break; case 'wildcard': $obj->setWildcard($value); break; case 'repeat': $obj->setRepeatable($value); break; case 'standard': $obj->setStandard($value); break; } } $obj->validate(); return $obj; } public static function newSpecsFromList(array $specs) { foreach ($specs as $key => $spec) { if (is_array($spec)) { - $specs[$key] = PhutilArgumentSpecification::newQuickSpec( + $specs[$key] = self::newQuickSpec( $spec); } } return $specs; } public function setName($name) { self::validateName($name); $this->name = $name; return $this; } private static function validateName($name) { if (!preg_match('/^[a-z0-9][a-z0-9-]*$/', $name)) { throw new PhutilArgumentSpecificationException( "Argument names may only contain a-z, 0-9 and -, and must be ". "at least one character long. '{$name}' is invalid."); } } public function getName() { return $this->name; } public function setHelp($help) { $this->help = $help; return $this; } public function getHelp() { return $this->help; } public function setShortAlias($short_alias) { self::validateShortAlias($short_alias); $this->shortAlias = $short_alias; return $this; } private static function validateShortAlias($alias) { if (strlen($alias) !== 1) { throw new PhutilArgumentSpecificationException( "Argument short aliases must be exactly one character long. ". "'{$alias}' is invalid."); } if (!preg_match('/^[a-zA-Z0-9]$/', $alias)) { throw new PhutilArgumentSpecificationException( "Argument short aliases may only be in a-z, A-Z and 0-9. ". "'{$alias}' is invalid."); } } public function getShortAlias() { return $this->shortAlias; } public function setParamName($param_name) { $this->paramName = $param_name; return $this; } public function getParamName() { return $this->paramName; } public function setDefault($default) { $this->default = $default; return $this; } public function getDefault() { if ($this->getParamName() === null) { if ($this->getRepeatable()) { return 0; } else { return false; } } else { if ($this->getRepeatable()) { return array(); } else { return $this->default; } } } public function setConflicts(array $conflicts) { $this->conflicts = $conflicts; return $this; } public function getConflicts() { return $this->conflicts; } public function setWildcard($wildcard) { $this->wildcard = $wildcard; return $this; } public function getWildcard() { return $this->wildcard; } public function setRepeatable($repeatable) { $this->repeatable = $repeatable; return $this; } public function getRepeatable() { return $this->repeatable; } public function setStandard($standard) { $this->standard = $standard; return $this; } public function getStandard() { return $this->standard; } public function validate() { if ($this->name === null) { throw new PhutilArgumentSpecificationException( "Argument specification MUST have a 'name'."); } if ($this->getWildcard()) { if ($this->getParamName()) { throw new PhutilArgumentSpecificationException( 'Wildcard arguments may not specify a parameter.'); } if ($this->getRepeatable()) { throw new PhutilArgumentSpecificationException( 'Wildcard arguments may not be repeatable.'); } } if ($this->default !== null) { if ($this->getRepeatable()) { throw new PhutilArgumentSpecificationException( 'Repeatable arguments may not have a default (always array() for '. 'arguments which accept a parameter, or 0 for arguments which do '. 'not).'); } else if ($this->getParamName() === null) { throw new PhutilArgumentSpecificationException( 'Flag arguments may not have a default (always false).'); } } } } diff --git a/src/parser/xhpast/api/XHPASTTree.php b/src/parser/xhpast/api/XHPASTTree.php index 0793f87..5ffe060 100644 --- a/src/parser/xhpast/api/XHPASTTree.php +++ b/src/parser/xhpast/api/XHPASTTree.php @@ -1,77 +1,77 @@ setTreeType('XHP'); $this->setNodeConstants(xhp_parser_node_constants()); $this->setTokenConstants(xhpast_parser_token_constants()); parent::__construct($tree, $stream, $source); } public function newNode($id, array $data, AASTTree $tree) { return new XHPASTNode($id, $data, $tree); } public function newToken($id, $type, $value, $offset, AASTTree $tree) { return new XHPASTToken($id, $type, $value, $offset, $tree); } public static function newFromData($php_source) { $future = PhutilXHPASTBinary::getParserFuture($php_source); return self::newFromDataAndResolvedExecFuture( $php_source, $future->resolve()); } public static function newStatementFromString($string) { $string = 'getRootNode()->selectDescendantsOfType('n_STATEMENT'); if (count($statements) != 1) { throw new Exception( pht('String does not parse into exactly one statement!')); } // Return the first one, trying to use reset() with iterators ends in tears. foreach ($statements as $statement) { return $statement; } } public static function newFromDataAndResolvedExecFuture( $php_source, array $resolved) { list($err, $stdout, $stderr) = $resolved; if ($err) { if ($err == 1) { $matches = null; $is_syntax = preg_match( '/^XHPAST Parse Error: (.*) on line (\d+)/s', $stderr, $matches); if ($is_syntax) { throw new XHPASTSyntaxErrorException($matches[2], trim($stderr)); } } throw new Exception( pht( 'XHPAST failed to parse file data %d: %s', $err, $stderr)); } $data = null; try { $data = phutil_json_decode($stdout); } catch (PhutilJSONParserException $ex) { throw new PhutilProxyException( pht('XHPAST: failed to decode tree.'), $ex); } return new XHPASTTree($data['tree'], $data['stream'], $php_source); } } diff --git a/src/serviceprofiler/PhutilServiceProfiler.php b/src/serviceprofiler/PhutilServiceProfiler.php index 4429488..655bbdb 100644 --- a/src/serviceprofiler/PhutilServiceProfiler.php +++ b/src/serviceprofiler/PhutilServiceProfiler.php @@ -1,161 +1,161 @@ discardMode = true; } public static function getInstance() { if (empty(self::$instance)) { self::$instance = new PhutilServiceProfiler(); } return self::$instance; } public function beginServiceCall(array $data) { $data['begin'] = microtime(true); $id = $this->logSize++; $this->events[$id] = $data; foreach ($this->listeners as $listener) { call_user_func($listener, 'begin', $id, $data); } return $id; } public function endServiceCall($call_id, array $data) { $data = ($this->events[$call_id] + $data); $data['end'] = microtime(true); $data['duration'] = ($data['end'] - $data['begin']); $this->events[$call_id] = $data; foreach ($this->listeners as $listener) { call_user_func($listener, 'end', $call_id, $data); } if ($this->discardMode) { unset($this->events[$call_id]); } } public function getServiceCallLog() { return $this->events; } public function addListener($callback) { $this->listeners[] = $callback; } public static function installEchoListener() { - $instance = PhutilServiceProfiler::getInstance(); + $instance = self::getInstance(); $instance->addListener(array('PhutilServiceProfiler', 'echoListener')); } public static function echoListener($type, $id, $data) { $is_begin = false; $is_end = false; switch ($type) { case 'begin': $is_begin = true; $mark = '>>>'; break; case 'end': $is_end = true; $mark = '<<<'; break; default: $mark = null; break; } $type = idx($data, 'type', 'mystery'); $desc = null; if ($is_begin) { switch ($type) { case 'connect': $desc = $data['database']; break; case 'query': $desc = substr($data['query'], 0, 512); break; case 'multi-query': $desc = array(); foreach ($data['queries'] as $query) { $desc[] = substr($query, 0, 256); } $desc = implode('; ', $desc); break; case 'exec': $desc = '$ '.$data['command']; break; case 'conduit': if (isset($data['size'])) { $desc = $data['method'].'() '; } else { $desc = $data['method'].'()'; } break; case 'http': $desc = phutil_censor_credentials($data['uri']); break; case 'lint': $desc = $data['linter']; if (isset($data['paths'])) { $desc .= ' '; } break; case 'lock': $desc = $data['name']; break; case 'event': $desc = $data['kind'].' '; break; case 'ldap': $call = idx($data, 'call', '?'); $params = array(); switch ($call) { case 'connect': $params[] = $data['host'].':'.$data['port']; break; case 'start-tls': break; case 'bind': $params[] = $data['user']; break; case 'search': $params[] = $data['dn']; $params[] = $data['query']; break; default: $params[] = '?'; break; } $desc = "{$call} (".implode(', ', $params).")"; break; } } else if ($is_end) { $desc = number_format((int)(1000000 * $data['duration'])).' us'; } $console = PhutilConsole::getConsole(); $console->writeLog( "%s [%s] <%s> %s\n", $mark, $id, $type, $desc); } } diff --git a/src/utils/viewutils.php b/src/utils/viewutils.php index 24bbd2b..0157f32 100644 --- a/src/utils/viewutils.php +++ b/src/utils/viewutils.php @@ -1,170 +1,170 @@ $now - $shift) { $format = pht('D, M j'); } else { $format = pht('M j Y'); } return $format; } function phutil_format_relative_time($duration) { return phutil_format_units_generic( $duration, array(60, 60, 24, 7), array('s', 'm', 'h', 'd', 'w'), $precision = 0); } /** * Format a relative time (duration) into weeks, days, hours, minutes, * seconds, but unlike phabricator_format_relative_time, does so for more than * just the largest unit. * * @param int Duration in seconds. * @param int Levels to render - will render the three highest levels, ie: * 5 h, 37 m, 1 s * @return string Human-readable description. */ function phutil_format_relative_time_detailed($duration, $levels = 2) { if ($duration == 0) { return 'now'; } $levels = max(1, min($levels, 5)); $remainder = 0; $is_negative = false; if ($duration < 0) { $is_negative = true; $duration = abs($duration); } $this_level = 1; $detailed_relative_time = phutil_format_units_generic( $duration, array(60, 60, 24, 7), array('s', 'm', 'h', 'd', 'w'), $precision = 0, $remainder); $duration = $remainder; while ($remainder > 0 && $this_level < $levels) { $detailed_relative_time .= ', '.phutil_format_units_generic( $duration, array(60, 60, 24, 7), array('s', 'm', 'h', 'd', 'w'), $precision = 0, $remainder); $duration = $remainder; $this_level++; - }; + } if ($is_negative) { $detailed_relative_time .= ' ago'; } return $detailed_relative_time; } /** * Format a byte count for human consumption, e.g. "10MB" instead of * "10485760". * * @param int Number of bytes. * @return string Human-readable description. */ function phutil_format_bytes($bytes) { return phutil_format_units_generic( $bytes, array(1024, 1024, 1024, 1024, 1024), array('B', 'KB', 'MB', 'GB', 'TB', 'PB'), $precision = 0); } /** * Parse a human-readable byte description (like "6MB") into an integer. * * @param string Human-readable description. * @return int Number of represented bytes. */ function phutil_parse_bytes($input) { $bytes = trim($input); if (!strlen($bytes)) { return null; } // NOTE: Assumes US-centric numeral notation. $bytes = preg_replace('/[ ,]/', '', $bytes); $matches = null; if (!preg_match('/^(?:\d+(?:[.]\d+)?)([kmgtp]?)b?$/i', $bytes, $matches)) { throw new Exception("Unable to parse byte size '{$input}'!"); } $scale = array( 'k' => 1024, 'm' => 1024 * 1024, 'g' => 1024 * 1024 * 1024, 't' => 1024 * 1024 * 1024 * 1024, 'p' => 1024 * 1024 * 1024 * 1024 * 1024, ); $bytes = (float)$bytes; if ($matches[1]) { $bytes *= $scale[strtolower($matches[1])]; } return (int)$bytes; } function phutil_format_units_generic( $n, array $scales, array $labels, $precision = 0, &$remainder = null) { $is_negative = false; if ($n < 0) { $is_negative = true; $n = abs($n); } $remainder = 0; $accum = 1; $scale = array_shift($scales); $label = array_shift($labels); while ($n >= $scale && count($labels)) { $remainder += ($n % $scale) * $accum; $n /= $scale; $accum *= $scale; $label = array_shift($labels); if (!count($scales)) { break; } $scale = array_shift($scales); } if ($is_negative) { $n = -$n; $remainder = -$remainder; } if ($precision) { $num_string = number_format($n, $precision); } else { $num_string = (int)floor($n); } if ($label) { $num_string .= ' '.$label; } return $num_string; }