1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
viewpathresolver.php
См. документацию.
1<?php
2
3namespace Bitrix\Main\Engine\View;
4
5use Bitrix\Main\Diag\Helper;
6use Bitrix\Main\Engine\View\Exception\FileNotExistsException;
7use Bitrix\Main\Engine\View\Exception\InvalidPathOnViewException;
8use Bitrix\Main\Engine\View\Exception\PathOutsideDocumentRootException;
9use Bitrix\Main\IO\Path;
10use Bitrix\Main\Loader;
11use Bitrix\Main\SystemException;
12
14{
15 private string $documentRoot;
16
17 public function __construct(
18 private string $viewPath,
19 private string $renderFunctionName,
20 )
21 {
22 $this->documentRoot = Loader::getDocumentRoot();
23 }
24
30 public function resolve(): string
31 {
32 $path = $this->getAbsolutePath();
33 if (!empty($path))
34 {
36 }
37
38 if (is_null($path))
39 {
40 throw new InvalidPathOnViewException($this->viewPath);
41 }
42 elseif (!file_exists($path))
43 {
45 }
46 elseif (!str_starts_with($path, $this->documentRoot))
47 {
48 throw new PathOutsideDocumentRootException($this->viewPath);
49 }
50
51 $pathOnDocumentRoot = substr(
52 $path,
53 strlen($this->documentRoot)
54 );
55
56 return $pathOnDocumentRoot;
57 }
58
59 private function getAbsolutePath(): ?string
60 {
61 if (str_starts_with($this->viewPath, '/'))
62 {
63 return $this->documentRoot . $this->viewPath;
64 }
65
66 return $this->getPathToViewOnModule();
67 }
68
69 private function getPathToViewOnModule(): ?string
70 {
71 $moduleId = $this->getModuleId();
72 $postfix = '';
73
74 $extension = pathinfo($this->viewPath, PATHINFO_EXTENSION);
75 if ($extension === '')
76 {
77 $postfix = '.php';
78 }
79 elseif ($extension !== 'php')
80 {
81 throw new SystemException('Invalid extension for view file: ' . $extension);
82 }
83
85 'modules/' . $moduleId . '/views/' . $this->viewPath . $postfix,
86 );
87 if ($absolutePath !== false)
88 {
89 return $absolutePath;
90 }
91
92 return null;
93 }
94
95 private function getModuleId(): string
96 {
97 $trace = Helper::getBackTrace();
98 foreach ($trace as $item)
99 {
100 if ($item['function'] === $this->renderFunctionName)
101 {
102 $path = Path::normalize($item['file']);
103 $parts = explode('/', $path);
104 $prevPart = null;
105 foreach ($parts as $part)
106 {
107 if ($part === 'lib')
108 {
109 if (isset($prevPart))
110 {
111 return $prevPart;
112 }
113 }
114
115 $prevPart = $part;
116 }
117 }
118 }
119
120 throw new SystemException('Cannot parse module id from path');
121 }
122}
$path
Определения access_edit.php:21
__construct(private string $viewPath, private string $renderFunctionName,)
Определения viewpathresolver.php:17
static normalize($path)
Определения path.php:22
static getDocumentRoot()
Определения loader.php:254
static getLocal($path, $root=null)
Определения loader.php:572
$absolutePath
Определения folder_edit.php:53
$moduleId
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393