Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php // http://stackoverflow.com/questions/34066827/is-there-a-php-equivalent-to-base-href
- namespace app\system;
- class AppFilePath
- {
- const FULL_PATH = true;
- private $appHomeRoot = '';
- private $documentRoot = '';
- private $appWebRoot = '';
- private $appWebBase = ''; // holds the difference in directory path
- private $hostname = 'localhost';
- private $protocol = 'http://';
- private static $instance = null;
- /**
- * The defaults provided here are setup as part of my 'application bootstrap'.
- *
- * @param type $appHomeRoot
- * @param type $appWebRoot
- * @param type $documentRoot
- * @param type $hostname
- * @return \self
- */
- public static function newInstance($appHomeRoot = APP_HOME_ROOT,
- $appWebRoot = APP_WEB_ROOT,
- $documentRoot = APP_DOCUMENT_ROOT,
- $hostname = '')
- {
- return new self($appHomeRoot, $appWebRoot, $documentRoot, $hostname );
- }
- /**
- * Generate an Absolute URL path based on 'DOCUMENT_ROOT' and the
- * path to the 'APP_WEB_ROOT' (where the web source code lives)
- *
- * @param type $appHomeRoot -- root directory of the application
- * @param type $appWebRoot -- root directory of the web source code
- * @param type $documentRoot -- $_SERVER['DOCUMENT_ROOT'] if available,
- * default: $appWebRoot if not provided.
- * @param type $hostname -- domain name -
- * $_SERVER['HTTP_HOST'] if available,
- * Default: 'localhost' if not provided
- *
- */
- public function __construct($appHomeRoot,
- $appWebRoot,
- $documentRoot = '',
- $hostname = '')
- {
- $this->appHomeRoot = toSlash($appHomeRoot);
- $this->appWebRoot = toSlash($appWebRoot);
- $this->documentRoot = toSlash($documentRoot);
- $this->hostname = 'localhost';
- if (empty($documentRoot)) {
- if (!empty($_SERVER['DOCUMENT_ROOT'])) {
- $this->documentRoot = toSlash($_SERVER['DOCUMENT_ROOT']);
- }
- else {
- $this->documentRoot = $this->appWebRoot;
- }
- }
- if (stripos($this->appWebRoot, $this->documentRoot)=== 0) {
- $this->appWebBase = trim(substr($this->appWebRoot, strlen($this->documentRoot)), '/');
- }
- if (!empty($hostname)) {
- $this->hostname = $hostname;
- }
- elseif (!empty($_SERVER['HTTP_HOST'])) {
- $this->hostname = $_SERVER['HTTP_HOST'];
- }
- }
- /**
- * Convert a relative path / filename to an absolute URL Path based on Document Root
- *
- * Optionally add the http://domain/
- *
- * Note the webroot may NOT be the same as document root! This happens
- * when webhosting sometimes.
- *
- * This routine always returns a absolute URL to the file based on document root
- * but assumes the file is based from 'appWebRoot'.
- *
- * Useful when my 'web' directory is not the same as 'DOCUMENT_ROOT'
- *
- * Example: DocumentRoot => /home/app
- * Webroot => /home/app/www
- *
- * filepath => images/picture.jpg
- *
- * Url => /www/images/picture.jpg
- *
- * @param string $filePath - relative path to file based on webroot
- * @param boolean $fullUrl - return Url including domain
- * @param boolean $checkFileExists - throw \InvalidArgumentException
- * @throws ErrorException
- * @return string
- */
- public function appUrl($filePath,
- $fullUrl = self::FULL_PATH,
- $checkFileExists = true)
- {
- $fullFilePath = addSlash($this->appWebRoot) . ltrim(toSlash($filePath), '/');
- // does the file exist?
- if ($checkFileExists) {
- $fi = new \SplFileInfo($fullFilePath);
- $realPath = toSlash($fi->getRealPath());
- if (empty($realPath)) {
- throw new \InvalidArgumentException($fullFilePath .' does not exist.', 500);
- }
- }
- $url = '/';
- $strPos = stripos($fullFilePath, $this->documentRoot);
- if ($strPos === 0) {
- $url = trim(substr($fullFilePath, strlen($this->documentRoot)), '/');
- }
- if ($fullUrl) {
- $url = $this->protocol . addSlash($this->hostname) . $url;
- }
- else {
- $url = '/'. $url;
- }
- return $url;
- }
- /**
- * Generate an absolute filepath to anywhere in the application
- *
- * You supply a path based on the 'home root'
- *
- * e.g. 'config/dev.ini'
- *
- * @param string $filepath
- * @param boolean $checkFileExists
- * @return string -- absolute path to the file
- * To create a file then pass false
- */
- public function appHomeRoot($filepath = '', $checkFileExists = true)
- {
- if (empty($filepath)) {
- return $this->appHomeRoot;
- }
- return $this->realFilePath($filepath, $this->appHomeRoot, $checkFileExists);
- }
- /**
- * Generate an absolute filepath to anywhere in the web source code directory
- *
- * You supply a path based on the 'web root'
- *
- * e.g. 'script/mytheme.js'
- *
- * @param string $filepath
- * @param boolean $checkFileExists
- * @return string
- */
- public function appWebRoot($filepath = '', $checkFileExists = true)
- {
- if (empty($filepath)) {
- return $this->appWebRoot;
- }
- return $this->realFilePath($filepath, $this->appWebRoot, $checkFileExists);
- }
- /**
- * Generate an absolute filepath to anywhere in the web source code directory
- *
- * You supply a path based on the 'document root'
- *
- * e.g. 'script/mytheme.js'
- *
- * @param type $filepath
- * @param type $checkFileExists
- * @return string
- */
- public function appDocumentRoot($filepath = '', $checkFileExists = true)
- {
- if (empty($filepath)) {
- return $this->documentRoot;
- }
- return $this->realFilePath($filepath, $this->documentRoot, $checkFileExists);
- }
- /**
- * Difference between APP_WEB_ROOT and APP_DOCUMENT_ROOT.
- *
- * The router needs this as may or may not be supplied in the url
- *
- * @return string
- */
- public function appWebBase()
- {
- return $this->appWebBase;
- }
- public function hostname($addSlash = false)
- {
- return $addSlash ? addSlash($this->hostname) : $this->hostname;
- }
- public function setHostname($hostname)
- {
- $this->hostname = $hostname;
- }
- /**
- * The routine that creates an absolute path to anywhere
- *
- * @param string $filePath
- * @param string $directory
- * @param boolean $checkFileExists
- * @return string
- * @throws \InvalidArgumentException
- */
- public function realFilePath($filePath,
- $directory,
- $checkFileExists = true)
- {
- $fullFilePath = addSlash($directory) . ltrim(toSlash($filePath), '/');
- // does the file exist?
- if ($checkFileExists) {
- $fi = new \SplFileInfo($fullFilePath);
- $realPath = toSlash($fi->getRealPath());
- if (empty($realPath)) {
- throw new \InvalidArgumentException($fullFilePath .' does not exist.', 500);
- }
- }
- return $fullFilePath;
- }
- /**
- * Allow static call by missing the leading 'app'
- *
- * e.e AppFilePath::homeRoot(filepath);
- *
- * @param string $name
- * @param string $arguments
- * @return string
- */
- public static function __callStatic($name, $arguments)
- {
- if (is_null(self::$instance)) {
- self::$instance = self::newInstance();
- }
- $method = 'app'. ucfirst($name);
- if (method_exists(self::$instance, $method)) {
- return call_user_func_array(array(self::$instance, $method), $arguments);
- }
- else {
- throw new \InvalidArgumentException('app'. $name .' method does not exist.', 500);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement