From 34ef3f26af03e5b4b69132a7ac0a9febb9af82ce Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Sun, 27 Nov 2016 23:54:03 +0100 Subject: [PATCH] Html: introduce a new namespace --- .../{Web/Component => Html}/Attribute.php | 40 ++++- .../{Web/Component => Html}/Attributes.php | 49 ++++-- library/Businessprocess/Html/Container.php | 67 ++++++++ .../{Web/Component => Html}/Link.php | 54 +++---- .../Web/Component/ActionBar.php | 6 +- .../Web/Component/Component.php | 139 ----------------- .../Web/Component/Container.php | 144 ------------------ 7 files changed, 166 insertions(+), 333 deletions(-) rename library/Businessprocess/{Web/Component => Html}/Attribute.php (63%) rename library/Businessprocess/{Web/Component => Html}/Attributes.php (75%) create mode 100644 library/Businessprocess/Html/Container.php rename library/Businessprocess/{Web/Component => Html}/Link.php (51%) delete mode 100644 library/Businessprocess/Web/Component/Component.php delete mode 100644 library/Businessprocess/Web/Component/Container.php diff --git a/library/Businessprocess/Web/Component/Attribute.php b/library/Businessprocess/Html/Attribute.php similarity index 63% rename from library/Businessprocess/Web/Component/Attribute.php rename to library/Businessprocess/Html/Attribute.php index dfabd00..d0ee15e 100644 --- a/library/Businessprocess/Web/Component/Attribute.php +++ b/library/Businessprocess/Html/Attribute.php @@ -1,8 +1,8 @@ value; } + /** + * @param string|array $value + * @return $this + */ + public function setValue($value) + { + $this->value = $value; + return $this; + } + + /** + * @param string $value + * @return $this + */ + public function addValue($value) + { + if (! is_array($this->value)) { + $this->value = array($this->value); + } + + if (is_array($value)) { + $this->value = array_merge($this->value, $value); + } else { + $this->value[] = $value; + } + return $this; + } + /** * @return string */ @@ -92,7 +120,11 @@ class Attribute extends Component */ public static function escapeValue($value) { - // TODO: escape - return (string) $value; + // TODO: escape differently + if (is_array($value)) { + return Util::escapeForHtml(implode(' ', $value)); + } else { + return Util::escapeForHtml((string) $value); + } } } diff --git a/library/Businessprocess/Web/Component/Attributes.php b/library/Businessprocess/Html/Attributes.php similarity index 75% rename from library/Businessprocess/Web/Component/Attributes.php rename to library/Businessprocess/Html/Attributes.php index c0c8039..ef130cb 100644 --- a/library/Businessprocess/Web/Component/Attributes.php +++ b/library/Businessprocess/Html/Attributes.php @@ -1,15 +1,17 @@ attributes = array(); if (empty($attributes)) { return; } @@ -44,7 +45,8 @@ class Attributes extends Component /** * @param Attributes|array|null $attributes - * @return static + * @return Attributes + * @throws IcingaException */ public static function wantAttributes($attributes) { @@ -144,15 +146,42 @@ class Attributes extends Component return $this; } + /** + * Callback must return an instance of Attribute + * + * @param $name + * @param $callback + */ + public function registerCallbackFor($name, callable $callback) + { + $this->callbacks[$name] = $callback; + return $this; + } + /** * @inheritdoc */ public function render() { - if (empty($this->attributes)) { + if (empty($this->attributes) && empty($this->callbacks)) { return ''; } - return ' ' . implode(' ', $this->attributes); + $parts = array(); + foreach ($this->callbacks as $callback) { + $attribute = call_user_func($callback); + if (! $attribute instanceof Attribute) { + throw new ProgrammingError( + 'A registered attribute callback must return an Attribute' + ); + } + + $parts[] = $attribute->render(); + } + + foreach ($this->attributes as $attribute) { + $parts[] = $attribute->render(); + } + return ' ' . implode(' ', $parts); } } diff --git a/library/Businessprocess/Html/Container.php b/library/Businessprocess/Html/Container.php new file mode 100644 index 0000000..14e8fc7 --- /dev/null +++ b/library/Businessprocess/Html/Container.php @@ -0,0 +1,67 @@ +renderContainerFor(parent::render()); + } + + /** + * @inheritdoc + */ + public function renderError($error) + { + // TODO: eventually add class="error" + return $this->renderContainerFor( + parent::renderError($error) + ); + } + + /** + * @param bool $render + * @return $this + */ + public function renderIfEmpty($render = true) + { + $this->renderIfEmpty = $render; + return $this; + } + + /** + * @param string $content + * @return string + */ + protected function renderContainerFor($content) + { + return sprintf( + '<%s%s>%s', + $this->tag, + $this->attributes->render(), + $content, + $this->tag + ); + } +} diff --git a/library/Businessprocess/Web/Component/Link.php b/library/Businessprocess/Html/Link.php similarity index 51% rename from library/Businessprocess/Web/Component/Link.php rename to library/Businessprocess/Html/Link.php index 149c7af..b82e0a2 100644 --- a/library/Businessprocess/Web/Component/Link.php +++ b/library/Businessprocess/Html/Link.php @@ -1,37 +1,39 @@ text = $text; + $link->content = Util::wantHtml($content); + $link->attributes()->registerCallbackFor('href', array($link, 'getHrefAttribute')); + $link->setAttributes($attributes); + return $link; + } + + public function setUrl($url, $urlParams) + { if ($url instanceof WebUrl) { // Hint: Url is also a WebUrl if ($urlParams !== null) { $url->addParams($urlParams); @@ -45,17 +47,15 @@ class Link extends Component $link->url = Url::fromPath($url, $urlParams); } } - $link->attributes = new Attributes($attributes); - - return $link; + $link->url->getParams(); } /** - * @return string + * @return Attribute */ - public function getRenderedText() + public function getHrefAttribute() { - return $this->view()->escape((string) $this->text); + return new Attribute('href', $this->getUrl()->getAbsoluteUrl('&')); } /** @@ -63,19 +63,7 @@ class Link extends Component */ public function getUrl() { + // TODO: What if null? #? return $this->url; } - - /** - * @inheritdoc - */ - public function render() - { - return sprintf( - '%s', - $this->getUrl(), - $this->attributes->render(), - $this->getRenderedText() - ); - } } diff --git a/library/Businessprocess/Web/Component/ActionBar.php b/library/Businessprocess/Web/Component/ActionBar.php index 887e06f..ec80347 100644 --- a/library/Businessprocess/Web/Component/ActionBar.php +++ b/library/Businessprocess/Web/Component/ActionBar.php @@ -1,10 +1,10 @@ 'action-bar'); + protected $defaultAttributes = array('class' => 'action-bar'); } diff --git a/library/Businessprocess/Web/Component/Component.php b/library/Businessprocess/Web/Component/Component.php deleted file mode 100644 index eaec881..0000000 --- a/library/Businessprocess/Web/Component/Component.php +++ /dev/null @@ -1,139 +0,0 @@ -view = $view; - return $this; - } - - /** - * @return View - */ - public function view() - { - if ($this->view === null) { - $this->view = $this->discoveredView(); - } - return $this->view; - } - - /** - * @return View - */ - protected function discoveredView() - { - if (self::$discoveredView === null) { - $icinga = Icinga::app(); - if ($icinga->isCli()) { - self::$discoveredView = new View(); - } else { - $viewRenderer = $icinga->getViewRenderer(); - if ($viewRenderer->view === null) { - $viewRenderer->initView(); - } - - self::$discoveredView = $viewRenderer->view; - } - } - - return self::$discoveredView; - } - - /** - * @return string - */ - abstract public function render(); - - public function wantHtml($any, $separator = '') - { - if ($any instanceof Component) { - return $any; - } elseif (is_string($any)) { - return $this->view()->escape($any); - } elseif (is_array($any)) { - $safe = array(); - foreach ($any as $el) { - $safe .= $this->wantHtml($el); - } - - return implode($separator, $safe); - } else { - // TODO: Should we add a dedicated Exception class? - throw new IcingaException( - 'String, Web Component or Array of such expected, got "%s"', - $this->getPhpTypeName($any) - ); - } - } - - public function getPhpTypeName($any) - { - if (is_object($any)) { - return get_class($any); - } else { - return gettype($any); - } - } - - /** - * @param Exception|string $error - * @return string - */ - protected function renderError($error) - { - if ($error instanceof Exception) { - $file = preg_split('/[\/\\\]/', $error->getFile(), -1, PREG_SPLIT_NO_EMPTY); - $file = array_pop($file); - $msg = sprintf( - '%s (%s:%d)', - $error->getMessage(), - $file, - $error->getLine() - ); - } elseif (is_string($error)) { - $msg = $error; - } else { - $msg = 'Got an invalid error'; - } - - - - - $view = $this->view(); - return sprintf( - $view->translate('ERROR: %s'), - $view->escape($msg) - ); - } - - /** - * @return string - */ - public function __toString() - { - try { - return $this->render(); - } catch (Exception $e) { - return $this->renderError($e); - } - } -} diff --git a/library/Businessprocess/Web/Component/Container.php b/library/Businessprocess/Web/Component/Container.php deleted file mode 100644 index 69cce5a..0000000 --- a/library/Businessprocess/Web/Component/Container.php +++ /dev/null @@ -1,144 +0,0 @@ -addContent($content); - - if ($this->attributes === null) { - $this->attributes = Attributes::wantAttributes($attributes); - } else { - $this->attributes = Attributes::wantAttributes($this->attributes); - } - if ($tag !== null) { - $this->tag = $tag; - } - } - - /** - * @param Component|array|string $content - * @param Attributes|array $attributes - * @param string $tag - * - * @return static - */ - public static function create($content = array(), $attributes = null, $tag = null) - { - return new static($content, $attributes, $tag); - } - - /** - * @return Attributes - */ - public function attributes() - { - return $this->attributes; - } - - /** - * @see addContent() - * @param Component|array|string $content - * @return $this - */ - public function add($content) - { - return $this->addContent($content); - } - - /** - * @param Component|array|string $content - * @return $this - */ - public function addContent($content) - { - if (is_array($content)) { - foreach ($content as $c) { - $this->addContent($c); - } - } - - $htmlOrComponent = $this->wantHtml($content); - if (strlen($htmlOrComponent)) { - $this->content[] = $htmlOrComponent; - } - - return $this; - } - - /** - * @param Component|array|string $content - * @return $this - */ - public function setContent($content) - { - $this->content = array(); - $this->addContent($content); - - return $this; - } - - /** - * @return string - */ - public function renderContent() - { - return implode($this->separator, $this->content); - } - - /** - * @inheritdoc - */ - public function render() - { - return $this->renderContainerFor($this->renderContent()); - } - - /** - * @inheritdoc - */ - public function renderError($error) - { - // TODO: eventually add class="error" - return $this->renderContainerFor( - parent::renderError($error) - ); - } - - /** - * @param $content - * @return string - */ - protected function renderContainerFor($content) - { - return sprintf( - '<%s%s>%s', - $this->tag, - $this->attributes->render(), - $content, - $this->tag - ); - } -}