1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92:
<?php
/**
* MvcCore
*
* This source file is subject to the BSD 3 License
* For the full copyright and license information, please view
* the LICENSE.md file that are distributed with this source code.
*
* @copyright Copyright (c) 2016 Tom Flidr (https://github.com/mvccore)
* @license https://mvccore.github.io/docs/mvccore/5.0.0/LICENCE.md
*/
namespace MvcCore\View;
trait LocalMethods {
/**
* If relative path declared in view starts with `"./anything/else.phtml"`,
* then change relative path to correct `"./"` context and return full path.
* @param string $typePath
* @param string $relativePath
* @return string full path
*/
protected function correctRelativePath ($typePath, $relativePath) {
/** @var $this \MvcCore\View */
$result = str_replace('\\', '/', $relativePath);
// if relative path starts with dot:
if (mb_substr($relativePath, 0, 1) === '.') {
if (self::$_viewScriptsFullPathBase === NULL)
self::initViewScriptsFullPathBase();
$typedViewDirFullPath = implode('/', [
self::$_viewScriptsFullPathBase, $typePath
]);
// get current view script full path:
$renderedFullPaths = & $this->__protected['renderedFullPaths'];
$lastRenderedFullPath = $renderedFullPaths[count($renderedFullPaths) - 1];
// create `$renderedRelPath` by cutting directory with typed view scripts:
$renderedRelPath = mb_substr($lastRenderedFullPath, mb_strlen($typedViewDirFullPath));
// set how many dots is at `$relativePath` string start:
$startingDotsCount = mb_substr($relativePath, 1, 1) === '.' ? 2 : 1;
// cut so many slash steps from `$renderedRelPath` start,
// how many dots is at the `$relativePath` string start:
$slashSteps = 0;
while ($slashSteps++ < $startingDotsCount) {
$renderedRelPathLastSlashPos = mb_strrpos($renderedRelPath, '/');
if ($renderedRelPathLastSlashPos !== FALSE)
$renderedRelPath = mb_substr($renderedRelPath, 0, $renderedRelPathLastSlashPos);
}
// trim relative path for starting dots:
$relativePath = mb_substr($relativePath, $startingDotsCount);
// complete result from corected relative path and given path:
$result = ltrim($renderedRelPath . $relativePath, '/');
}
return $result;
}
/**
* Init view scripts full class string for methods:
* - `\MvcCore\View::GetViewScriptFullPath();`
* - `\MvcCore\View::correctRelativePath();`
* @return void
*/
protected static function initViewScriptsFullPathBase () {
$app = \MvcCore\Application::GetInstance();
self::$_viewScriptsFullPathBase = implode('/', [
$app->GetRequest()->GetAppRoot(),
$app->GetAppDir(),
$app->GetViewsDir()
]);
}
/**
* Static initialization to complete
* `static::$helpersNamespaces`
* by application configuration once.
* @return void
*/
protected static function initHelpersNamespaces () {
$app = \MvcCore\Application::GetInstance();
static::$helpersNamespaces = [
'\\MvcCore\\Ext\\Views\Helpers\\',
// and '\App\Views\Helpers\' by default:
'\\' . implode('\\', [
$app->GetAppDir(),
$app->GetViewsDir(),
static::$helpersDir
]) . '\\',
];
}
}