public function addPropertyList(PHUIPropertyListView $property_list, $tab = null)
 {
     if (!$tab instanceof PHUIListItemView && $tab !== null) {
         assert_stringlike($tab);
         $tab = id(new PHUIListItemView())->setName($tab);
     }
     if ($tab) {
         if ($tab->getKey()) {
             $key = $tab->getKey();
         } else {
             $key = 'tab.default.' . spl_object_hash($tab);
             $tab->setKey($key);
         }
     } else {
         $key = 'tab.default';
     }
     if ($tab) {
         if (empty($this->tabs[$key])) {
             $tab->addSigil('phui-object-box-tab');
             $tab->setMetadata(array('tabKey' => $key));
             if (!$tab->getHref()) {
                 $tab->setHref('#');
             }
             if (!$tab->getType()) {
                 $tab->setType(PHUIListItemView::TYPE_LINK);
             }
             $this->tabs[$key] = $tab;
         }
     }
     $this->propertyLists[$key][] = $property_list;
     return $this;
 }
Exemple #2
0
/**
 * @group markup
 */
function phutil_escape_html($string)
{
    if ($string instanceof PhutilSafeHTML) {
        return $string;
    } else {
        if ($string instanceof PhutilSafeHTMLProducerInterface) {
            $result = $string->producePhutilSafeHTML();
            if ($result instanceof PhutilSafeHTML) {
                return phutil_escape_html($result);
            } else {
                if (is_array($result)) {
                    return phutil_escape_html($result);
                } else {
                    if ($result instanceof PhutilSafeHTMLProducerInterface) {
                        return phutil_escape_html($result);
                    } else {
                        try {
                            assert_stringlike($result);
                            return phutil_escape_html((string) $result);
                        } catch (Exception $ex) {
                            $class = get_class($string);
                            throw new Exception("Object (of class '{$class}') implements " . "PhutilSafeHTMLProducerInterface but did not return anything " . "renderable from producePhutilSafeHTML().");
                        }
                    }
                }
            }
        } else {
            if (is_array($string)) {
                return implode('', array_map('phutil_escape_html', $string));
            }
        }
    }
    return htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
}
Exemple #3
0
 public function testAssertStringLike()
 {
     assert_stringlike(null);
     assert_stringlike("");
     assert_stringlike("Hello World");
     assert_stringlike(1);
     assert_stringlike(9.9999);
     assert_stringlike(true);
     assert_stringlike(new Exception('.'));
     $obj = (object) [];
     $caught = null;
     try {
         assert_stringlike($obj);
     } catch (InvalidArgumentException $ex) {
         $caught = $ex;
     }
     $this->assertInstanceOf("InvalidArgumentException", $caught);
     $array = ["foo" => "bar", "bar" => "foo"];
     $caught = null;
     try {
         assert_stringlike($array);
     } catch (InvalidArgumentException $ex) {
         $caught = $ex;
     }
     $this->assertInstanceOf("InvalidArgumentException", $caught);
 }
 /**
  * Append to a file without having to deal with file handles, with
  * detailed exceptions on failure.
  *
  * @param  string  File path to write. This file must be writable or its
  *                 parent directory must exist and be writable.
  * @param  string  Data to write.
  *
  * @task   file
  */
 public static function appendFile($path, $data)
 {
     $path = self::resolvePath($path);
     // Use self::writeFile() if the file doesn't already exist
     try {
         self::assertExists($path);
     } catch (FilesystemException $ex) {
         self::writeFile($path, $data);
         return;
     }
     // File needs to exist or the directory needs to be writable
     $dir = dirname($path);
     self::assertExists($dir);
     self::assertIsDirectory($dir);
     self::assertWritable($dir);
     assert_stringlike($data);
     if (($fh = fopen($path, 'a')) === false) {
         throw new FilesystemException($path, pht("Failed to open file '%s'.", $path));
     }
     $dlen = strlen($data);
     if (fwrite($fh, $data) !== $dlen) {
         throw new FilesystemException($path, pht("Failed to write %d bytes to '%s'.", $dlen, $path));
     }
     if (!fflush($fh) || !fclose($fh)) {
         throw new FilesystemException($path, pht("Failed closing file '%s' after write.", $path));
     }
 }
 public function testAssertStringLike()
 {
     $this->assertEqual(null, assert_stringlike(null));
     $this->assertEqual(null, assert_stringlike(''));
     $this->assertEqual(null, assert_stringlike('Hello World'));
     $this->assertEqual(null, assert_stringlike(1));
     $this->assertEqual(null, assert_stringlike(9.9999));
     $this->assertEqual(null, assert_stringlike(true));
     $obj = new Exception('.');
     $this->assertEqual(null, assert_stringlike($obj));
     $obj = (object) array();
     try {
         assert_stringlike($obj);
     } catch (InvalidArgumentException $ex) {
         $caught = $ex;
     }
     $this->assertTrue($caught instanceof InvalidArgumentException);
     $array = array('foo' => 'bar', 'bar' => 'foo');
     try {
         assert_stringlike($array);
     } catch (InvalidArgumentException $ex) {
         $caught = $ex;
     }
     $this->assertTrue($caught instanceof InvalidArgumentException);
     $tmp = new TempFile();
     $resource = fopen($tmp, 'r');
     try {
         assert_stringlike($resource);
     } catch (InvalidArgumentException $ex) {
         $caught = $ex;
     }
     fclose($resource);
     $this->assertTrue($caught instanceof InvalidArgumentException);
 }
function phutil_escape_html($string)
{
    if ($string instanceof PhutilSafeHTML) {
        return $string;
    } else {
        if ($string instanceof PhutilSafeHTMLProducerInterface) {
            $result = $string->producePhutilSafeHTML();
            if ($result instanceof PhutilSafeHTML) {
                return phutil_escape_html($result);
            } else {
                if (is_array($result)) {
                    return phutil_escape_html($result);
                } else {
                    if ($result instanceof PhutilSafeHTMLProducerInterface) {
                        return phutil_escape_html($result);
                    } else {
                        try {
                            assert_stringlike($result);
                            return phutil_escape_html((string) $result);
                        } catch (Exception $ex) {
                            throw new Exception(pht("Object (of class '%s') implements %s but did not return anything " . "renderable from %s.", get_class($string), 'PhutilSafeHTMLProducerInterface', 'producePhutilSafeHTML()'));
                        }
                    }
                }
            }
        } else {
            if (is_array($string)) {
                $result = '';
                foreach ($string as $item) {
                    $result .= phutil_escape_html($item);
                }
                return $result;
            }
        }
    }
    return htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
}