Advertisement
rfv123

AppFilePath.php

Dec 4th, 2015
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.59 KB | None | 0 0
  1. <?php // http://stackoverflow.com/questions/34066827/is-there-a-php-equivalent-to-base-href
  2.  
  3. namespace app\system;
  4.  
  5. class AppFilePath
  6. {
  7.     const FULL_PATH = true;
  8.  
  9.     private $appHomeRoot  = '';
  10.  
  11.     private $documentRoot = '';
  12.  
  13.     private $appWebRoot = '';
  14.  
  15.     private $appWebBase = ''; // holds the difference in directory path
  16.                                
  17.  
  18.     private $hostname = 'localhost';
  19.  
  20.     private $protocol = 'http://';
  21.  
  22.     private static $instance = null;
  23.  
  24.     /**
  25.      * The defaults provided here are setup as part of my 'application bootstrap'.
  26.      *
  27.      * @param type $appHomeRoot
  28.      * @param type $appWebRoot
  29.      * @param type $documentRoot
  30.      * @param type $hostname
  31.      * @return \self
  32.      */
  33.     public static function newInstance($appHomeRoot   = APP_HOME_ROOT,
  34.                                            $appWebRoot    = APP_WEB_ROOT,
  35.                                            $documentRoot  = APP_DOCUMENT_ROOT,
  36.                                            $hostname      = '')
  37.     {
  38.         return new self($appHomeRoot, $appWebRoot, $documentRoot,  $hostname );
  39.     }
  40.  
  41.  
  42.     /**
  43.      * Generate an Absolute URL path based on 'DOCUMENT_ROOT' and the
  44.      *      path to the 'APP_WEB_ROOT' (where the web source code lives)
  45.      *
  46.      * @param type $appHomeRoot   -- root directory of the application
  47.      * @param type $appWebRoot    -- root directory of the web source code
  48.      * @param type $documentRoot  -- $_SERVER['DOCUMENT_ROOT'] if available,
  49.      *                                default: $appWebRoot if not provided.
  50.      * @param type $hostname      -- domain name -
  51.      *                                  $_SERVER['HTTP_HOST'] if available,
  52.      *                                  Default: 'localhost' if not provided
  53.      *
  54.      */
  55.     public function __construct($appHomeRoot,
  56.                                    $appWebRoot,
  57.                                    $documentRoot = '',
  58.                                    $hostname  = '')
  59.     {
  60.         $this->appHomeRoot  = toSlash($appHomeRoot);
  61.         $this->appWebRoot   = toSlash($appWebRoot);
  62.         $this->documentRoot = toSlash($documentRoot);
  63.         $this->hostname = 'localhost';
  64.  
  65.         if (empty($documentRoot)) {
  66.             if (!empty($_SERVER['DOCUMENT_ROOT'])) {
  67.                 $this->documentRoot = toSlash($_SERVER['DOCUMENT_ROOT']);
  68.             }
  69.             else {
  70.                 $this->documentRoot = $this->appWebRoot;
  71.             }
  72.         }
  73.  
  74.         if (stripos($this->appWebRoot, $this->documentRoot)=== 0) {
  75.             $this->appWebBase = trim(substr($this->appWebRoot, strlen($this->documentRoot)), '/');
  76.         }
  77.  
  78.         if (!empty($hostname)) {
  79.             $this->hostname = $hostname;
  80.         }
  81.         elseif (!empty($_SERVER['HTTP_HOST'])) {
  82.             $this->hostname = $_SERVER['HTTP_HOST'];
  83.         }
  84.      }
  85.  
  86.     /**
  87.      * Convert a relative path / filename to an absolute URL Path based on Document Root
  88.      *
  89.      * Optionally add the http://domain/
  90.      *
  91.      * Note the webroot may NOT be the same as document root! This happens
  92.      * when webhosting sometimes.
  93.      *
  94.      * This routine always returns a absolute URL to the file based on document root
  95.      * but assumes the file is based from 'appWebRoot'.
  96.      *
  97.      * Useful when my 'web' directory is not the same as 'DOCUMENT_ROOT'
  98.      *
  99.      * Example:  DocumentRoot => /home/app
  100.      *           Webroot      => /home/app/www
  101.      *
  102.      *           filepath     => images/picture.jpg
  103.      *
  104.      *           Url          => /www/images/picture.jpg
  105.      *
  106.      * @param string $filePath - relative path to file based on webroot
  107.      * @param boolean $fullUrl - return Url including domain
  108.      * @param boolean $checkFileExists - throw \InvalidArgumentException
  109.      * @throws ErrorException
  110.      * @return string
  111.      */
  112.     public function appUrl($filePath,
  113.                           $fullUrl = self::FULL_PATH,
  114.                           $checkFileExists = true)
  115.     {
  116.  
  117.         $fullFilePath = addSlash($this->appWebRoot) . ltrim(toSlash($filePath), '/');
  118.         // does the file exist?
  119.         if ($checkFileExists) {
  120.             $fi = new \SplFileInfo($fullFilePath);
  121.             $realPath = toSlash($fi->getRealPath());
  122.             if (empty($realPath)) {
  123.                throw new \InvalidArgumentException($fullFilePath .' does not exist.', 500);
  124.             }
  125.         }
  126.  
  127.         $url = '/';
  128.         $strPos = stripos($fullFilePath, $this->documentRoot);
  129.         if ($strPos === 0) {
  130.             $url = trim(substr($fullFilePath, strlen($this->documentRoot)), '/');
  131.         }
  132.  
  133.         if ($fullUrl) {
  134.             $url = $this->protocol . addSlash($this->hostname) . $url;
  135.         }
  136.         else {
  137.             $url = '/'. $url;
  138.         }
  139.         return $url;
  140.     }
  141.  
  142.     /**
  143.      * Generate an absolute filepath to anywhere in the application
  144.      *
  145.      * You supply a path based on the 'home root'
  146.      *
  147.      * e.g. 'config/dev.ini'
  148.      *
  149.      * @param string $filepath
  150.      * @param boolean $checkFileExists
  151.      * @return string -- absolute path to the file
  152.      *                    To create a file then pass false
  153.      */
  154.     public function appHomeRoot($filepath = '', $checkFileExists = true)
  155.     {
  156.         if (empty($filepath)) {
  157.             return $this->appHomeRoot;
  158.         }
  159.         return $this->realFilePath($filepath, $this->appHomeRoot, $checkFileExists);
  160.     }
  161.  
  162.     /**
  163.      * Generate an absolute filepath to anywhere in the web source code directory
  164.      *
  165.      * You supply a path based on the 'web root'
  166.      *
  167.      * e.g. 'script/mytheme.js'
  168.      *
  169.      * @param string $filepath
  170.      * @param boolean $checkFileExists
  171.      * @return string
  172.      */
  173.     public function appWebRoot($filepath = '', $checkFileExists = true)
  174.     {
  175.         if (empty($filepath)) {
  176.             return $this->appWebRoot;
  177.         }
  178.         return $this->realFilePath($filepath, $this->appWebRoot, $checkFileExists);
  179.     }
  180.  
  181.     /**
  182.      * Generate an absolute filepath to anywhere in the web source code directory
  183.      *
  184.      * You supply a path based on the 'document root'
  185.      *
  186.      * e.g. 'script/mytheme.js'
  187.      *
  188.      * @param type $filepath
  189.      * @param type $checkFileExists
  190.      * @return string
  191.      */
  192.     public function appDocumentRoot($filepath = '', $checkFileExists = true)
  193.     {
  194.         if (empty($filepath)) {
  195.             return $this->documentRoot;
  196.         }
  197.         return $this->realFilePath($filepath, $this->documentRoot, $checkFileExists);
  198.     }
  199.  
  200.     /**
  201.      * Difference between APP_WEB_ROOT and APP_DOCUMENT_ROOT.
  202.      *
  203.      * The router needs this as may or may not be supplied in the url
  204.      *
  205.      * @return string
  206.      */
  207.     public function appWebBase()
  208.     {
  209.         return $this->appWebBase;
  210.     }
  211.  
  212.     public function hostname($addSlash = false)
  213.     {
  214.         return $addSlash ? addSlash($this->hostname) : $this->hostname;
  215.     }
  216.  
  217.     public function setHostname($hostname)
  218.     {
  219.         $this->hostname = $hostname;
  220.     }
  221.  
  222.     /**
  223.      * The routine that creates an absolute path to anywhere
  224.      *
  225.      * @param string $filePath
  226.      * @param string $directory
  227.      * @param boolean $checkFileExists
  228.      * @return string
  229.      * @throws \InvalidArgumentException
  230.      */
  231.     public function realFilePath($filePath,
  232.                                     $directory,
  233.                                     $checkFileExists = true)
  234.     {
  235.         $fullFilePath = addSlash($directory) . ltrim(toSlash($filePath), '/');
  236.         // does the file exist?
  237.         if ($checkFileExists) {
  238.             $fi = new \SplFileInfo($fullFilePath);
  239.             $realPath = toSlash($fi->getRealPath());
  240.             if (empty($realPath)) {
  241.                throw new \InvalidArgumentException($fullFilePath .' does not exist.', 500);
  242.             }
  243.         }
  244.  
  245.         return $fullFilePath;
  246.     }
  247.  
  248.     /**
  249.      * Allow static call by missing the leading 'app'
  250.      *
  251.      * e.e AppFilePath::homeRoot(filepath);
  252.      *
  253.      * @param string $name
  254.      * @param string $arguments
  255.      * @return string
  256.      */
  257.     public static function __callStatic($name, $arguments)
  258.     {
  259.         if (is_null(self::$instance)) {
  260.             self::$instance = self::newInstance();
  261.         }
  262.  
  263.         $method = 'app'. ucfirst($name);
  264.  
  265.         if (method_exists(self::$instance, $method)) {
  266.            return call_user_func_array(array(self::$instance, $method), $arguments);
  267.         }
  268.         else {
  269.             throw new \InvalidArgumentException('app'. $name .' method does not exist.', 500);
  270.         }
  271.     }
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement