Last active
January 23, 2022 07:12
-
-
Save WinterSilence/011e8f8a613c463082c54f966b5143b0 to your computer and use it in GitHub Desktop.
Extended plugin for Webasyst framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
да и в один класс не уложиться
А этого и не нужно, класс плагина по сути реализует паттерн Facade. Все методы waExtendedPlugin (кроме getThemeTemplate, я добавил его только для твоего случая) относятся непосредственно к плагину и поэтому находятся в классе плагина.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Концепция сборки нужных методов тоже хороша, но собирать лень, да и в один класс не уложиться