Skip to content

Commit

Permalink
add legacy support injections
Browse files Browse the repository at this point in the history
  • Loading branch information
coffeeich committed Mar 22, 2013
1 parent 3b79b7f commit 5bd12b8
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 42 deletions.
101 changes: 73 additions & 28 deletions classes/Kohana/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@ abstract class Kohana_Client {
* Статический метод для подключения контроллеров страниц в шаблоне макетов.
* Устанавливает нужный js-контроллер для страницы в зависимости от настроек в config'е client
* для текущего контроллера и экшена
* @param bool $needLegacyConf
* @see Client_Twig_Extension
* @example При использовании Kohana View нужно вставить перед закрывающим тэгом body:
* <?= Client::inject_controller_script() ?>
*
* @example При использовании Twig нужно вставить перед закрывающим тэгом body:
* {{ inject_controller_script() }}
* @return string
*/
static public function inject_controller_script() {
$data = self::get_client_page_data();
static public function inject_controller_script($needLegacyConf=FALSE) {
$data = self::get_client_page_data($needLegacyConf);

if (empty($data)) {
return;
return '';
}

$config = Kohana::$config->load('client');
Expand All @@ -37,10 +39,10 @@ static public function inject_controller_script() {
}

if (! $page) {
return;
return '';
}

$isDevelopment = self::is_development_controller_script();
$isDevelopment = self::is_development_controller_script($needLegacyConf);

$location = "/{$config->get($isDevelopment ? self::CLIENT_PAGE_TYPE_DEVELOPMENT : self::CLIENT_PAGE_TYPE_PRODUCTION)}";

Expand Down Expand Up @@ -69,6 +71,25 @@ static public function inject_controller_script() {
return HTML::script("{$location}/{$page}.js", array());
}

/**
* Статический метод для подключения скриптов поддержки устаревших браузеров в шаблоне макетов.
* Устанавливает js-контроллер для страницы в зависимости от настроек в config'е client в разделе legacy
* @see Client_Twig_Extension
* @example При использовании Kohana View нужно вставить перед закрывающим тэгом head:
* <!--[if lte IE 7]>
* <?= Client::inject_legacy_support_script() ?>
* <![endif]-->
*
* @example При использовании Twig нужно вставить перед закрывающим тэгом head:
* <!--[if lte IE 7]>
* {{ inject_legacy_support_script() }}
* <![endif]-->
* @return string
*/
static public function inject_legacy_support_script() {
return static::inject_controller_script(TRUE);
}

/**
* Статический метод для подключения стилей страниц в шаблоне макетов.
* Устанавливает нужный набор css/less тэгов для страницы в зависимости от настроек в config'е client
Expand All @@ -80,8 +101,8 @@ static public function inject_controller_script() {
* @example При использовании Twig нужно вставить перед закрывающим тэгом head:
* {{ inject_theme_styles() }}
*/
static public function inject_theme_styles() {
$data = self::get_client_page_data();
static public function inject_theme_styles($needLegacyConf=FALSE) {
$data = self::get_client_page_data($needLegacyConf);

if (empty($data)) {
return;
Expand Down Expand Up @@ -113,7 +134,7 @@ static public function inject_theme_styles() {
$theme = "skins/%s/%s/$theme";
}

$isDevelopment = self::is_development_controller_script();
$isDevelopment = self::is_development_controller_script($needLegacyConf);

$scripts = "/{$config->get($isDevelopment ? self::CLIENT_PAGE_TYPE_DEVELOPMENT : self::CLIENT_PAGE_TYPE_PRODUCTION)}";

Expand Down Expand Up @@ -142,14 +163,32 @@ static public function inject_theme_styles() {
return HTML::style($theme, array( 'rel' => $rel ));
}

/**
* Статический метод для подключения в шаблоне макетов стилей страниц для поддержки устаревших браузеров.
* Устанавливает нужный набор css/less тэгов для страницы в зависимости от настроек в config'е client в разделе legacy
* @see Client_Twig_Extension
* @example При использовании Kohana View нужно вставить перед закрывающим тэгом head:
* <!--[if lte IE 7]>
* <?= Client::inject_legacy_support_styles() ?>
* <![endif]-->
*
* @example При использовании Twig нужно вставить перед закрывающим тэгом head:
* <!--[if lte IE 7]>
* {{ inject_legacy_support_styles() }}
* <![endif]-->
*/
static public function inject_legacy_support_styles() {
return static::inject_theme_styles(TRUE);
}

/**
* Проверка режима работы скрипта на странице. Если в файле конфигурации
* сказано использовать билд, то вернет false, во всех остальных случаях - true.
* Это будет означать сто режим работы - разработка.
* @return bool
*/
static private function is_development_controller_script() {
$pageData = self::get_client_page_data();
static private function is_development_controller_script($needLegacyConf=FALSE) {
$pageData = self::get_client_page_data($needLegacyConf);

return ! (array_key_exists('use_build', $pageData) && $pageData['use_build']);
}
Expand All @@ -158,30 +197,36 @@ static private function is_development_controller_script() {
* Получение данных с настройками страницы из файла конфигурации.
* @return array
*/
static private function get_client_page_data() {
if (! array_key_exists(__METHOD__, self::$request_cache)) {
$pages = Kohana::$config->load('client')->get('pages');
static private function get_client_page_data($needLegacyConf=FALSE) {
$key = __METHOD__ . "$needLegacyConf";

if (! is_array($pages) || empty($pages)) {
self::$request_cache[__METHOD__] = array();
if (! array_key_exists($key, self::$request_cache)) {
if ($needLegacyConf) {
self::$request_cache[$key] = Kohana::$config->load('client')->get('legacy', array());
} else {
list($directory, $controller, $action) = array(
Request::$current->directory(),
Request::$current->controller(),
Request::$current->action()
);

if (strlen($directory) !== 0) {
$controller = $directory . '_' . $controller;
$pages = Kohana::$config->load('client')->get('pages');

if (! is_array($pages) || empty($pages)) {
self::$request_cache[$key] = array();
} else {
list($directory, $controller, $action) = array(
Request::$current->directory(),
Request::$current->controller(),
Request::$current->action()
);

if (strlen($directory) !== 0) {
$controller = $directory . '_' . $controller;
}

self::$request_cache[$key] = isset($pages[$controller][$action])
? $pages[$controller][$action]
: array();
}

self::$request_cache[__METHOD__] = isset($pages[$controller][$action])
? $pages[$controller][$action]
: array();
}
}

return self::$request_cache[__METHOD__];
return self::$request_cache[$key];
}

}
6 changes: 4 additions & 2 deletions classes/Kohana/Client/Twig/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ class Kohana_Client_Twig_Extension extends Twig_Extension {

public function getFunctions() {
return array(
'inject_controller_script' => new Twig_Function_Function('Client::inject_controller_script', array('is_safe' => array('html'))),
'inject_theme_styles' => new Twig_Function_Function('Client::inject_theme_styles', array('is_safe' => array('html'))),
'inject_controller_script' => new Twig_Function_Function('Client::inject_controller_script', array('is_safe' => array('html'))),
'inject_legacy_support_script' => new Twig_Function_Function('Client::inject_legacy_support_script', array('is_safe' => array('html'))),
'inject_theme_styles' => new Twig_Function_Function('Client::inject_theme_styles', array('is_safe' => array('html'))),
'inject_legacy_support_styles' => new Twig_Function_Function('Client::inject_legacy_support_styles', array('is_safe' => array('html'))),
);
}

Expand Down
32 changes: 20 additions & 12 deletions config/client.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
<?php

return array(
'loader' => '', //'frontend/loader', script which should initialize all require.js environment
'require' => '', //'frontend/require', require.js script path for development mode
'production' => '', //'frontend/builds', production builds path, which contains require.js builds with almond stubs
'development' => '', //'frontend/pages', development paged path with web pages entry points
'pages' => array(), // a list of web pages configutation, it's hash map like:
// 'Welcome' => array( - controller name (with directory subpath)
// 'index' => array( - action name
// 'use_build' => true, - use a script from builds dir
// 'page' => 'welcome_index', - use a script wich main name is 'welcome_index'
// 'theme' => 'default', - use a theme named 'default' (it's in '{pages,builds}/skins/welcome_index/{less,css}/default.{less,css}')
// ),
// ),
'loader' => '', // the script which should initialize all require.js environment, i.e. 'frontend/loader'
'require' => '', // a require.js script path for development mode, i.e. 'frontend/require'
'production' => '', // a production builds path, which contains a require.js builds with the almond stubs, i.e. 'frontend/builds'
'development' => '', // a development page path with the web pages entry points, i.e. 'frontend/pages'
'legacy' => array(), // a web page configutation, i.e.:
// array(
// 'use_build' => true, - use a script from builds dir
// 'page' => 'legacy_support', - use a script wich main name is 'welcome_index'
// 'theme' => 'default', - use a theme named 'default' (it's in '{pages,builds}/skins/legacy_support/{less,css}/default.{less,css}')
// ),
'pages' => array(), // a list of web pages configutation, it's hash, i.e.:
// array(
// 'Welcome' => array( - controller name (with directory subpath)
// 'index' => array( - action name
// 'use_build' => true, - use a script from builds dir
// 'page' => 'welcome_index', - use a script wich main name is 'welcome_index'
// 'theme' => 'default', - use a theme named 'default' (it's in '{pages,builds}/skins/welcome_index/{less,css}/default.{less,css}')
// ),
// ),
// ),
);
4 changes: 4 additions & 0 deletions views/layout.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
{% block stylesheets%}{% endblock stylesheets%}

{{ inject_theme_styles() }}
<!--[if lte IE 7]>
{{ inject_legacy_support_styles() }}
{{ inject_legacy_support_script() }}
<![endif]-->
</head>
<body>
{% block body%}{% endblock body%}
Expand Down

0 comments on commit 5bd12b8

Please sign in to comment.