aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorGravatar Marien Fressinaud <dev@marienfressinaud.fr> 2015-01-08 21:33:13 +0100
committerGravatar Marien Fressinaud <dev@marienfressinaud.fr> 2015-01-08 21:33:13 +0100
commit250cd79251f5474915ad2230e786db70643b0ef3 (patch)
tree3b757a544dfb0239bd119b953c316d27860f70cf /lib
parent0e4e16ac55097aa173c7c439367294ebd7645562 (diff)
parentb23fc3187cb90800aad6417badf7822a8d280b74 (diff)
Merge branch '252-extensions' into dev
Diffstat (limited to 'lib')
-rw-r--r--lib/Minz/Configuration.php13
-rw-r--r--lib/Minz/Dispatcher.php53
-rw-r--r--lib/Minz/Extension.php198
-rw-r--r--lib/Minz/ExtensionException.php15
-rw-r--r--lib/Minz/ExtensionManager.php249
-rw-r--r--lib/Minz/Translate.php127
-rw-r--r--lib/Minz/View.php64
-rw-r--r--lib/lib_rss.php25
8 files changed, 698 insertions, 46 deletions
diff --git a/lib/Minz/Configuration.php b/lib/Minz/Configuration.php
index 6044fc269..fe415e22b 100644
--- a/lib/Minz/Configuration.php
+++ b/lib/Minz/Configuration.php
@@ -101,6 +101,19 @@ class Minz_Configuration {
*/
private $configuration_setter = null;
+ public function removeExtension($ext_name) {
+ self::$extensions_enabled = array_diff(
+ self::$extensions_enabled,
+ array($ext_name)
+ );
+ }
+ public function addExtension($ext_name) {
+ $found = array_search($ext_name, self::$extensions_enabled) !== false;
+ if (!$found) {
+ self::$extensions_enabled[] = $ext_name;
+ }
+ }
+
/**
* Create a new Minz_Configuration object.
*
diff --git a/lib/Minz/Dispatcher.php b/lib/Minz/Dispatcher.php
index f62a92911..125ce5757 100644
--- a/lib/Minz/Dispatcher.php
+++ b/lib/Minz/Dispatcher.php
@@ -15,6 +15,7 @@ class Minz_Dispatcher {
/* singleton */
private static $instance = null;
private static $needsReset;
+ private static $registrations = array();
private $controller;
@@ -38,7 +39,7 @@ class Minz_Dispatcher {
self::$needsReset = false;
try {
- $this->createController ('FreshRSS_' . Minz_Request::controllerName () . '_Controller');
+ $this->createController (Minz_Request::controllerName ());
$this->controller->init ();
$this->controller->firstAction ();
if (!self::$needsReset) {
@@ -67,14 +68,18 @@ class Minz_Dispatcher {
/**
* Instancie le Controller
- * @param $controller_name le nom du controller à instancier
+ * @param $base_name le nom du controller à instancier
* @exception ControllerNotExistException le controller n'existe pas
* @exception ControllerNotActionControllerException controller n'est
* > pas une instance de ActionController
*/
- private function createController ($controller_name) {
- $filename = APP_PATH . self::CONTROLLERS_PATH_NAME . '/'
- . $controller_name . '.php';
+ private function createController ($base_name) {
+ if (self::isRegistered($base_name)) {
+ self::loadController($base_name);
+ $controller_name = 'FreshExtension_' . $base_name . '_Controller';
+ } else {
+ $controller_name = 'FreshRSS_' . $base_name . '_Controller';
+ }
if (!class_exists ($controller_name)) {
throw new Minz_ControllerNotExistException (
@@ -114,4 +119,42 @@ class Minz_Dispatcher {
$action_name
));
}
+
+ /**
+ * Register a controller file.
+ *
+ * @param $base_name the base name of the controller (i.e. ./?c=<base_name>)
+ * @param $base_path the base path where we should look into to find info.
+ */
+ public static function registerController($base_name, $base_path) {
+ if (!self::isRegistered($base_name)) {
+ self::$registrations[$base_name] = $base_path;
+ }
+ }
+
+ /**
+ * Return if a controller is registered.
+ *
+ * @param $base_name the base name of the controller.
+ * @return true if the controller has been registered, false else.
+ */
+ public static function isRegistered($base_name) {
+ return isset(self::$registrations[$base_name]);
+ }
+
+ /**
+ * Load a controller file (include).
+ *
+ * @param $base_name the base name of the controller.
+ */
+ private static function loadController($base_name) {
+ $base_path = self::$registrations[$base_name];
+ $controller_filename = $base_path . '/controllers/' . $base_name . 'Controller.php';
+ include_once $controller_filename;
+ }
+
+ private static function setViewPath($controller, $base_name) {
+ $base_path = self::$registrations[$base_name];
+ $controller->view()->setBasePathname($base_path);
+ }
}
diff --git a/lib/Minz/Extension.php b/lib/Minz/Extension.php
new file mode 100644
index 000000000..b3991c129
--- /dev/null
+++ b/lib/Minz/Extension.php
@@ -0,0 +1,198 @@
+<?php
+
+/**
+ * The extension base class.
+ */
+class Minz_Extension {
+ private $name;
+ private $entrypoint;
+ private $path;
+ private $author;
+ private $description;
+ private $version;
+ private $type;
+
+ public static $authorized_types = array(
+ 'system',
+ 'user',
+ );
+
+ private $is_enabled;
+
+ /**
+ * The constructor to assign specific information to the extension.
+ *
+ * Available fields are:
+ * - name: the name of the extension (required).
+ * - entrypoint: the extension class name (required).
+ * - path: the pathname to the extension files (required).
+ * - author: the name and / or email address of the extension author.
+ * - description: a short description to describe the extension role.
+ * - version: a version for the current extension.
+ * - type: "system" or "user" (default).
+ *
+ * It must not be redefined by child classes.
+ *
+ * @param $meta_info contains information about the extension.
+ */
+ public function __construct($meta_info) {
+ $this->name = $meta_info['name'];
+ $this->entrypoint = $meta_info['entrypoint'];
+ $this->path = $meta_info['path'];
+ $this->author = isset($meta_info['author']) ? $meta_info['author'] : '';
+ $this->description = isset($meta_info['description']) ? $meta_info['description'] : '';
+ $this->version = isset($meta_info['version']) ? $meta_info['version'] : '0.1';
+ $this->setType(isset($meta_info['type']) ? $meta_info['type'] : 'user');
+
+ $this->is_enabled = false;
+ }
+
+ /**
+ * Used when installing an extension (e.g. update the database scheme).
+ *
+ * It must be redefined by child classes.
+ */
+ public function install() {}
+
+ /**
+ * Used when uninstalling an extension (e.g. revert the database scheme to
+ * cancel changes from install).
+ *
+ * It must be redefined by child classes.
+ */
+ public function uninstall() {}
+
+ /**
+ * Call at the initialization of the extension (i.e. when the extension is
+ * enabled by the extension manager).
+ *
+ * It must be redefined by child classes.
+ */
+ public function init() {}
+
+ /**
+ * Set the current extension to enable.
+ */
+ public function enable() {
+ $this->is_enabled = true;
+ }
+
+ /**
+ * Return if the extension is currently enabled.
+ *
+ * @return true if extension is enabled, false else.
+ */
+ public function is_enabled() {
+ return $this->is_enabled;
+ }
+
+ /**
+ * Return the content of the configure view for the current extension.
+ *
+ * @return the html content from ext_dir/configure.phtml, false if it does
+ * not exist.
+ */
+ public function getConfigureView() {
+ $filename = $this->path . '/configure.phtml';
+ if (!file_exists($filename)) {
+ return false;
+ }
+
+ ob_start();
+ include($filename);
+ return ob_get_clean();
+ }
+
+ /**
+ * Handle the configure action.
+ *
+ * It must be redefined by child classes.
+ */
+ public function handleConfigureAction() {}
+
+ /**
+ * Getters and setters.
+ */
+ public function getName() {
+ return $this->name;
+ }
+ public function getEntrypoint() {
+ return $this->entrypoint;
+ }
+ public function getPath() {
+ return $this->path;
+ }
+ public function getAuthor() {
+ return $this->author;
+ }
+ public function getDescription() {
+ return $this->description;
+ }
+ public function getVersion() {
+ return $this->version;
+ }
+ public function getType() {
+ return $this->type;
+ }
+ private function setType($type) {
+ if (!in_array($type, self::$authorized_types)) {
+ throw new Minz_ExtensionException('invalid `type` info', $this->name);
+ }
+ $this->type = $type;
+ }
+
+ /**
+ * Return the url for a given file.
+ *
+ * @param $filename name of the file to serve.
+ * @param $type the type (js or css) of the file to serve.
+ * @return the url corresponding to the file.
+ */
+ public function getFileUrl($filename, $type) {
+ $dir = end(explode('/', $this->path));
+ $file_name_url = urlencode($dir . '/static/' . $filename);
+
+ $absolute_path = $this->path . '/static/' . $filename;
+ $mtime = @filemtime($absolute_path);
+
+ $url = '/ext.php?f=' . $file_name_url .
+ '&amp;t=' . $type .
+ '&amp;' . $mtime;
+ return Minz_Url::display($url);
+ }
+
+ /**
+ * Register a controller in the Dispatcher.
+ *
+ * @param @base_name the base name of the controller. Final name will be:
+ * FreshExtension_<base_name>_Controller.
+ */
+ public function registerController($base_name) {
+ Minz_Dispatcher::registerController($base_name, $this->path);
+ }
+
+ /**
+ * Register the views in order to be accessible by the application.
+ */
+ public function registerViews() {
+ Minz_View::addBasePathname($this->path);
+ }
+
+ /**
+ * Register i18n files from ext_dir/i18n/
+ */
+ public function registerTranslates() {
+ $i18n_dir = $this->path . '/i18n';
+ Minz_Translate::registerPath($i18n_dir);
+ }
+
+ /**
+ * Register a new hook.
+ *
+ * @param $hook_name the hook name (must exist).
+ * @param $hook_function the function name to call (must be callable).
+ */
+ public function registerHook($hook_name, $hook_function) {
+ Minz_ExtensionManager::addHook($hook_name, $hook_function, $this);
+ }
+}
diff --git a/lib/Minz/ExtensionException.php b/lib/Minz/ExtensionException.php
new file mode 100644
index 000000000..647f1a9b9
--- /dev/null
+++ b/lib/Minz/ExtensionException.php
@@ -0,0 +1,15 @@
+<?php
+
+class Minz_ExtensionException extends Minz_Exception {
+ public function __construct ($message, $extension_name = false, $code = self::ERROR) {
+ if ($extension_name) {
+ $message = 'An error occured in `' . $extension_name
+ . '` extension with the message: ' . $message;
+ } else {
+ $message = 'An error occured in an unnamed '
+ . 'extension with the message: ' . $message;
+ }
+
+ parent::__construct($message, $code);
+ }
+}
diff --git a/lib/Minz/ExtensionManager.php b/lib/Minz/ExtensionManager.php
new file mode 100644
index 000000000..5880e80f6
--- /dev/null
+++ b/lib/Minz/ExtensionManager.php
@@ -0,0 +1,249 @@
+<?php
+
+/**
+ * An extension manager to load extensions present in EXTENSIONS_PATH.
+ *
+ * @todo see coding style for methods!!
+ */
+class Minz_ExtensionManager {
+ private static $ext_metaname = 'metadata.json';
+ private static $ext_entry_point = 'extension.php';
+ private static $ext_list = array();
+ private static $ext_list_enabled = array();
+
+ private static $ext_auto_enabled = array();
+
+ // List of available hooks. Please keep this list sorted.
+ private static $hook_list = array(
+ 'entry_before_display' => array(), // function($entry) -> Entry | null
+ 'entry_before_insert' => array(), // function($entry) -> Entry | null
+ 'feed_before_insert' => array(), // function($feed) -> Feed | null
+ );
+ private static $ext_to_hooks = array();
+
+ /**
+ * Initialize the extension manager by loading extensions in EXTENSIONS_PATH.
+ *
+ * A valid extension is a directory containing metadata.json and
+ * extension.php files.
+ * metadata.json is a JSON structure where the only required fields are
+ * `name` and `entry_point`.
+ * extension.php should contain at least a class named <name>Extension where
+ * <name> must match with the entry point in metadata.json. This class must
+ * inherit from Minz_Extension class.
+ */
+ public static function init() {
+ $list_potential_extensions = array_values(array_diff(
+ scandir(EXTENSIONS_PATH),
+ array('..', '.')
+ ));
+
+ $system_conf = Minz_Configuration::get('system');
+ self::$ext_auto_enabled = $system_conf->extensions_enabled;
+
+ foreach ($list_potential_extensions as $ext_dir) {
+ $ext_pathname = EXTENSIONS_PATH . '/' . $ext_dir;
+ $metadata_filename = $ext_pathname . '/' . self::$ext_metaname;
+
+ // Try to load metadata file.
+ if (!file_exists($metadata_filename)) {
+ // No metadata file? Invalid!
+ continue;
+ }
+ $meta_raw_content = file_get_contents($metadata_filename);
+ $meta_json = json_decode($meta_raw_content, true);
+ if (!$meta_json || !self::is_valid_metadata($meta_json)) {
+ // metadata.json is not a json file? Invalid!
+ // or metadata.json is invalid (no required information), invalid!
+ Minz_Log::warning('`' . $metadata_filename . '` is not a valid metadata file');
+ continue;
+ }
+
+ $meta_json['path'] = $ext_pathname;
+
+ // Try to load extension itself
+ $extension = self::load($meta_json);
+ if (!is_null($extension)) {
+ self::register($extension);
+ }
+ }
+ }
+
+ /**
+ * Indicates if the given parameter is a valid metadata array.
+ *
+ * Required fields are:
+ * - `name`: the name of the extension
+ * - `entry_point`: a class name to load the extension source code
+ * If the extension class name is `TestExtension`, entry point will be `Test`.
+ * `entry_point` must be composed of alphanumeric characters.
+ *
+ * @param $meta is an array of values.
+ * @return true if the array is valid, false else.
+ */
+ public static function is_valid_metadata($meta) {
+ return !(empty($meta['name']) ||
+ empty($meta['entrypoint']) ||
+ !ctype_alnum($meta['entrypoint']));
+ }
+
+ /**
+ * Load the extension source code based on info metadata.
+ *
+ * @param $info an array containing information about extension.
+ * @return an extension inheriting from Minz_Extension.
+ */
+ public static function load($info) {
+ $entry_point_filename = $info['path'] . '/' . self::$ext_entry_point;
+ $ext_class_name = $info['entrypoint'] . 'Extension';
+
+ include($entry_point_filename);
+
+ // Test if the given extension class exists.
+ if (!class_exists($ext_class_name)) {
+ Minz_Log::warning('`' . $ext_class_name .
+ '` cannot be found in `' . $entry_point_filename . '`');
+ return null;
+ }
+
+ // Try to load the class.
+ $extension = null;
+ try {
+ $extension = new $ext_class_name($info);
+ } catch (Minz_ExtensionException $e) {
+ // We cannot load the extension? Invalid!
+ Minz_Log::warning('In `' . $metadata_filename . '`: ' . $e->getMessage());
+ return null;
+ }
+
+ // Test if class is correct.
+ if (!($extension instanceof Minz_Extension)) {
+ Minz_Log::warning('`' . $ext_class_name .
+ '` is not an instance of `Minz_Extension`');
+ return null;
+ }
+
+ return $extension;
+ }
+
+ /**
+ * Add the extension to the list of the known extensions ($ext_list).
+ *
+ * If the extension is present in $ext_auto_enabled and if its type is "system",
+ * it will be enabled in the same time.
+ *
+ * @param $ext a valid extension.
+ */
+ public static function register($ext) {
+ $name = $ext->getName();
+ self::$ext_list[$name] = $ext;
+
+ if ($ext->getType() === 'system' &&
+ in_array($name, self::$ext_auto_enabled)) {
+ self::enable($ext->getName());
+ }
+
+ self::$ext_to_hooks[$name] = array();
+ }
+
+ /**
+ * Enable an extension so it will be called when necessary.
+ *
+ * The extension init() method will be called.
+ *
+ * @param $ext_name is the name of a valid extension present in $ext_list.
+ */
+ public static function enable($ext_name) {
+ if (isset(self::$ext_list[$ext_name])) {
+ $ext = self::$ext_list[$ext_name];
+ self::$ext_list_enabled[$ext_name] = $ext;
+ $ext->enable();
+ $ext->init();
+ }
+ }
+
+ /**
+ * Enable a list of extensions.
+ *
+ * @param $ext_list the names of extensions we want to load.
+ */
+ public static function enable_by_list($ext_list) {
+ foreach ($ext_list as $ext_name) {
+ self::enable($ext_name);
+ }
+ }
+
+ /**
+ * Return a list of extensions.
+ *
+ * @param $only_enabled if true returns only the enabled extensions (false by default).
+ * @return an array of extensions.
+ */
+ public static function list_extensions($only_enabled = false) {
+ if ($only_enabled) {
+ return self::$ext_list_enabled;
+ } else {
+ return self::$ext_list;
+ }
+ }
+
+ /**
+ * Return an extension by its name.
+ *
+ * @param $ext_name the name of the extension.
+ * @return the corresponding extension or null if it doesn't exist.
+ */
+ public static function find_extension($ext_name) {
+ if (!isset(self::$ext_list[$ext_name])) {
+ return null;
+ }
+
+ return self::$ext_list[$ext_name];
+ }
+
+ /**
+ * Add a hook function to a given hook.
+ *
+ * The hook name must be a valid one. For the valid list, see self::$hook_list
+ * array keys.
+ *
+ * @param $hook_name the hook name (must exist).
+ * @param $hook_function the function name to call (must be callable).
+ * @param $ext the extension which register the hook.
+ */
+ public static function addHook($hook_name, $hook_function, $ext) {
+ if (isset(self::$hook_list[$hook_name]) && is_callable($hook_function)) {
+ self::$hook_list[$hook_name][] = $hook_function;
+ self::$ext_to_hooks[$ext->getName()][] = $hook_name;
+ }
+ }
+
+ /**
+ * Call functions related to a given hook.
+ *
+ * The hook name must be a valid one. For the valid list, see self::$hook_list
+ * array keys.
+ *
+ * @param $hook_name the hook to call.
+ * @param additionnal parameters (for signature, please see self::$hook_list comments)
+ * @todo hook functions will have different signatures. So the $res = func($args);
+ * $args = $res; will not work for all of them in the future. We must
+ * find a better way to call hooks.
+ */
+ public static function callHook($hook_name) {
+ $args = func_get_args();
+ unset($args[0]);
+
+ $result = $args[1];
+ foreach (self::$hook_list[$hook_name] as $function) {
+ $result = call_user_func_array($function, $args);
+
+ if (is_null($result)) {
+ break;
+ }
+
+ $args = $result;
+ }
+ return $result;
+ }
+}
diff --git a/lib/Minz/Translate.php b/lib/Minz/Translate.php
index 1b4102ca9..baddcb424 100644
--- a/lib/Minz/Translate.php
+++ b/lib/Minz/Translate.php
@@ -10,9 +10,9 @@
*/
class Minz_Translate {
/**
- * $lang_list is the list of available languages.
+ * $path_list is the list of registered base path to search translations.
*/
- private static $lang_list = array();
+ private static $path_list = array();
/**
* $lang_name is the name of the current language to use.
@@ -20,9 +20,9 @@ class Minz_Translate {
private static $lang_name;
/**
- * $lang_path is the pathname of i18n files (e.g. ./app/i18n/en/).
+ * $lang_files is a list of registered i18n files.
*/
- private static $lang_path;
+ private static $lang_files = array();
/**
* $translates is a cache for i18n translation.
@@ -31,13 +31,16 @@ class Minz_Translate {
/**
* Init the translation object.
- * @param $lang_list the list of available languages.
* @param $lang_name the lang to show.
*/
- public static function init($lang_list, $lang_name) {
- self::$lang_list = $lang_list;
+ public static function init($lang_name = null) {
self::$lang_name = $lang_name;
- self::$lang_path = APP_PATH . '/i18n/' . self::$lang_name . '/';
+ self::$lang_files = array();
+ self::$translates = array();
+ self::registerPath(APP_PATH . '/i18n');
+ foreach (self::$path_list as $path) {
+ self::loadLang($path);
+ }
}
/**
@@ -45,15 +48,103 @@ class Minz_Translate {
* @param $lang_name the new language to use
*/
public static function reset($lang_name) {
- self::init(self::$lang_list, $lang_name);
+ self::$lang_name = $lang_name;
+ self::$lang_files = array();
+ self::$translates = array();
+ foreach (self::$path_list as $path) {
+ self::loadLang($path);
+ }
}
/**
* Return the list of available languages.
- * @return an array.
+ * @return an array containing langs found in different registered paths.
*/
public static function availableLanguages() {
- return self::$lang_list;
+ $list_langs = array();
+
+ foreach (self::$path_list as $path) {
+ $path_langs = array_values(array_diff(
+ scandir($path),
+ array('..', '.')
+ ));
+
+ $list_langs = array_merge($list_langs, $path_langs);
+ }
+
+ return array_unique($list_langs);
+ }
+
+ /**
+ * Register a new path.
+ * @param $path a path containing i18n directories (e.g. ./en/, ./fr/).
+ */
+ public static function registerPath($path) {
+ if (in_array($path, self::$path_list)) {
+ return;
+ }
+
+ self::$path_list[] = $path;
+ self::loadLang($path);
+ }
+
+ /**
+ * Load translations of the current language from the given path.
+ * @param $path the path containing i18n directories.
+ */
+ private static function loadLang($path) {
+ $lang_path = $path . '/' . self::$lang_name;
+ if (!file_exists($lang_path) || is_null(self::$lang_name)) {
+ // The lang path does not exist, nothing more to do.
+ return;
+ }
+
+ $list_i18n_files = array_values(array_diff(
+ scandir($lang_path),
+ array('..', '.')
+ ));
+
+ // Each file basename correspond to a top-level i18n key. For each of
+ // these keys we store the file pathname and mark translations must be
+ // reloaded (by setting $translates[$i18n_key] to null).
+ foreach ($list_i18n_files as $i18n_filename) {
+ $i18n_key = basename($i18n_filename, '.php');
+ if (!isset(self::$lang_files[$i18n_key])) {
+ self::$lang_files[$i18n_key] = array();
+ }
+ self::$lang_files[$i18n_key][] = $lang_path . '/' . $i18n_filename;
+ self::$translates[$i18n_key] = null;
+ }
+ }
+
+ /**
+ * Load the files associated to $key into $translates.
+ * @param $key the top level i18n key we want to load.
+ */
+ private static function loadKey($key) {
+ // The top level key is not in $lang_files, it means it does not exist!
+ if (!isset(self::$lang_files[$key])) {
+ Minz_Log::debug($key . ' is not a valid top level key');
+ return false;
+ }
+
+ self::$translates[$key] = array();
+
+ foreach (self::$lang_files[$key] as $lang_pathname) {
+ $i18n_array = include($lang_pathname);
+ if (!is_array($i18n_array)) {
+ Minz_Log::warning('`' . $lang_pathname . '` does not contain a PHP array');
+ continue;
+ }
+
+ // We must avoid to erase previous data so we just override them if
+ // needed.
+ self::$translates[$key] = array_replace_recursive(
+ self::$translates[$key], $i18n_array
+ );
+ }
+
+ return true;
}
/**
@@ -73,16 +164,14 @@ class Minz_Translate {
$top_level = array_shift($group);
}
- $filename = self::$lang_path . $top_level . '.php';
-
- // Try to load the i18n file if it's not done yet.
- if (!isset(self::$translates[$top_level])) {
- if (!file_exists($filename)) {
- Minz_Log::debug($top_level . ' is not a valid top level key');
+ // If $translates[$top_level] is null it means we have to load the
+ // corresponding files.
+ if (!isset(self::$translates[$top_level]) ||
+ is_null(self::$translates[$top_level])) {
+ $res = self::loadKey($top_level);
+ if (!$res) {
return $key;
}
-
- self::$translates[$top_level] = include($filename);
}
// Go through the i18n keys to get the correct translation value.
diff --git a/lib/Minz/View.php b/lib/Minz/View.php
index 481b0376d..ff5cce4a5 100644
--- a/lib/Minz/View.php
+++ b/lib/Minz/View.php
@@ -13,8 +13,9 @@ class Minz_View {
const LAYOUT_FILENAME = '/layout.phtml';
private $view_filename = '';
- private $use_layout = null;
+ private $use_layout = true;
+ private static $base_pathnames = array(APP_PATH);
private static $title = '';
private static $styles = array ();
private static $scripts = array ();
@@ -37,19 +38,26 @@ class Minz_View {
* Change le fichier de vue en fonction d'un controller / action
*/
public function change_view($controller_name, $action_name) {
- $this->view_filename = APP_PATH
- . self::VIEWS_PATH_NAME . '/'
+ $this->view_filename = self::VIEWS_PATH_NAME . '/'
. $controller_name . '/'
. $action_name . '.phtml';
}
/**
+ * Add a base pathname to search views.
+ *
+ * New pathnames will be added at the beginning of the list.
+ *
+ * @param $base_pathname the new base pathname.
+ */
+ public static function addBasePathname($base_pathname) {
+ array_unshift(self::$base_pathnames, $base_pathname);
+ }
+
+ /**
* Construit la vue
*/
public function build () {
- if ($this->use_layout === null) { //TODO: avoid file_exists and require views to be explicit
- $this->use_layout = file_exists (APP_PATH . self::LAYOUT_PATH_NAME . self::LAYOUT_FILENAME);
- }
if ($this->use_layout) {
$this->buildLayout ();
} else {
@@ -58,21 +66,39 @@ class Minz_View {
}
/**
+ * Include a view file.
+ *
+ * The file is searched inside list of $base_pathnames.
+ *
+ * @param $filename the name of the file to include.
+ * @return true if the file has been included, false else.
+ */
+ private function includeFile($filename) {
+ // We search the filename in the list of base pathnames. Only the first view
+ // found is considered.
+ foreach (self::$base_pathnames as $base) {
+ $absolute_filename = $base . $filename;
+ if (file_exists($absolute_filename)) {
+ include $absolute_filename;
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ /**
* Construit le layout
*/
public function buildLayout () {
- include (
- APP_PATH
- . self::LAYOUT_PATH_NAME
- . self::LAYOUT_FILENAME
- );
+ $this->includeFile(self::LAYOUT_PATH_NAME . self::LAYOUT_FILENAME);
}
/**
* Affiche la Vue en elle-même
*/
public function render () {
- if ((include($this->view_filename)) === false) {
+ if (!$this->includeFile($this->view_filename)) {
Minz_Log::notice('File not found: `' . $this->view_filename . '`');
}
}
@@ -82,11 +108,8 @@ class Minz_View {
* @param $part l'élément partial à ajouter
*/
public function partial ($part) {
- $fic_partial = APP_PATH
- . self::LAYOUT_PATH_NAME . '/'
- . $part . '.phtml';
-
- if ((include($fic_partial)) === false) {
+ $fic_partial = self::LAYOUT_PATH_NAME . '/' . $part . '.phtml';
+ if (!$this->includeFile($fic_partial)) {
Minz_Log::warning('File not found: `' . $fic_partial . '`');
}
}
@@ -96,11 +119,8 @@ class Minz_View {
* @param $helper l'élément à afficher
*/
public function renderHelper ($helper) {
- $fic_helper = APP_PATH
- . '/views/helpers/'
- . $helper . '.phtml';
-
- if ((include($fic_helper)) === false) {;
+ $fic_helper = '/views/helpers/' . $helper . '.phtml';
+ if (!$this->includeFile($fic_helper)) {
Minz_Log::warning('File not found: `' . $fic_helper . '`');
}
}
diff --git a/lib/lib_rss.php b/lib/lib_rss.php
index ffd56eae4..083e87745 100644
--- a/lib/lib_rss.php
+++ b/lib/lib_rss.php
@@ -375,6 +375,7 @@ function recursive_unlink($dir) {
if (!is_dir($dir)) {
return true;
}
+
$files = array_diff(scandir($dir), array('.', '..'));
foreach ($files as $filename) {
$filename = $dir . '/' . $filename;
@@ -385,6 +386,7 @@ function recursive_unlink($dir) {
unlink($filename);
}
}
+
return rmdir($dir);
}
@@ -404,3 +406,26 @@ function remove_query_by_get($get, $queries) {
}
return $final_queries;
}
+
+
+/**
+ * Add a value in an array and take care it is unique.
+ * @param $array the array in which we add the value.
+ * @param $value the value to add.
+ */
+function array_push_unique(&$array, $value) {
+ $found = array_search($value, $array) !== false;
+ if (!$found) {
+ $array[] = $value;
+ }
+}
+
+
+/**
+ * Remove a value from an array.
+ * @param $array the array from wich value is removed.
+ * @param $value the value to remove.
+ */
+function array_remove(&$array, $value) {
+ $array = array_diff($array, array($value));
+}