From 628458026f158653ae5c8412dda0aaba0d94de9f Mon Sep 17 00:00:00 2001 From: Demian Katz <demian.katz@villanova.edu> Date: Fri, 3 Mar 2017 13:26:57 -0500 Subject: [PATCH] Optimization: don't build specs until we need to use them. --- .../View/Helper/Root/RecordDataFormatter.php | 24 +++++++++++++++---- .../Root/RecordDataFormatterFactory.php | 8 +++---- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/module/VuFind/src/VuFind/View/Helper/Root/RecordDataFormatter.php b/module/VuFind/src/VuFind/View/Helper/Root/RecordDataFormatter.php index e07720c8673..b186f4f2eb4 100644 --- a/module/VuFind/src/VuFind/View/Helper/Root/RecordDataFormatter.php +++ b/module/VuFind/src/VuFind/View/Helper/Root/RecordDataFormatter.php @@ -121,19 +121,35 @@ class RecordDataFormatter extends AbstractHelper */ public function getDefaults($key) { - return isset($this->defaults[$key]) ? $this->defaults[$key] : []; + // No value stored? Return empty array: + if (!isset($this->defaults[$key])) { + return []; + } + // Callback stored? Resolve to array on demand: + if (is_callable($this->defaults[$key])) { + $this->defaults[$key] = $this->defaults[$key](); + if (!is_array($this->defaults[$key])) { + throw new \Exception('Callback for ' . $key . ' must return array'); + } + } + // Send back array: + return $this->defaults[$key]; } /** * Set default configuration. * - * @param string $key Key for configuration to set. - * @param array $values Defaults to store. + * @param string $key Key for configuration to set. + * @param array|Callable $values Defaults to store (either an array, or a + * Callable returning an array). * * @return void */ - public function setDefaults($key, array $values) + public function setDefaults($key, $values) { + if (!is_array($values) && !is_callable($values)) { + throw new \Exception('$values must be array or Callable'); + } $this->defaults[$key] = $values; } diff --git a/module/VuFind/src/VuFind/View/Helper/Root/RecordDataFormatterFactory.php b/module/VuFind/src/VuFind/View/Helper/Root/RecordDataFormatterFactory.php index 478638a654b..e03388acb4d 100644 --- a/module/VuFind/src/VuFind/View/Helper/Root/RecordDataFormatterFactory.php +++ b/module/VuFind/src/VuFind/View/Helper/Root/RecordDataFormatterFactory.php @@ -49,12 +49,12 @@ class RecordDataFormatterFactory { $helper = new RecordDataFormatter(); $helper - ->setDefaults('collection-info', $this->getDefaultCollectionInfoSpecs()); + ->setDefaults('collection-info', [$this, 'getDefaultCollectionInfoSpecs']); $helper->setDefaults( - 'collection-record', $this->getDefaultCollectionRecordSpecs() + 'collection-record', [$this, 'getDefaultCollectionRecordSpecs'] ); - $helper->setDefaults('core', $this->getDefaultCoreSpecs()); - $helper->setDefaults('description', $this->getDefaultDescriptionSpecs()); + $helper->setDefaults('core', [$this, 'getDefaultCoreSpecs']); + $helper->setDefaults('description', [$this, 'getDefaultDescriptionSpecs']); return $helper; } -- GitLab