vendor/tedivm/stash/src/Stash/Utilities.php line 227

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Stash package.
  4.  *
  5.  * (c) Robert Hafner <tedivm@tedivm.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Stash;
  11. use Stash\Exception\RuntimeException;
  12. use Stash\Exception\InvalidArgumentException;
  13. use Stash\Interfaces\DriverInterface;
  14. /**
  15.  * StashUtilities contains static functions used throughout the Stash project, both by core classes and drivers.
  16.  *
  17.  * @package Stash
  18.  * @author  Robert Hafner <tedivm@tedivm.com>
  19.  */
  20. class Utilities
  21. {
  22.     /**
  23.      * Various drivers use this to define what kind of encoding to use on objects being cached. It needs to be revamped
  24.      * a bit.
  25.      */
  26.     public static function encoding($data)
  27.     {
  28.         if (is_scalar($data)) {
  29.             if (is_bool($data)) {
  30.                 return 'bool';
  31.             }
  32.             if (is_numeric($data)) {
  33.                 if (is_numeric($data) && ($data >= 2147483648 || $data < -2147483648)) {
  34.                     return 'serialize';
  35.                 } else {
  36.                     return 'numeric';
  37.                 }
  38.             }
  39.             return 'string';
  40.         }
  41.         return 'serialize';
  42.     }
  43.     /**
  44.      * Uses the encoding function to define an encoding and uses it on the data. This system is going to be revamped.
  45.      */
  46.     public static function encode($data)
  47.     {
  48.         switch (self::encoding($data)) {
  49.             case 'bool':
  50.                 return $data 'true' 'false';
  51.             case 'serialize':
  52.                 return serialize($data);
  53.             case 'string':
  54.             case 'none':
  55.             default:
  56.         }
  57.         return $data;
  58.     }
  59.     /**
  60.      * Takes a piece of data encoded with the 'encode' function and returns it's actual value.
  61.      *
  62.      */
  63.     public static function decode($data$method)
  64.     {
  65.         switch ($method) {
  66.             case 'bool':
  67.                 // note that true is a string, so this
  68.                 return $data == 'true' true false;
  69.             case 'serialize':
  70.                 return unserialize($data);
  71.             case 'string':
  72.             case 'none':
  73.             default:
  74.         }
  75.         return $data;
  76.     }
  77.     /**
  78.      * Returns the default base directory for the system when one isn't provided by the developer. This is a directory
  79.      * of last resort and can cause problems if one library is shared by multiple projects. The directory returned
  80.      * resides in the system's temp folder and is specific to each Stash installation and driver.
  81.      *
  82.      * @param  DriverInterface $driver
  83.      * @return string          Path for Stash files
  84.      */
  85.     public static function getBaseDirectory(DriverInterface $driver null)
  86.     {
  87.         $tmp rtrim(sys_get_temp_dir(), '/\\') . '/';
  88.         $baseDir $tmp 'stash/' md5(__DIR__) . '/';
  89.         if (isset($driver)) {
  90.             $baseDir .= str_replace(array('/''\\'), '_'get_class($driver)) . '/';
  91.         }
  92.         if (!is_dir($baseDir)) {
  93.             mkdir($baseDir0770true);
  94.         }
  95.         return $baseDir;
  96.     }
  97.     /**
  98.      * Deletes a directory and all of its contents.
  99.      *
  100.      * @param  string $file Path to file or directory.
  101.      * @param  bool $cleanParent Whether to prune empty parent directories or not.
  102.      * @throws Exception\RuntimeException
  103.      * @return bool                       Returns true on success, false otherwise.
  104.      */
  105.     public static function deleteRecursive($file$cleanParent false)
  106.     {
  107.         if (!preg_match('/^(?:\/|\\\\|\w:\\\\|\w:\/).*$/'$file)) {
  108.             throw new RuntimeException('deleteRecursive function requires an absolute path.');
  109.         }
  110.         $badCalls = array('/''/*''/.''/..');
  111.         if (in_array($file$badCalls)) {
  112.             throw new RuntimeException('deleteRecursive function does not like that call.');
  113.         }
  114.         $filePath rtrim($file' /');
  115.         if (is_dir($filePath)) {
  116.             $currentPerms fileperms($filePath);
  117.             $currentOwner fileowner($filePath);
  118.             $parentPath dirname($filePath);
  119.             $directoryIt = new \RecursiveDirectoryIterator($filePath);
  120.             foreach (new \RecursiveIteratorIterator($directoryIt\RecursiveIteratorIterator::CHILD_FIRST) as $file) {
  121.                 $filename $file->getPathname();
  122.                 // Clear out children if this is a directory.
  123.                 if ($file->isDir()) {
  124.                     $dirFiles scandir($file->getPathname());
  125.                     if ($dirFiles && count($dirFiles) == 2) {
  126.                         $filename rtrim($filename'/.');
  127.                         if (file_exists($filename)) {
  128.                             rmdir($filename);
  129.                         }
  130.                     }
  131.                     unset($dirFiles);
  132.                     continue;
  133.                 }
  134.                 if (file_exists($filename)) {
  135.                     unlink($filename);
  136.                 }
  137.             }
  138.             unset($directoryIt);
  139.             if (is_dir($filePath)) {
  140.                 rmdir($filePath);
  141.             }
  142.             if ($cleanParent && static::checkForEmptyDirectory($parentPath)) {
  143.                 $parentPerms fileperms($parentPath);
  144.                 $parentOwner fileowner($parentPath);
  145.                 if ($currentPerms == $parentPerms && $currentOwner == $parentOwner) {
  146.                     $grandParentPath dirname($parentPath);
  147.                     if ($parentOwner fileowner($grandParentPath)) {
  148.                         if (is_writable($grandParentPath) && $parentPerms == fileperms($grandParentPath)) {
  149.                             rmdir($parentPath);
  150.                         }
  151.                     }
  152.                 }
  153.             }
  154.             return true;
  155.         } elseif (is_file($filePath)) {
  156.             return unlink($file);
  157.         }
  158.         return false;
  159.     }
  160.     public static function normalizeKeys($keys$hash 'md5')
  161.     {
  162.         $pKey = array();
  163.         foreach ($keys as $keyPiece) {
  164.             $prefix substr($keyPiece01) == '@' '@' '';
  165.             $pKeyPiece $prefix $hash($keyPiece);
  166.             $pKey[] = $pKeyPiece;
  167.         }
  168.         return $pKey;
  169.     }
  170.     /**
  171.      * Checks to see whether the requisite permissions are available on the specified path.
  172.      *
  173.      * @throws Exception\RuntimeException
  174.      * @throws Exception\InvalidArgumentException
  175.      */
  176.     public static function checkFileSystemPermissions($path null$permissions)
  177.     {
  178.         if (!isset($path)) {
  179.             throw new RuntimeException('Cache path was not set correctly.');
  180.         }
  181.         if (file_exists($path) && !is_dir($path)) {
  182.             throw new InvalidArgumentException('Cache path is not a directory.');
  183.         }
  184.         if (!is_dir($path) && !@mkdir($path$permissionstrue)) {
  185.             throw new InvalidArgumentException('Failed to create cache path.');
  186.         }
  187.         if (!is_writable($path)) {
  188.             throw new InvalidArgumentException('Cache path is not writable.');
  189.         }
  190.     }
  191.     /**
  192.      * Checks to see if a directory is empty.
  193.      *
  194.      * @param  string $path
  195.      * @return bool
  196.      */
  197.     public static function checkForEmptyDirectory($path)
  198.     {
  199.         $empty true;
  200.         $dir opendir($path);
  201.         while ($file readdir($dir)) {
  202.             if ($file != '.' && $file != '..') {
  203.                 $empty false;
  204.                 break;
  205.             }
  206.         }
  207.         closedir($dir);
  208.         return $empty;
  209.     }
  210. }