[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/tests_runner/lib/spikephpcoverage/src/util/ -> Utility.php (source)

   1  <?php
   2      /*
   3      *  $Id: Utility.php 14663 2005-03-23 19:27:27Z npac $
   4      *
   5      *  Copyright(c) 2004-2005, SpikeSource Inc. All Rights Reserved.
   6      *  Licensed under the Open Source License version 2.1
   7      *  (See http://www.spikesource.com/license.html)
   8      */
   9  ?>
  10  <?php
  11  
  12      if(!defined("__PHPCOVERAGE_HOME")) {
  13          define("__PHPCOVERAGE_HOME", dirname(dirname(__FILE__)));
  14      }
  15      require_once  __PHPCOVERAGE_HOME . "/conf/phpcoverage.conf.php";
  16  
  17      // include our dummy implementation
  18      require_once  'Logger.php';
  19  
  20  
  21      /**
  22      * Utility functions
  23      *
  24      * @author Nimish Pachapurkar <npac@spikesource.com>
  25      * @version $Revision: $
  26      * @package tests_runner
  27      */
  28      class Utility {
  29  
  30          public static $logger;
  31  
  32          /*{{{ public function getTimeStamp() */
  33  
  34          /**
  35          * Return the current timestamp in human readable format.
  36          * Thursday March 17, 2005 19:10:47
  37          *
  38          * @return Readable timestamp
  39          * @access public
  40          */
  41          public function getTimeStamp() {
  42              $ts = getdate();
  43              return $ts["weekday"] . " " . $ts["month"] . " " . $ts["mday"]
  44              . ", " . $ts["year"] . " " . sprintf("%02d:%02d:%02d", $ts["hours"], $ts["minutes"], $ts["seconds"]);
  45          }
  46  
  47          /*}}}*/
  48          /*{{{ public function shortenFilename() */
  49  
  50          /**
  51          * Shorten the filename to some maximum characters
  52          *
  53          * @param $filename Complete file path
  54          * @param $maxlength=150 Maximum allowable length of the shortened
  55          * filepath
  56          * @return Shortened file path
  57          * @access public
  58          */
  59          public function shortenFilename($filename, $maxlength=80) {
  60              $length = strlen($filename);
  61              if($length < $maxlength) {
  62                  return $filename;
  63              }
  64  
  65              // trim the first few characters
  66              $filename = substr($filename, $length-$maxlength);
  67              // If there is a path separator slash in first n characters,
  68              // trim upto that point.
  69              $n = 20;
  70              $firstSlash = strpos($filename, "/");
  71              if($firstSlash === false || $firstSlash > $n) {
  72                  $firstSlash = strpos($filename, "\\");
  73                  if($firstSlash === false || $firstSlash > $n) {
  74                      return "..." . $filename;
  75                  }
  76                  return "..." . substr($filename, $firstSlash);
  77              }
  78              return "..." . substr($filename, $firstSlash);
  79          }
  80  
  81          /*}}}*/
  82          /*{{{ public function writeError() */
  83  
  84          /**
  85          * Write error log if debug is on
  86          *
  87          * @param $str Error string
  88          * @access public
  89          */
  90          public function writeError($str) {
  91              if(__PHPCOVERAGE_DEBUG) {
  92                  error_log($str);
  93              }
  94          }
  95          /*}}}*/
  96          /*{{{ public function unixifyPath() */
  97  
  98          /**
  99          * Convert Windows paths to Unix paths
 100          *
 101          * @param $path File path
 102          * @return String Unixified file path
 103          * @access public
 104          */
 105          public function unixifyPath($path) {
 106              // Remove the drive-letter:
 107              if(strpos($path, ":") == 1) {
 108                  $path = substr($path, 2);
 109              }
 110              $path = $this->replaceBackslashes($path);
 111              return $path;
 112          }
 113  
 114          /*}}}*/
 115          /*{{{ public function replaceBackslashes() */
 116  
 117          /**
 118          * Convert the back slash path separators with forward slashes.
 119          *
 120          * @param $path Windows path with backslash path separators
 121          * @return String Path with back slashes replaced with forward slashes.
 122          * @access public
 123          */
 124          public function replaceBackslashes($path) {
 125              $path = str_replace("\\", "/", $path);
 126              return $this->capitalizeDriveLetter($path);
 127          }
 128          /*}}}*/
 129          /*{{{ public function capitalizeDriveLetter() */
 130  
 131          /**
 132           * Convert the drive letter to upper case
 133           *
 134           * @param $path Windows path with "c:<blah>"
 135           * @return String Path with driver letter capitalized.
 136           * @access public
 137          */
 138          public function capitalizeDriveLetter($path) {
 139              if(strpos($path, ":") === 1) {
 140                  $path = strtoupper(substr($path, 0, 1)) . substr($path, 1);
 141              }
 142              return $path;
 143          }
 144  
 145          /*}}}*/
 146          /*{{{ public function makeDirRecursive() */
 147          /**
 148           * Make directory recursively.
 149           * (Taken from: http://aidan.dotgeek.org/lib/?file=function.mkdirr.php)
 150           *
 151           * @param $dir Directory path to create
 152           * @param $mode=0755
 153           * @return True on success, False on failure
 154           * @access public
 155          */
 156          public function makeDirRecursive($dir, $mode=0755) {
 157              // Check if directory already exists
 158              if (is_dir($dir) || empty($dir)) {
 159                  return true;
 160              }
 161  
 162              // Ensure a file does not already exist with the same name
 163              if (is_file($dir)) {
 164                  $this->getLogger()->debug("File already exists: " . $dir,
 165                      __FILE__, __LINE__);
 166                  return false;
 167              }
 168  
 169              $dir = $this->replaceBackslashes($dir);
 170  
 171              // Crawl up the directory tree
 172              $next_pathname = substr($dir, 0, strrpos($dir, "/"));
 173              if ($this->makeDirRecursive($next_pathname, $mode)) {
 174                  if (!file_exists($dir)) {
 175                      return mkdir($dir, $mode);
 176                  }
 177              }
 178  
 179              return false;
 180          }
 181          /*}}}*/
 182          /*{{{ public function getOS() */
 183          /**
 184           * Returns the current OS code
 185           * WIN - Windows, LIN -Linux, etc.
 186           *
 187           * @return String 3 letter code for current OS
 188           * @access public
 189           * @since  0.6.6
 190          */
 191          public function getOS() {
 192              return strtoupper(substr(PHP_OS, 0, 3));
 193          }
 194          /*}}}*/
 195          /*{{{ public function getTmpDir() */
 196  
 197          public function getTmpDir() {
 198              global $spc_config;
 199              $OS = $this->getOS();
 200              switch($OS) {
 201              case "WIN":
 202                  return $spc_config['windows_tmpdir'];
 203              default:
 204                  return $spc_config['tmpdir'];
 205              }
 206          }
 207  
 208          /*}}}*/
 209          /*{{{ public function getLogger() */
 210  
 211          public function getLogger($package=false) {
 212              global $spc_config;
 213              if(!isset($this->logger) || $this->logger == NULL) {
 214                  $this->logger =& new Logger();
 215                  $this->logger->setLevel($spc_config["log_level"]);
 216              }
 217              return $this->logger;
 218          }
 219  
 220          /*}}}*/
 221      }
 222  
 223      global $util;
 224      $util = new Utility();
 225  
 226  ?>


Generated: Tue Dec 2 03:54:09 2008 Cross-referenced by PHPXref 0.7