48 $this->request = $request;
50 $this->component = $this->request->getQuery(
'c') ?:
null;
51 $this->mode = $this->request->getQuery(
'mode') ?:
null;
53 $this->action = $this->request->getQuery(
'action');
54 if ($this->action && is_string($this->action) && !$this->component)
56 list($this->vendor, $this->action) = $this->resolveVendor($this->action);
57 list(
$module, $this->action) = $this->resolveModuleAndAction($this->action);
63 private function resolveModuleAndAction(
$action)
65 $actionParts = explode(
'.',
$action);
66 $module = array_shift($actionParts);
67 $action = implode(
'.', $actionParts);
74 private function resolveVendor(
$action)
110 protected function buildComponent($componentName, $signedParameters =
null, $template =
null)
112 $class = \CBitrixComponent::includeComponentClass($componentName);
113 if (!is_subclass_of($class,
'CBitrixComponent'))
118 $parameters = array();
119 if ($signedParameters)
129 throw new SystemException(
130 "The component {$this->component} must be implement interface \Bitrix\Main\Engine\Contract\Controllerable",
131 self::EXCEPTION_INVALID_COMPONENT_INTERFACE
135 $component->initComponent($componentName, $template);
148 if ($this->component)
150 return $this->getComponentControllerAndAction();
153 $this->includeModule($this->module);
155 if ($controllerAndAction)
157 return $controllerAndAction;
163 return array(
new $ajaxClass, $this->action);
166 private function getComponentControllerAndAction()
168 $componentAsString = var_export($this->component,
true);
169 if ($this->mode === self::COMPONENT_MODE_CLASS)
171 $component = $this->buildComponent($this->component, $this->request->getPost(
'signedParameters'));
175 "Could not build component instance {$componentAsString}",
176 self::EXCEPTION_INVALID_COMPONENT
180 return array(
new ComponentController(
$component), $this->action);
182 elseif ($this->mode === self::COMPONENT_MODE_AJAX)
184 $ajaxClass = $this->includeComponentAjaxClass($this->component);
186 $controller = ControllerBuilder::build($ajaxClass, [
195 $modeAsString = var_export($this->mode,
true);
196 throw new SystemException(
197 "Unknown ajax mode ({$modeAsString}) to work {$componentAsString}",
198 self::EXCEPTION_INVALID_AJAX_MODE
203 private function includeModule(
$module)
207 throw new SystemException(
208 "Invalid module name {$module}",
209 self::EXCEPTION_INVALID_MODULE_NAME
213 if (!Configuration::getInstance(
$module)->
get(
'controllers'))
215 throw new SystemException(
216 "There is no configuration in {$module} with 'controllers' value.",
217 self::EXCEPTION_NO_CONFIGURATION
223 throw new SystemException(
"Could not find module {$module}", self::EXCEPTION_NO_MODULE);
227 private function includeComponentAjaxClass($name)
229 $path2Comp = \CComponentEngine::makeComponentPath($name);
230 if ($path2Comp ===
'')
232 throw new SystemException(
"{$name} is not a valid component name", self::EXCEPTION_INVALID_COMPONENT_NAME);
235 $componentPath = getLocalPath(
"components" . $path2Comp);
236 if ($componentPath ===
false)
238 throw new SystemException(
"Could not find component by name {$name}", self::EXCEPTION_NO_COMPONENT);
241 $ajaxClass = $this->getAjaxClassForPath($componentPath);
244 throw new SystemException(
"Could not find ajax class {$componentPath}", self::EXCEPTION_NO_COMPONENT_AJAX_CLASS);
250 private function getAjaxClassForPath($componentPath)
252 $filename = \Bitrix\Main\Application::getDocumentRoot() . $componentPath .
'/ajax.php';
253 if (!file_exists($filename) || !is_file($filename))
258 $beforeClasses = get_declared_classes();
259 $beforeClassesCount = count($beforeClasses);
260 include_once($filename);
261 $afterClasses = get_declared_classes();
262 $afterClassesCount = count($afterClasses);
263 $furthestClass =
null;
264 for ($i = $afterClassesCount - 1; $i >= $beforeClassesCount; $i--)
268 ($furthestClass && is_subclass_of($afterClasses[$i], $furthestClass))
271 $furthestClass = $afterClasses[$i];
275 return $furthestClass;
284 $this->request = $request;
static unsignParameters($componentName, $signedParameters)
static getControllerAndAction($vendor, $module, $action, $scope=Controller::SCOPE_AJAX)
const COMPONENT_MODE_AJAX
setRequest(HttpRequest $request)
const EXCEPTION_NO_MODULE
const COMPONENT_MODE_CLASS
const EXCEPTION_INVALID_COMPONENT_INTERFACE
const EXCEPTION_NO_CONFIGURATION
const EXCEPTION_NO_COMPONENT_AJAX_CLASS
const EXCEPTION_NO_COMPONENT
const EXCEPTION_INVALID_MODULE_NAME
const EXCEPTION_INVALID_COMPONENT
__construct(HttpRequest $request)
refineModuleName($vendor, $module)
const EXCEPTION_INVALID_COMPONENT_NAME
const EXCEPTION_INVALID_AJAX_MODE
static includeModule($moduleName)
static isValidModule(string $moduleName)