/** * Does a command exist? * * @param string $command Command line * * @return boolean */ public static function is($command) { $result = trim(shell_exec("which {$command}")); return !TextHelper::isEmpty($result); }
/** * Sets inner HTML code. * * @param string $value Inner HTML code * * @return DomNode */ private function _setInnerHtml($value) { $this->clear(); if (!TextHelper::isEmpty($value)) { foreach ($this->elements() as $element) { $doc = $element->ownerDocument; $fragment = $doc->createDocumentFragment(); @$fragment->appendXML($value); $node = @$element->appendChild($fragment); if ($node === false) { throw new DomNodeException("Invalid XML fragment"); } } } return $this; }
/** * Gets an available name under a given directory. * * This function returns an available name under a given directory. For * example, if there's a file named 'test.txt' under de directory 'dir1', the * following command returns 'test_1.txt': * * ```php * // prints 'test_1.txt' if the name 'test.txt' is taken: * echo SysFile::getAvailName('dir1', 'test1.txt'); * ``` * * @param string $dir Directory * @param string $refname Filename used as reference (default is "") * @param string $refext Extension used as reference (default is "") * * @return string */ public static function getAvailName($dir, $refname = "", $refext = "") { // fixes arguments $dir = trim($dir); $refname = trim($refname); $refext = ltrim(trim($refext), "."); if (!is_dir($dir)) { throw new SysException("Directory not found: {$dir}"); } // default refname if (TextHelper::isEmpty($refname)) { $refname = "file"; } // gets name and extension $refname = basename($refname); $pos = strrpos($refname, "."); $name = $refname; $ext = $refext; if ($pos !== false) { $name = substr($refname, 0, $pos); if (TextHelper::isEmpty($refext)) { $ext = substr($refname, $pos + 1); } } // gets an available name for ($i = 0; $i < 100; $i++) { $basename = $i > 0 ? TextHelper::concat(".", $name . "_" . $i, $ext) : TextHelper::concat(".", $name, $ext); $filename = SysFileHelper::concat($dir, $basename); if (!is_file($filename)) { break; } } return $filename; }
/** * Gets the string representation of the instance. * * @return string */ public function toString() { $ret = $this->_name ? "-" . SysCmdHelper::escape($this->_name) : null; if (!TextHelper::isEmpty($this->_value)) { $value = $this->_value; if (!$this->_isRaw) { $value = SysCmdHelper::escape($value); } $ret = TextHelper::concat(" ", $ret, $value); } return $ret; }
/** * Creates an instance from a given string. * * @param string $str Well formed document * @param string $contentType Content Type (default is "text/xml") * * @return DomNode */ public static function createFromString($str, $contentType = "text/xml") { $doc = new DOMDocument("1.0"); $doc->preserveWhiteSpace = false; $doc->formatOutput = true; // use internal errors $useInternalErrors = libxml_use_internal_errors(true); if ($contentType == "text/html") { $doc->loadHTML($str); } else { $doc->loadXML($str); } // retrieves the errors $text = ""; $errors = libxml_get_errors(); foreach ($errors as $error) { $message = trim($error->message); $text = TextHelper::concat("\n", $text, "{$message} on line {$error->line}, column {$error->column}"); } libxml_clear_errors(); // restores internal errors status libxml_use_internal_errors($useInternalErrors); if (!TextHelper::isEmpty($text)) { throw new DomNodeException($text); } $node = new static(); $node->document = $doc; $node->elements = array($doc->documentElement); return $node; }