Skip to content

Commit

Permalink
This object is not used enough to warrant storage in the session (esp…
Browse files Browse the repository at this point in the history
…ecially since it can be very large)

Better to just load on-demand (block list is not even used on main
portal page)
  • Loading branch information
slusarz committed Mar 25, 2014
1 parent bfc0521 commit 8f4d546
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 19 deletions.
63 changes: 51 additions & 12 deletions framework/Core/lib/Horde/Core/Block/Collection.php
Expand Up @@ -16,13 +16,20 @@
*/
class Horde_Core_Block_Collection implements Serializable
{
/**
* List of apps to load blocks from.
*
* @var array
*/
protected $_apps;

/**
* A hash storing the information about all available blocks from
* all applications.
*
* @var array
*/
protected $_blocks = array();
protected $_blocks;

/**
* Layout configuration preference name.
Expand All @@ -39,16 +46,7 @@ class Horde_Core_Block_Collection implements Serializable
*/
public function __construct(array $apps, $layout)
{
foreach ($apps as $app) {
$drivers = $GLOBALS['registry']->getAppDrivers($app, 'Block');
foreach ($drivers as $val) {
$tmp = new $val($app);
if ($tmp->enabled) {
$this->_blocks[$app][$val]['name'] = $tmp->getName();
}
}
}

$this->_apps = $apps;
$this->_layout = $layout;
}

Expand Down Expand Up @@ -158,6 +156,8 @@ public function getBlocksList()
{
$blocks = array();

$this->_loadBlocks();

/* Get available blocks from all apps. */
foreach ($this->_blocks as $app => $app_blocks) {
$app_name = $GLOBALS['registry']->get('name', $app);
Expand Down Expand Up @@ -235,6 +235,7 @@ public function getBlocksWidget($cur_app = null, $cur_block = null,
*/
public function getOptionType($app, $block, $param_id)
{
/* getParams() loads $_blocks */
$this->getParams($app, $block);
return $this->_blocks[$app][$block]['params'][$param_id]['type2'];
}
Expand All @@ -250,6 +251,7 @@ public function getOptionType($app, $block, $param_id)
*/
public function getOptionRequired($app, $block, $param_id)
{
/* getParams() loads $_blocks */
$this->getParams($app, $block);
return isset($this->_blocks[$app][$block]['params'][$param_id]['required'])
? $this->_blocks[$app][$block]['params'][$param_id]['required']
Expand All @@ -267,6 +269,7 @@ public function getOptionRequired($app, $block, $param_id)
*/
public function getOptionValues($app, $block, $param_id)
{
/* getParams() loads $_blocks */
$this->getParams($app, $block);
return $this->_blocks[$app][$block]['params'][$param_id]['values'];
}
Expand All @@ -285,6 +288,7 @@ public function getOptionsWidget($app, $block, $param_id, $val = null)
{
$widget = '';

/* getParams() loads $_blocks */
$this->getParams($app, $block);
$param = $this->_blocks[$app][$block]['params'][$param_id];
if (!isset($param['default'])) {
Expand Down Expand Up @@ -387,6 +391,8 @@ public function getOptionsWidget($app, $block, $param_id, $val = null)
*/
public function getName($app, $block)
{
$this->_loadBlocks();

return isset($this->_blocks[$app][$block])
? $this->_blocks[$app][$block]['name']
: sprintf(Horde_Core_Translation::t("Block \"%s\" of application \"%s\" not found."), $block, $app);
Expand All @@ -402,6 +408,8 @@ public function getName($app, $block)
*/
public function getParams($app, $block)
{
$this->_loadBlocks();

if (!isset($this->_blocks[$app][$block])) {
return array();
}
Expand Down Expand Up @@ -430,6 +438,7 @@ public function getParams($app, $block)
*/
public function getParamName($app, $block, $param)
{
/* getParams() loads $_blocks */
$this->getParams($app, $block);
return $this->_blocks[$app][$block]['params'][$param]['name'];
}
Expand All @@ -445,6 +454,7 @@ public function getParamName($app, $block, $param)
*/
public function getDefaultValue($app, $block, $param)
{
/* getParams() loads $_blocks */
$this->getParams($app, $block);
return isset($this->_blocks[$app][$block]['params'][$param]['default'])
? $this->_blocks[$app][$block]['params'][$param]['default']
Expand All @@ -465,19 +475,48 @@ public function isEditable($app, $block)
return $block->updateable || $block->getParams();
}

/**
* Loads the blocks from all applications.
*/
public function _loadBlocks()
{
global $registry;

if (isset($this->_blocks)) {
return;
}

$this->_blocks = array();

foreach ($this->_apps as $app) {
$drivers = $registry->getAppDrivers($app, 'Block');
foreach ($drivers as $val) {
$tmp = new $val($app);
if ($tmp->enabled) {
$this->_blocks[$app][$val]['name'] = $tmp->getName();
}
}
}
}

/* Serializable methods. */

public function serialize()
{
return json_encode(array(
$this->_apps,
$this->_blocks,
$this->_layout
));
}

public function unserialize($data)
{
list($this->_blocks, $this->_layout) = json_decode($data, true);
list(
$this->_apps,
$this->_blocks,
$this->_layout
) = json_decode($data, true);
}

}
10 changes: 3 additions & 7 deletions framework/Core/lib/Horde/Core/Factory/BlockCollection.php
Expand Up @@ -48,7 +48,7 @@ class Horde_Core_Factory_BlockCollection extends Horde_Core_Factory_Base
*/
public function create(array $apps = array(), $layout = 'portal_layout')
{
global $registry, $session;
global $registry;

$apps = empty($apps)
? $registry->listApps()
Expand All @@ -60,12 +60,8 @@ public function create(array $apps = array(), $layout = 'portal_layout')
);

if (!isset($this->_instances[$sig])) {
if (!($ob = $session->retrieve('horde', 'blocks/' . $sig))) {
$ob = new Horde_Core_Block_Collection($apps, $layout);
$session->set('horde', 'blocks/' . $sig, $ob);
}

$this->_instances[$sig] = $ob;
$this->_instances[$sig] =
new Horde_Core_Block_Collection($apps, $layout);
}

return $this->_instances[$sig];
Expand Down

0 comments on commit 8f4d546

Please sign in to comment.