Merge branch 'feature/allow-module-developers-to-define-additional-static-files-9702'

resolves #9702
This commit is contained in:
Johannes Meyer 2015-08-06 15:17:46 +02:00
commit c289b2f1ef
6 changed files with 166 additions and 86 deletions

View File

@ -5,6 +5,9 @@ namespace Icinga\Application;
require_once dirname(__FILE__) . '/ApplicationBootstrap.php'; require_once dirname(__FILE__) . '/ApplicationBootstrap.php';
use Icinga\Web\Request;
use Icinga\Web\Response;
/** /**
* Use this if you want to make use of Icinga functionality in other web projects * Use this if you want to make use of Icinga functionality in other web projects
* *
@ -16,6 +19,40 @@ require_once dirname(__FILE__) . '/ApplicationBootstrap.php';
*/ */
class EmbeddedWeb extends ApplicationBootstrap class EmbeddedWeb extends ApplicationBootstrap
{ {
/**
* Request object
*
* @var Request
*/
protected $request;
/**
* Response
*
* @var Response
*/
protected $response;
/**
* Get the request
*
* @return Request
*/
public function getRequest()
{
return $this->request;
}
/**
* Get the response
*
* @return Response
*/
public function getResponse()
{
return $this->response;
}
/** /**
* Embedded bootstrap parts * Embedded bootstrap parts
* *
@ -26,10 +63,34 @@ class EmbeddedWeb extends ApplicationBootstrap
{ {
return $this return $this
->setupZendAutoloader() ->setupZendAutoloader()
->loadConfig()
->setupErrorHandling() ->setupErrorHandling()
->loadConfig()
->setupRequest()
->setupResponse()
->setupTimezone() ->setupTimezone()
->setupModuleManager() ->setupModuleManager()
->loadEnabledModules(); ->loadEnabledModules();
} }
/**
* Set the request
*
* @return $this
*/
protected function setupRequest()
{
$this->request = new Request();
return $this;
}
/**
* Set the response
*
* @return $this
*/
protected function setupResponse()
{
$this->response = new Response();
return $this;
}
} }

View File

@ -162,6 +162,20 @@ class Module
*/ */
private $app; private $app;
/**
* The CSS/LESS files this module provides
*
* @var array
*/
protected $cssFiles = array();
/**
* The Javascript files this module provides
*
* @var array
*/
protected $jsFiles = array();
/** /**
* Routes to add to the route chain * Routes to add to the route chain
* *
@ -388,6 +402,19 @@ class Module
return $manager->getModule($name); return $manager->getModule($name);
} }
/**
* Provide an additional CSS/LESS file
*
* @param string $path The path to the file, relative to self::$cssdir
*
* @return $this
*/
protected function provideCssFile($path)
{
$this->cssFiles[] = $this->cssdir . DIRECTORY_SEPARATOR . $path;
return $this;
}
/** /**
* Test if module provides css * Test if module provides css
* *
@ -395,7 +422,12 @@ class Module
*/ */
public function hasCss() public function hasCss()
{ {
return file_exists($this->getCssFilename()); if (file_exists($this->getCssFilename())) {
return true;
}
$this->launchConfigScript();
return !empty($this->cssFiles);
} }
/** /**
@ -408,6 +440,31 @@ class Module
return $this->cssdir . '/module.less'; return $this->cssdir . '/module.less';
} }
/**
* Return the CSS/LESS files this module provides
*
* @return array
*/
public function getCssFiles()
{
$files = $this->cssFiles;
$files[] = $this->getCssFilename();
return $files;
}
/**
* Provide an additional Javascript file
*
* @param string $path The path to the file, relative to self::$jsdir
*
* @return $this
*/
protected function provideJsFile($path)
{
$this->jsFiles[] = $this->jsdir . DIRECTORY_SEPARATOR . $path;
return $this;
}
/** /**
* Test if module provides js * Test if module provides js
* *
@ -415,7 +472,12 @@ class Module
*/ */
public function hasJs() public function hasJs()
{ {
return file_exists($this->getJsFilename()); if (file_exists($this->getJsFilename())) {
return true;
}
$this->launchConfigScript();
return !empty($this->jsFiles);
} }
/** /**
@ -428,6 +490,18 @@ class Module
return $this->jsdir . '/module.js'; return $this->jsdir . '/module.js';
} }
/**
* Return the Javascript files this module provides
*
* @return array
*/
public function getJsFiles()
{
$files = $this->jsFiles;
$files[] = $this->getJsFilename();
return $files;
}
/** /**
* Get the module name * Get the module name
* *

View File

@ -3,7 +3,7 @@
namespace Icinga\Application; namespace Icinga\Application;
require_once __DIR__ . '/ApplicationBootstrap.php'; require_once __DIR__ . '/EmbeddedWeb.php';
use Zend_Controller_Action_HelperBroker; use Zend_Controller_Action_HelperBroker;
use Zend_Controller_Front; use Zend_Controller_Front;
@ -11,14 +11,11 @@ use Zend_Controller_Router_Route;
use Zend_Layout; use Zend_Layout;
use Zend_Paginator; use Zend_Paginator;
use Zend_View_Helper_PaginationControl; use Zend_View_Helper_PaginationControl;
use Icinga\Application\Logger;
use Icinga\Authentication\Auth; use Icinga\Authentication\Auth;
use Icinga\User; use Icinga\User;
use Icinga\Util\TimezoneDetect; use Icinga\Util\TimezoneDetect;
use Icinga\Util\Translator; use Icinga\Util\Translator;
use Icinga\Web\Notification; use Icinga\Web\Notification;
use Icinga\Web\Request;
use Icinga\Web\Response;
use Icinga\Web\Session; use Icinga\Web\Session;
use Icinga\Web\Session\Session as BaseSession; use Icinga\Web\Session\Session as BaseSession;
use Icinga\Web\View; use Icinga\Web\View;
@ -28,11 +25,11 @@ use Icinga\Web\View;
* *
* Usage example: * Usage example:
* <code> * <code>
* use Icinga\Application\EmbeddedWeb; * use Icinga\Application\Web;
* EmbeddedWeb::start(); * Web::start();
* </code> * </code>
*/ */
class Web extends ApplicationBootstrap class Web extends EmbeddedWeb
{ {
/** /**
* View object * View object
@ -48,20 +45,6 @@ class Web extends ApplicationBootstrap
*/ */
private $frontController; private $frontController;
/**
* Request object
*
* @var Request
*/
private $request;
/**
* Response
*
* @var Response
*/
protected $response;
/** /**
* Session object * Session object
* *
@ -145,26 +128,6 @@ class Web extends ApplicationBootstrap
return $this->frontController; return $this->frontController;
} }
/**
* Get the request
*
* @return Request
*/
public function getRequest()
{
return $this->request;
}
/**
* Get the response
*
* @return Response
*/
public function getResponse()
{
return $this->response;
}
/** /**
* Getter for view * Getter for view
* *
@ -211,7 +174,7 @@ class Web extends ApplicationBootstrap
$auth = Auth::getInstance(); $auth = Auth::getInstance();
if ($auth->isAuthenticated()) { if ($auth->isAuthenticated()) {
$user = $auth->getUser(); $user = $auth->getUser();
$this->request->setUser($user); $this->getRequest()->setUser($user);
$this->user = $user; $this->user = $user;
} }
return $this; return $this;
@ -239,28 +202,6 @@ class Web extends ApplicationBootstrap
return $this; return $this;
} }
/**
* Set the request
*
* @return $this
*/
private function setupRequest()
{
$this->request = new Request();
return $this;
}
/**
* Set the response
*
* @return $this
*/
protected function setupResponse()
{
$this->response = new Response();
return $this;
}
/** /**
* Instantiate front controller * Instantiate front controller
* *
@ -269,7 +210,7 @@ class Web extends ApplicationBootstrap
private function setupFrontController() private function setupFrontController()
{ {
$this->frontController = Zend_Controller_Front::getInstance(); $this->frontController = Zend_Controller_Front::getInstance();
$this->frontController->setRequest($this->request); $this->frontController->setRequest($this->getRequest());
$this->frontController->setControllerDirectory($this->getApplicationDir('/controllers')); $this->frontController->setControllerDirectory($this->getApplicationDir('/controllers'));
$this->frontController->setParams( $this->frontController->setParams(
array( array(

View File

@ -42,17 +42,6 @@ class JavaScript
'js/vendor/jquery.tipsy' 'js/vendor/jquery.tipsy'
); );
public static function listModuleFiles()
{
$list = array();
foreach (Icinga::app()->getModuleManager()->getLoadedModules() as $name => $module) {
if ($module->hasJs()) {
$list[] = 'js/' . $name . '/module.js';
}
}
return $list;
}
public static function sendMinified() public static function sendMinified()
{ {
return self::send(true); return self::send(true);
@ -86,7 +75,11 @@ class JavaScript
foreach (Icinga::app()->getModuleManager()->getLoadedModules() as $name => $module) { foreach (Icinga::app()->getModuleManager()->getLoadedModules() as $name => $module) {
if ($module->hasJs()) { if ($module->hasJs()) {
$jsFiles[] = $module->getJsFilename(); foreach ($module->getJsFiles() as $path) {
if (file_exists($path)) {
$jsFiles[] = $path;
}
}
} }
} }
$files = array_merge($vendorFiles, $jsFiles); $files = array_merge($vendorFiles, $jsFiles);
@ -114,7 +107,7 @@ class JavaScript
} }
foreach ($jsFiles as $file) { foreach ($jsFiles as $file) {
$js .= file_get_contents($file); $js .= file_get_contents($file) . "\n\n\n";
} }
if ($minified) { if ($minified) {

View File

@ -93,14 +93,21 @@ class LessCompiler
public function addModule($name, $module) public function addModule($name, $module)
{ {
if ($module->hasCss()) { if ($module->hasCss()) {
$this->source .= "\n/* CSS: modules/$name/module.less */\n" $contents = array();
foreach ($module->getCssFiles() as $path) {
if (file_exists($path)) {
$contents[] = "/* CSS: modules/$name/$path */\n" . file_get_contents($path);
}
}
$this->source .= ''
. '.icinga-module.module-' . '.icinga-module.module-'
. $name . $name
. " {\n" . " {\n"
. file_get_contents($module->getCssFilename()) . join("\n\n", $contents)
. "}\n\n" . "}\n\n";
;
} }
return $this; return $this;
} }

View File

@ -76,7 +76,11 @@ class StyleSheet
$files = $lessFiles; $files = $lessFiles;
foreach ($app->getModuleManager()->getLoadedModules() as $name => $module) { foreach ($app->getModuleManager()->getLoadedModules() as $name => $module) {
if ($module->hasCss()) { if ($module->hasCss()) {
$files[] = $module->getCssFilename(); foreach ($module->getCssFiles() as $path) {
if (file_exists($path)) {
$files[] = $path;
}
}
} }
} }