public function testisDescendant()
 {
     $test_cases = array(array(__FILE__, dirname(__FILE__), true), array(dirname(__FILE__), dirname(dirname(__FILE__)), true), array(dirname(__FILE__), phutil_get_library_root_for_path(__FILE__), true), array(dirname(dirname(__FILE__)), dirname(__FILE__), false), array(dirname(__FILE__) . '/quack', dirname(__FILE__), false));
     foreach ($test_cases as $test_case) {
         list($path, $root, $expected) = $test_case;
         $this->assertEqual($expected, Filesystem::isDescendant($path, $root), sprintf('Filesystem::isDescendant(%s, %s)', phutil_var_export($path), phutil_var_export($root)));
     }
 }
Esempio n. 2
0
 public function testVarExport()
 {
     // Constants
     $this->assertEqual('null', phutil_var_export(null));
     $this->assertEqual('true', phutil_var_export(true));
     $this->assertEqual('false', phutil_var_export(false));
     $this->assertEqual("'quack'", phutil_var_export('quack'));
     $this->assertEqual('1234567', phutil_var_export(1234567));
     // Arrays
     $this->assertEqual('array()', phutil_var_export(array()));
     $this->assertEqual(implode("\n", array('array(', '  1,', '  2,', '  3,', ')')), phutil_var_export(array(1, 2, 3)));
     $this->assertEqual(implode("\n", array('array(', "  'foo' => 'bar',", "  'bar' => 'baz',", ')')), phutil_var_export(array('foo' => 'bar', 'bar' => 'baz')));
     $this->assertEqual(implode("\n", array('array(', "  'foo' => array(", "    'bar' => array(", "      'baz' => array(),", '    ),', '  ),', ')')), phutil_var_export(array('foo' => array('bar' => array('baz' => array())))));
     // Objects
     $this->assertEqual("stdClass::__set_state(array(\n))", phutil_var_export(new stdClass()));
     $this->assertEqual("PhutilTestPhobject::__set_state(array(\n))", phutil_var_export(new PhutilTestPhobject()));
 }
 private function formatAndIndent($var, $depth)
 {
     $var = phutil_var_export($var);
     $var = str_replace("\n", "\n" . str_repeat(' ', $depth), $var);
     return $var;
 }
    /**
     * Write a finalized library map.
     *
     * @param  dict Library map structure to write.
     * @return void
     *
     * @task source
     */
    private function writeLibraryMap(array $library_map)
    {
        $map_file = $this->getPathForLibraryMap();
        $version = self::LIBRARY_MAP_VERSION;
        $library_map = array(self::LIBRARY_MAP_VERSION_KEY => $version) + $library_map;
        $library_map = phutil_var_export($library_map);
        $at = '@';
        $source_file = <<<EOPHP
<?php

/**
 * This file is automatically generated. Use 'arc liberate' to rebuild it.
 *
 * {$at}generated
 * {$at}phutil-library-version {$version}
 */
phutil_register_library_map({$library_map});

EOPHP;
        Filesystem::writeFile($map_file, $source_file);
    }
Esempio n. 5
0
/**
 * Returns a parsable string representation of a variable.
 *
 * This function is intended to behave similarly to PHP's `var_export` function,
 * but the output is intended to follow our style conventions.
 *
 * @param  wild    The variable you want to export.
 * @return string
 */
function phutil_var_export($var)
{
    // `var_export(null, true)` returns `"NULL"` (in uppercase).
    if ($var === null) {
        return 'null';
    }
    // PHP's `var_export` doesn't format arrays very nicely. In particular:
    //
    //   - An empty array is split over two lines (`"array (\n)"`).
    //   - A space separates "array" and the first opening brace.
    //   - Non-associative arrays are returned as associative arrays with an
    //     integer key.
    //
    if (is_array($var)) {
        if (count($var) === 0) {
            return 'array()';
        }
        // Don't show keys for non-associative arrays.
        $show_keys = array_keys($var) !== range(0, count($var) - 1);
        $output = array();
        $output[] = 'array(';
        foreach ($var as $key => $value) {
            // Adjust the indentation of the value.
            $value = str_replace("\n", "\n  ", phutil_var_export($value));
            $output[] = '  ' . ($show_keys ? var_export($key, true) . ' => ' : '') . $value . ',';
        }
        $output[] = ')';
        return implode("\n", $output);
    }
    // Let PHP handle everything else.
    return var_export($var, true);
}
 private function buildPHPExample(ConduitAPIMethod $method, $params)
 {
     $parts = array();
     $libphutil_path = 'path/to/libphutil/src/__phutil_library_init__.php';
     $parts[] = '<?php';
     $parts[] = "\n\n";
     $parts[] = 'require_once ';
     $parts[] = phutil_var_export($libphutil_path, true);
     $parts[] = ";\n\n";
     $parts[] = '$api_token = "';
     $parts[] = phutil_tag('strong', array(), pht('<api-token>'));
     $parts[] = "\";\n";
     $parts[] = '$api_parameters = ';
     if ($params === null) {
         $parts[] = 'array(';
         $parts[] = phutil_tag('strong', array(), pht('<parameters>'));
         $parts[] = ');';
     } else {
         $params = $this->simplifyParams($params);
         $params = phutil_var_export($params, true);
         $parts[] = phutil_tag('strong', array('class' => 'real'), $params);
         $parts[] = ';';
     }
     $parts[] = "\n\n";
     $parts[] = '$client = new ConduitClient(';
     $parts[] = phutil_tag('strong', array('class' => 'real'), phutil_var_export(PhabricatorEnv::getURI('/'), true));
     $parts[] = ");\n";
     $parts[] = '$client->setConduitToken($api_token);';
     $parts[] = "\n\n";
     $parts[] = '$result = $client->callMethodSynchronous(';
     $parts[] = phutil_tag('strong', array('class' => 'real'), phutil_var_export($method->getAPIMethodName(), true));
     $parts[] = ', ';
     $parts[] = '$api_parameters';
     $parts[] = ");\n";
     $parts[] = 'print_r($result);';
     return $this->renderExampleCode($parts);
 }
    private function formatMapContent(array $data)
    {
        $content = phutil_var_export($data);
        $generated = '@' . 'generated';
        return <<<EOFILE
<?php

/**
 * This file is automatically generated. Use 'bin/celerity map' to rebuild it.
 *
 * {$generated}
 */
return {$content};

EOFILE;
    }