[icinga-checkins] icinga.org: icingaweb2/feature/announce-banner-11198: Implement AnnounceHook

git at icinga.org git at icinga.org
Wed Nov 2 17:52:34 CET 2016


Module: icingaweb2
Branch: feature/announce-banner-11198
Commit: 2c80167224f96f7b6a9f550386db08b645590b48
URL:    https://git.icinga.org/?p=icingaweb2.git;a=commit;h=2c80167224f96f7b6a9f550386db08b645590b48

Author: Alexander A. Klimov <alexander.klimov at icinga.com>
Date:   Wed Nov  2 16:50:51 2016 +0100

Implement AnnounceHook

refs #11198

---

 library/Icinga/Application/Hook/AnnounceHook.php |   79 ++++++++++++++++++++++
 1 file changed, 79 insertions(+)

diff --git a/library/Icinga/Application/Hook/AnnounceHook.php b/library/Icinga/Application/Hook/AnnounceHook.php
new file mode 100644
index 0000000..16eadea
--- /dev/null
+++ b/library/Icinga/Application/Hook/AnnounceHook.php
@@ -0,0 +1,79 @@
+<?php
+/* Icinga Web 2 | (c) 2016 Icinga Development Team | GPLv2+ */
+
+namespace Icinga\Application\Hook;
+
+use Icinga\Exception\ProgrammingError;
+use Icinga\Web\Announce;
+
+/**
+ * Base class for Announce hooks
+ *
+ * Extend this class if you want your module to provide Announces.
+ */
+abstract class AnnounceHook
+{
+    /**
+     * Cache for {@link getAnnounces()}
+     *
+     * @var Announce[]
+     */
+    private $announces = null;
+
+    /**
+     * AnnounceHook constructor
+     *
+     * @see {@link init()} for hook initialization.
+     */
+    final public function __construct()
+    {
+        $this->init();
+    }
+
+    /**
+     * Overwrite this function for hook initialization, e.g. loading the hook's config
+     */
+    protected function init()
+    {
+    }
+
+    /**
+     * Get all new Announces
+     *
+     * @return Announce[]
+     */
+    abstract protected function loadAnnounces();
+
+    /**
+     * Call {@link loadAnnounces()} and validate, cache and return its return value
+     *
+     * @return  Announce[]
+     *
+     * @throws  ProgrammingError    If {@link loadAnnounces()} returns something invalid
+     */
+    final public function getAnnounces()
+    {
+        if ($this->announces === null) {
+            $announces = $this->loadAnnounces();
+            if (! is_array($announces)) {
+                throw new ProgrammingError(
+                    '%s::loadAnnounces() returned a %s, expected Icinga\Web\Announce[]',
+                    get_class($this),
+                    is_object($announces) ? get_class($announces) : gettype($announces)
+                );
+            }
+            foreach ($announces as $announce) {
+                if (! ($announce instanceof Announce)) {
+                    throw new ProgrammingError(
+                        '%s::loadAnnounces() returned an array with a %s, expected only Icinga\Web\Announce',
+                        get_class($this),
+                        is_object($announce) ? get_class($announce) : gettype($announce)
+                    );
+                }
+            }
+            $this->announces = $announces;
+        }
+
+        return $this->announces;
+    }
+}



More information about the icinga-checkins mailing list