Skip to content

Instantly share code, notes, and snippets.

@unwelt
Last active July 29, 2021 04:01
Show Gist options
  • Select an option

  • Save unwelt/8204cb01a0a665b0df30feeeb03258c5 to your computer and use it in GitHub Desktop.

Select an option

Save unwelt/8204cb01a0a665b0df30feeeb03258c5 to your computer and use it in GitHub Desktop.
UMI.CMS Подключение custom класса из папки шаблонов.

По умолчанию, расширение custom не предусмотрено системой, но это можно легко исправить, внеся правки в файл /classes/components/custom.php. После этого, можно перенести методы кастомных классов в классы, хранящиеся в папке шаблонов - /templates/<имя_шаблона>/classes/(components|modules)/custom/class.php. Необходимо помнить про именование классов:

  • Папка components - CustomCustom
  • Папка modules - custom_custom
<?php
class custom extends def_module
{
private const COMPONENTS_CUSTOM_CLASS_NAME = 'CustomCustom';
private const MODULES_CUSTOM_CLASS_NAME = 'custom_custom';
public function __construct()
{
parent::__construct();
$this->loadTemplateCustoms();
}
public function cms_callMethod($method_name, $args)
{
return call_user_func_array([$this, $method_name], $args);
}
public function __call($method, $args)
{
if (!$this->isMethodExistsInCustom($method)) {
throw new publicException('Method ' . get_class($this) . '::' . $method . " doesn't exist");
}
return parent::__call($method, $args);
}
/**
* Проверка наличия метода у классов расширяющих кастомый класс
*
* @param string $method
* @return bool
*/
private function isMethodExistsInCustom(string $method): bool
{
return $this->isClassHasMethod(self::COMPONENTS_CUSTOM_CLASS_NAME, $method)
|| $this->isClassHasMethod(self::MODULES_CUSTOM_CLASS_NAME, $method);
}
/**
* Проверка наличия метода у класса
*
* @param string $className
* @param string $methodName
* @return bool
*/
private function isClassHasMethod(string $className, string $methodName): bool
{
try {
$classInstance = $this->getImplementedInstance($className);
} catch (Exception $exception) {
return false;
}
return method_exists($classInstance, $methodName);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment