Skip to content

Instantly share code, notes, and snippets.

@serebro
Last active April 15, 2019 09:00
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save serebro/8587256b67f5fc869bd7 to your computer and use it in GitHub Desktop.
Save serebro/8587256b67f5fc869bd7 to your computer and use it in GitHub Desktop.
Phalcon Widget
<?php
/* /app/bootstrap.php */
// Регистрация namespace Widgets для хранения виджетов
$loader = new \Phalcon\Loader();
$loader->registerNamespaces(
[
// ...
'Widgets' => '/../app/widgets'
// ...
]
);
$loader->register();
// Регистрация сервиса volt с дополнительной функцией "widget"
$voltService = function($view, $di) use($config) {
$volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);
$volt->setOptions(array(
'compiledPath' => $config->application->cacheDir,
'compiledSeparator' => '_',
'compiledExtension' => '.php'
));
/** @var \Phalcon\Mvc\View\Engine\Volt\Compiler $compiler */
$compiler = $volt->getCompiler();
$compiler->addFunction('widget', function($resolvedArgs, $exprArgs) use($compiler) {
$class = trim($compiler->expression($exprArgs[0]['expr']), "'");
$params = empty($exprArgs[1]['expr']) ? null : $compiler->expression($exprArgs[1]['expr']);
return "\\Widgets\\$class::widget($params)";
});
return $volt;
};
// Регистрация стандратного сервиса View
$di->set('view', [
'className' => '\Phalcon\Mvc\View',
'calls' => [
['method' => 'setViewsDir', 'arguments' => [
['type' => 'parameter', 'value' => '../app/views'],
]],
['method' => 'registerEngines', 'arguments' => [
['type' => 'parameter', 'value' => [
'.phtml' => 'Phalcon\Mvc\View\Engine\Php',
'.volt' => $voltService,
]],
]],
],
], true);
// Регистрация сервиса widgetView
$di->set('widgetView', [
'className' => '\Phalcon\Mvc\View',
'calls' => [
['method' => 'setViewsDir', 'arguments' => [
['type' => 'parameter', 'value' => '../app/widgets'],
]],
['method' => 'registerEngines', 'arguments' => [
['type' => 'parameter', 'value' => [
'.phtml' => 'Phalcon\Mvc\View\Engine\Php',
'.volt' => $voltService,
]],
]],
],
]);
{# /app/views/index/index.volt #}
<div>
{{ widget('Slider', ['slides': [
['link': '', 'img': '', 'alt': '', 'text': ''],
['link': '', 'img': '', 'alt': '', 'text': '']
]]) }}
</div>
<?php
/* /app/widgets/Slider.php */
namespace Widgets;
use Widget;
// Наш новый виджет слайдера
class Slider extends Widget
{
/** @var array */
public $slides = [];
public function run()
{
return $this->render(
'sliderView',
[
'slides' => $this->slides,
]
);
}
}
{# /app/widgets/views/sliderView.volt #}
<ul id="slider">
{% for slide in slides %}
<li>
<div class="slide">
<a href="{{ slide['link'] }}">
{{ image(slide['img'], 'alt': slide['alt']) }}
</a>
</div>
</li>
{% endfor %}
</ul>
<?php
/* /app/library/Widget.php */
// Базовый класс для виджета
use Phalcon\Mvc\View;
class Widget extends Phalcon\DI\Injectable
{
/** @var View */
private $_view;
public function __construct()
{
}
public function initialize()
{
}
public static function widget(array $params = [])
{
$class = get_called_class();
/* @var $widget Widget */
$widget = new $class();
foreach ($params as $key => $value) {
if (!property_exists($class, $key)) {
trigger_error(__METHOD__ . " property $class::$key is not defined.");
continue;
}
$widget->$key = $value;
}
$widget->initialize();
return $widget->run();
}
/**
* @return View
*/
public function getView()
{
if ($this->_view === null) {
$this->_view = $this->di->get('widgetView');
$this->_view->setDi($this->di);
}
return $this->_view;
}
/**
* @param View $view
* @return Widget
*/
public function setView(View $view)
{
$this->_view = $view;
return $this;
}
/**
* @param string $view_name
* @param array $params
* @return string
*/
public function render($view_name, array $params = [])
{
return $this->getView()->getRender('views', $view_name, $params);
}
/**
* @return string
*/
public function run()
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment