-
-
Save WinterSilence/011e8f8a613c463082c54f966b5143b0 to your computer and use it in GitHub Desktop.
<?php | |
/** | |
* Extended plugin for Webasyst framework. | |
*/ | |
abstract class waExtendedPlugin extends waPlugin | |
{ | |
/** | |
* @var waSystem | |
*/ | |
protected $wa; | |
/** | |
* @var string | |
*/ | |
protected $dataPath; | |
/** | |
* @var string | |
*/ | |
protected $dataUrl; | |
/** | |
* Creates plugin object. | |
* | |
* @param array $info the information about plugin | |
* @return void | |
*/ | |
public function __construct(array $info = []) | |
{ | |
parent::__construct($info); | |
$this->wa = wa($this->app_id); | |
} | |
/** | |
* Returns absolute path to plugin's directory/file. | |
* @param string $path the relative path | |
* @return string | |
*/ | |
public function getPath($path = '') | |
{ | |
if ($path) { | |
$path = '/' . trim($path, '\/'); | |
} | |
return $this->path . $path; | |
} | |
/** | |
* Get path to data folder of plugin. | |
* @param string $path | |
* @param bool $create | |
* @return string | |
*/ | |
public function getDataPath($path = '', $create = false) | |
{ | |
if (!$this->dataPath) { | |
$this->dataPath = $this->wa->getDataPath('plugins/' . $this->id, true); | |
} | |
if ($path) { | |
$path = '/' . ltrim($path, '\/'); | |
} | |
if ($create) { | |
return $this->wa->getDataPath('plugins/' . $this->id . $path, true); | |
} | |
return $this->dataPath . $path; | |
} | |
/** | |
* Get image URL. | |
* @param string $path | |
* @param string $size | |
* @return string | |
*/ | |
public function getDataUrl($path = '') | |
{ | |
if (!$this->dataUrl) { | |
$this->dataUrl = $this->wa->getDataUrl('plugins/' . $this->id . '/', true, $this->app_id, true); | |
} | |
if ($path) { | |
$path = ltrim($path, '/'); | |
} | |
return $this->dataUrl . $path; | |
} | |
/** | |
* Returns URL to plugin's backend action. | |
* @param string $action | |
* @param array $params | |
* @return string | |
*/ | |
public function getBackendActionUrl($action, array $params = []) | |
{ | |
return '?' . http_build_query(['plugin' => $this->id, 'action' => $action] + $params); | |
} | |
/** | |
* Returns rendered plugin's template. | |
* @param string $template path to template or template as string | |
* @param array $data the template variables | |
* @param bool $themeCopy if true, load theme template | |
* @return string | |
*/ | |
public function renderTemplate($template, array $data = [], $themeCopy = false) | |
{ | |
$view = $this->wa->getView(); | |
$data[$this->id] = $this; | |
$view->assign($data); | |
if (strpos($template, 'string:') !== 0 && !file_exists($template)) { | |
$extension = $view->getPostfix(); | |
if (pathinfo($template, PATHINFO_EXTENSION) !== ltrim($extension, '.')) { | |
$template .= $extension; | |
} | |
if ($themeCopy) { | |
$theme = new waTheme(waRequest::getTheme(), $this->app_id); | |
// Plugin templates in theme contains in directory "app/plugin" | |
$dir = $this->app_id . '/' . $this->id . '/' . pathinfo($template, PATHINFO_DIRNAME); | |
$dir = rtrim($dir, '/'); | |
$filename = pathinfo($template, PATHINFO_BASENAME); | |
$themeTemplate = $dir . '/' . $filename; | |
// If template not exists in theme, copy from plugin. | |
if (!$view->setThemeTemplate($theme, $themeTemplate)) { | |
waFiles::copy($this->getPath('templates/' . $template), $theme->getPath() . '/' . $dir); | |
$theme->addFile($themeTemplate, $this->getName())->save(); | |
} | |
$template = $themeTemplate; | |
} else { | |
$view->setTemplateDir($this->getPath('templates')); | |
} | |
} | |
return $view->fetch($template); | |
} | |
} |
Все плагины приложений, должны быть наследованы от класса плагигов приложения (shopPlugin,crmPlugin), которые наследованы от waPlugin
Я в курсе, это просто пример.
И куда ты эту абстракцию всунешь?
На деле же имеется аналог плагина dummy, в котором класс плагина выглядит как в данном примере и я оставляю те методы которые нужны в конкретном плагине (хотя в большинстве "больших" плагинов нужны все).
Данный класс решает намного больше задач, непосредственно к шаблонам относятся только 3 метода.
Концепция сборки нужных методов тоже хороша, но собирать лень, да и в один класс не уложиться
да и в один класс не уложиться
А этого и не нужно, класс плагина по сути реализует паттерн Facade. Все методы waExtendedPlugin (кроме getThemeTemplate, я добавил его только для твоего случая) относятся непосредственно к плагину и поэтому находятся в классе плагина.
Все плагины приложений, должны быть наследованы от класса плагигов приложения (shopPlugin,crmPlugin), которые наследованы от waPlugin. И куда ты эту абстракцию всунешь? единственный вариант брать такой класс в файлы плагина и там уже собирать его, а сам класс плагина наследовать от него, но это костыли!