2013-10-02 18:14:53 +02:00
|
|
|
<?php
|
|
|
|
namespace Rails\ActionView;
|
|
|
|
|
|
|
|
use Rails;
|
|
|
|
|
|
|
|
class ViewHelpers
|
|
|
|
{
|
|
|
|
const BASE_HELPER_NAME = 'Rails\ActionView\Helper\Base';
|
|
|
|
|
|
|
|
static protected $registry = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helpers instances.
|
|
|
|
*/
|
|
|
|
static protected $helpers = [];
|
|
|
|
|
|
|
|
static protected $helpersLoaded = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helpers queue list. They will be available in this order.
|
|
|
|
*/
|
|
|
|
static protected $queue = [];
|
|
|
|
|
2013-10-03 04:03:33 +02:00
|
|
|
static protected $appQueue = [];
|
|
|
|
|
2013-10-02 18:14:53 +02:00
|
|
|
/**
|
|
|
|
* Searches for the helper that owns $method.
|
|
|
|
*
|
|
|
|
* @return object | false
|
|
|
|
*/
|
|
|
|
static public function findHelperFor($method)
|
|
|
|
{
|
|
|
|
if (isset(self::$registry[$method])) {
|
|
|
|
return self::$helpers[self::$registry[$method]];
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (self::$helpers as $helperName => $helper) {
|
|
|
|
if (method_exists($helper, $method)) {
|
|
|
|
self::$registry[$method] = $helperName;
|
|
|
|
return $helper;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static public function getBaseHelper()
|
|
|
|
{
|
|
|
|
return self::getHelper(self::BASE_HELPER_NAME);
|
|
|
|
}
|
|
|
|
|
|
|
|
static public function getHelper($name)
|
|
|
|
{
|
|
|
|
return self::$helpers[$name];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds class names to the helper queues. These classes will be instantiated
|
|
|
|
* in includeHelpers().
|
|
|
|
*
|
|
|
|
* @var name string helper name
|
|
|
|
* @see load()
|
|
|
|
*/
|
|
|
|
static public function addHelper($className)
|
|
|
|
{
|
|
|
|
self::$queue[] = $className;
|
|
|
|
}
|
|
|
|
|
|
|
|
static public function addHelpers(array $classNames)
|
|
|
|
{
|
|
|
|
self::$queue = array_merge(self::$queue, $className);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For application helpers. Class names passed will be appended with "Helper".
|
2013-10-03 04:03:33 +02:00
|
|
|
* This should only be called by Rails.
|
2013-10-02 18:14:53 +02:00
|
|
|
*/
|
|
|
|
static public function addAppHelpers(array $helpers)
|
|
|
|
{
|
2013-10-03 04:03:33 +02:00
|
|
|
self::$appQueue = array_merge(self::$appQueue, array_map(function($c) { return $c . 'Helper'; }, $helpers));
|
2013-10-02 18:14:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Actually include the helpers files.
|
|
|
|
*
|
|
|
|
* Application and current controller's helper are added here,
|
|
|
|
* to make sure they're top on the list.
|
|
|
|
*/
|
|
|
|
static public function load()
|
|
|
|
{
|
|
|
|
if (!self::$helpersLoaded) {
|
|
|
|
if (($router = Rails::application()->dispatcher()->router()) && ($route = $router->route())) {
|
|
|
|
$controllerHelper = Rails::services()->get('inflector')->camelize($route->controller()) . 'Helper';
|
2013-10-03 04:03:33 +02:00
|
|
|
array_unshift(self::$appQueue, $controllerHelper);
|
2013-10-02 18:14:53 +02:00
|
|
|
}
|
|
|
|
$appHelper = 'ApplicationHelper';
|
2013-10-03 04:03:33 +02:00
|
|
|
array_unshift(self::$appQueue, $appHelper);
|
2013-10-02 18:14:53 +02:00
|
|
|
|
2013-10-03 04:03:33 +02:00
|
|
|
foreach (array_unique(self::$appQueue) as $name) {
|
|
|
|
self::loadHelper($name);
|
|
|
|
}
|
2013-10-02 18:14:53 +02:00
|
|
|
foreach (array_unique(self::$queue) as $name) {
|
2013-10-03 04:03:33 +02:00
|
|
|
self::loadHelper($name, true);
|
2013-10-02 18:14:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Add base helper
|
|
|
|
self::$helpers[self::BASE_HELPER_NAME] = new Helper\Base();
|
|
|
|
|
|
|
|
self::$helpersLoaded = true;
|
|
|
|
}
|
|
|
|
}
|
2013-10-03 04:03:33 +02:00
|
|
|
|
|
|
|
static protected function loadHelper($name, $throwE = false)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
Rails::loader()->loadClass($name);
|
|
|
|
self::$helpers[$name] = new $name();
|
|
|
|
} catch (Rails\Loader\Exception\ExceptionInterface $e) {
|
|
|
|
if ($throwE) {
|
|
|
|
throw new Exception\RuntimeException(
|
|
|
|
sprintf("Couldn't load file for helper %s", $name)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-02 18:14:53 +02:00
|
|
|
}
|