Ejemplo n.º 1
0
 /**
  * Call function.
  *
  * ```php
  * $this->callFunction('\foo\Snippet');
  * $this->callFunction('\foo\FooController.get');
  * $this->callFunction(function{}());
  * $this->callFunction([Foo::className(), 'get']);
  * $this->callFunction([new Foo(), 'get']);
  * ```
  *
  * @param mixed $function may be a callable, snippet, and instance.
  * @param array $params
  * @return mixed
  */
 protected function callFunction($function, array $params = [])
 {
     if (is_string($function)) {
         $function = trim($function);
         if (strpos($function, '.') !== false) {
             $function = explode('.', $function);
         } else {
             return $this->template->getSnippet($function, $params);
         }
     }
     if (is_array($function)) {
         if ($function[0] === 'context') {
             $function[0] = $this->template->getContext();
             return call_user_func_array($function, $params);
         } elseif (is_string($function[0])) {
             if (class_exists('\\rock\\di\\Container') && Container::exists($function[0])) {
                 $function[0] = Container::load($function[0]);
             }
             return call_user_func_array($function, $params);
         }
     }
     return call_user_func_array($function, $params);
 }
Ejemplo n.º 2
0
 /**
  * Condition is empty.
  *
  * @param string $value value
  * @param array $params params
  *
  * - then: returns when value is empty.
  *
  * @param Template $template
  * @return string
  */
 public static function _empty($value = null, array $params, Template $template)
 {
     if (!empty($value)) {
         return $value;
     }
     //$template = clone $template;
     $placeholders = array_merge(!empty($params['addPlaceholders']) ? $template->findPlaceholders($params['addPlaceholders']) : [], ['output' => $value]);
     return $template->replaceByPrefix($params['then'], $placeholders);
 }
Ejemplo n.º 3
0
 /**
  * Get thumb.
  *
  * @param string $path src to image
  * @param array $params params:
  *
  * - type:     get `src`, `<a>`, `<img>` (default: `<img>`)
  * - w:        width
  * - h:        height
  * - q:        quality
  * - class:    attr `class`
  * - alt:      attr `alt`
  * - const
  * - dummy
  * @param Template $template
  * @return string
  * @throws \rock\helpers\InstanceException
  */
 public static function thumb($path, array $params, Template $template)
 {
     if (empty($path)) {
         if (empty($params['dummy'])) {
             return '';
         }
         $path = $params['dummy'];
     }
     $const = Helper::getValue($params['const'], 1, true);
     /** @var ImageProvider $imageProvider */
     $imageProvider = Instance::ensure(isset($params['imageProvider']) ? $params['imageProvider'] : 'imageProvider');
     $src = $imageProvider->get($path, Helper::getValue($params['w']), Helper::getValue($params['h']));
     if (!($const & ThumbInterface::WITHOUT_WIDTH_HEIGHT)) {
         $params['width'] = $imageProvider->width;
         $params['height'] = $imageProvider->height;
     }
     unset($params['h'], $params['w'], $params['type'], $params['const']);
     if (!empty($params['alt'])) {
         $params['alt'] = $template->replace($params['alt']);
     }
     return $const & ThumbInterface::OUTPUT_IMG ? Html::img($src, $params) : $src;
 }
Ejemplo n.º 4
0
<?php

use rock\base\Alias;
use rock\db\BatchQueryResult;
use rock\rbac\Permission;
use rock\rbac\Role;
use rock\Rock;
use rock\security\Security;
use rock\template\Template;
return array_merge(['route' => ['class' => \rock\route\Route::className()], 'access' => ['class' => \rock\access\Access::className()], 'behavior' => ['class' => \rock\components\Behavior::className()], 'db' => ['class' => \rock\db\Connection::className(), 'username' => 'root', 'password' => 'root', 'charset' => 'utf8', 'dsn' => 'mysql:host=localhost;dbname=rockdemo;charset=utf8', 'tablePrefix' => 'spt_', 'aliasSeparator' => '__'], 'BatchQueryResult' => ['class' => BatchQueryResult::className()], 'template' => ['class' => Template::className(), 'locale' => [\rock\LocaleProperties::className(), 'locale'], 'autoEscape' => Template::ESCAPE | Template::TO_TYPE, 'handlerLink' => function ($link, Template $template, array $params = []) {
    if (!($link = Alias::getAlias("@{$link}", [], false))) {
        return '#';
    }
    return $template->autoEscape(\rock\template\filters\BaseFilter::modifyUrl($link, $params));
}, 'extensions' => ['cfg' => function (array $keys) {
    return \rock\helpers\ArrayHelper::getValue(Rock::$config, $keys);
}, 'user' => function (array $keys) {
    if (current($keys) === 'isGuest') {
        return Rock::$app->user->isGuest();
    } elseif (in_array(current($keys), ['isLogged', 'isAuthenticated'], true)) {
        return !Rock::$app->user->isGuest();
    }
    return \rock\helpers\ArrayHelper::getValue(Rock::$app->user->getAll(), $keys);
}, 'call' => function (array $call, array $params = [], Template $template) {
    if (!isset($call[1])) {
        $call[1] = null;
    }
    list($class, $method) = $call;
    if ($class === 'context') {
        $object = $template->context;
        $function = [$object, $method];