Permalink
Browse files

feature(views): Allow mapping views dirs via views.php files

views.php files can now map entire directory trees into the view system with
a desired prefix for the view name.

Fixes #8546
Fixes #8550
  • Loading branch information...
mrclay committed Jun 22, 2015
1 parent e1018be commit 9ba31f20e7b278cc472749cb1d2cd1353c3bbec9
View
@@ -175,6 +175,28 @@ Elgg core uses this feature extensively. See ``engine/views.php``.
managing your plugin's assets, but we highly recommend using a package manager
of some kind because it makes upgrading so much easier.
+Specifying additional views directories
+---------------------------------------
+
+In your ``views.php`` file you can also specify directories to be scanned for views. Just provide
+a view name prefix ending with ``/`` and a directory path (like above).
+
+.. code-block:: php
+
+ <?php // mod/file/views.php
+ return [
+ 'default' => [
+ 'file/icon/' => __DIR__ . '/graphics/icons',
+ ],
+ ];
+
+With the above, files found within the ``icons`` folder will be interpreted as views. E.g. the view
+``file/icon/general.gif`` will be created and mapped to ``mod/file/graphics/icons/general.gif``.
+
+.. note::
+
+ This is a fully recursive scan. All files found will be brought into the views system.
+
Viewtypes
=========
@@ -93,7 +93,10 @@ public function __construct(\Elgg\PluginHooksService $hooks, \Elgg\Logger $logge
/**
* @access private
*/
- public function autoregisterViews($view_base, $folder, $base_location_path, $viewtype) {
+ public function autoregisterViews($view_base, $folder, $viewtype) {
+ $folder = rtrim($folder, '/\\');
+ $view_base = rtrim($view_base, '/\\');
+
$handle = opendir($folder);
if ($handle) {
while ($entry = readdir($handle)) {
@@ -110,8 +113,7 @@ public function autoregisterViews($view_base, $folder, $base_location_path, $vie
}
if (is_dir($path)) {
- $this->autoregisterViews($view_base_new . $entry, $path,
- $base_location_path, $viewtype);
+ $this->autoregisterViews($view_base_new . $entry, $path, $viewtype);
} else {
$view = $view_base_new . basename($entry, '.php');
$this->setViewLocation($view, $viewtype, $path);
@@ -448,7 +450,7 @@ public function registerPluginViews($path, &$failed_dir = '') {
$view_type_dir = $view_dir . $view_type;
if ('.' !== substr($view_type, 0, 1) && is_dir($view_type_dir)) {
- if ($this->autoregisterViews('', $view_type_dir, $view_dir, $view_type)) {
+ if ($this->autoregisterViews('', $view_type_dir, $view_type)) {
elgg_register_viewtype($view_type);
} else {
$failed_dir = $view_type_dir;
@@ -473,16 +475,17 @@ public function registerPluginViews($path, &$failed_dir = '') {
public function mergeViewsSpec(array $spec) {
foreach ($spec as $viewtype => $list) {
foreach ($list as $view => $path) {
- if (substr($view, -1) === '/') {
- // prefixes. not supported yet
+ if (preg_match('~^([/\\\\]|[a-zA-Z]\:)~', $path)) {
+ // absolute path
} else {
- if (preg_match('~^([/\\\\]|[a-zA-Z]\:)~', $path)) {
- // absolute path
- } else {
- // relative path
- $path = elgg_get_root_path() . $path;
- }
+ // relative path
+ $path = elgg_get_root_path() . $path;
+ }
+ if (substr($view, -1) === '/') {
+ // prefix
+ $this->autoregisterViews($view, $path, $viewtype);
+ } else {
$this->setViewLocation($view, $viewtype, $path);
}
}
View
@@ -1460,18 +1460,18 @@ function elgg_view_access_collections($owner_guid) {
* Plugin authors would only need to call this if optionally including
* an entire views structure.
*
- * @param string $view_base Optional The base of the view name without the view type.
- * @param string $folder Required The folder to begin looking in
- * @param string $base_location_path The base views directory to use with elgg_set_view_location()
- * @param string $viewtype The type of view we're looking at (default, rss, etc)
+ * @param string $view_base Optional The base of the view name without the view type.
+ * @param string $folder Required The folder to begin looking in
+ * @param string $ignored This argument is ignored
+ * @param string $viewtype The type of view we're looking at (default, rss, etc)
*
* @return bool returns false if folder can't be read
* @since 1.7.0
* @see elgg_set_view_location()
* @access private
*/
-function autoregister_views($view_base, $folder, $base_location_path, $viewtype) {
- return _elgg_services()->views->autoregisterViews($view_base, $folder, $base_location_path, $viewtype);
+function autoregister_views($view_base, $folder, $ignored, $viewtype) {
+ return _elgg_services()->views->autoregisterViews($view_base, $folder, $viewtype);
}
/**
@@ -21,7 +21,7 @@ public function setUp() {
$this->logger = $this->getMock('\Elgg\Logger', array(), array(), '', false);
$this->views = new \Elgg\ViewsService($this->hooks, $this->logger);
- $this->views->autoregisterViews('', "$this->viewsDir/default", "$this->viewsDir/", 'default');
+ $this->views->autoregisterViews('', "$this->viewsDir/default", 'default');
// supports deprecation wrapper for $vars['user']
_elgg_services()->setValue('session', \ElggSession::getMock());
View
@@ -30,8 +30,6 @@
"js/elgg/ui.river.js" => dirname(__DIR__) . "/js/lib/ui.river.js",
"lightbox.css" => dirname(__DIR__) . "/vendors/elgg-colorbox-theme/colorbox.css",
- "colorbox-images/border1.png" => dirname(__DIR__) . "/vendors/elgg-colorbox-theme/colorbox-images/border1.png",
- "colorbox-images/border2.png" => dirname(__DIR__) . "/vendors/elgg-colorbox-theme/colorbox-images/border2.png",
- "colorbox-images/loading.gif" => dirname(__DIR__) . "/vendors/elgg-colorbox-theme/colorbox-images/loading.gif",
+ "colorbox-images/" => dirname(__DIR__) . "/vendors/elgg-colorbox-theme/colorbox-images",
],
];
@@ -2,7 +2,6 @@
return [
'default' => [
- 'aalborg_theme/avatar_menu_arrows.png' => __DIR__ . '/graphics/avatar_menu_arrows.png',
- 'allborg_theme/homescreen.png' => __DIR__ . '/graphics/homescreen.png',
+ 'aalborg_theme/' => __DIR__ . '/graphics',
],
];
View
@@ -2,7 +2,6 @@
return [
'default' => [
- 'bookmarks/bookmark.gif' => __DIR__ . '/graphics/bookmark.gif',
- 'bookmarks/bookmarklet.gif' => __DIR__ . '/graphics/bookmarklet.gif',
+ 'bookmarks/' => __DIR__ . '/graphics',
],
];
View
@@ -1,16 +1,7 @@
<?php
-return call_user_func(function() {
- $dir = dir(__DIR__ . '/graphics/icons');
- $ret = [];
-
- while (false !== ($entry = $dir->read())) {
- if ($entry[0] === '.') {
- continue;
- }
-
- $ret['default']["file/icons/$entry"] = "{$dir->path}/$entry";
- }
-
- return $ret;
-});
+return [
+ 'default' => [
+ 'file/icons/' => __DIR__ . '/graphics/icons',
+ ],
+];
View
@@ -1,9 +1,7 @@
<?php
-return call_user_func(function() {
- foreach (['large', 'medium', 'small', 'tiny'] as $size) {
- $ret['default']["groups/default$size.gif"] = __DIR__ . "/graphics/default$size.gif";
- }
-
- return $ret;
-});
+return [
+ 'default' => [
+ 'groups/' => __DIR__ . '/graphics',
+ ],
+];
@@ -2,7 +2,6 @@
return [
'default' => [
- 'notifications/icon_notifications_email.gif' => __DIR__ . '/graphics/icon_notifications_email.gif',
- 'notifications/icon_notifications_site.gif' => __DIR__ . '/graphics/icon_notifications_site.gif',
+ 'notifications/' => __DIR__ . '/graphics',
],
];
View
@@ -1,7 +1,6 @@
<?php
return [
'default' => [
- 'pages/pages.gif' => __DIR__ . '/images/pages.gif',
- 'pages/pages_lrg.gif' => __DIR__ . '/images/pages_lrg.gif',
+ 'pages/' => __DIR__ . '/images',
],
];
@@ -2,6 +2,6 @@
return [
'default' => [
- 'reportedcontent/icon_reportthis.gif' => __DIR__ . '/graphics/icon_reportthis.gif',
+ 'reportedcontent/' => __DIR__ . '/graphics',
],
];
@@ -2,8 +2,6 @@
return [
'default' => [
- 'twitter_api/sign-in-with-twitter-d.png' => __DIR__ . '/graphics/sign-in-with-twitter-d.png',
- 'twitter_api/sign-in-with-twitter-l.png' => __DIR__ . '/graphics/sign-in-with-twitter-l.png',
- 'twitter_api/sign-in-with-twitter.gif' => __DIR__ . '/graphics/sign-in-with-twitter.gif',
+ 'twitter_api/' => __DIR__ . '/graphics',
],
];

0 comments on commit 9ba31f2

Please sign in to comment.