com web/php: Replace overengineering of 367 lines of code with 29 lines: Entity/NewsItem.php Gateway/NewsFileSystemGateway.php Gateway/NewsGateway.php
View/HomepageNewsView.php index.php
| From: | Hannes Magnusson | Date: | Thu, 21 Nov 2013 04:54:22 +0000 |
| Subject: | com web/php: Replace overengineering of 367 lines of code with 29 lines: Entity/NewsItem.php Gateway/NewsFileSystemGateway.php Gateway/NewsGateway.php View/HomepageNewsView.php index.php |
||
| Groups: | php.webmaster | ||
| Request: | Send a blank email to php-webmaster+get-17133@lists.php.net to get a copy of this message | ||
Commit: f46b45e9d5d8e4ee3ea07a3e45d057237af2a375
Author: Hannes Magnusson <bjori@mongodb.com> Wed, 20 Nov 2013 20:51:57 -0800
Parents: ea1ec3575286453704133acd012976130635d16f
Branches: master
Link: http://git.php.net/?p=web/php.git;a=commitdiff;h=f46b45e9d5d8e4ee3ea07a3e45d057237af2a375
Log:
Replace overengineering of 367 lines of code with 29 lines
Changed paths:
D Entity/NewsItem.php
D Gateway/NewsFileSystemGateway.php
D Gateway/NewsGateway.php
D View/HomepageNewsView.php
M index.php
diff --git a/Entity/NewsItem.php b/Entity/NewsItem.php deleted file mode 100644 index 6f1db10..0000000 --- a/Entity/NewsItem.php +++ /dev/null @@ -1,158 +0,0 @@ -<?php - -class NewsItem { - - /** - * @var string - */ - protected $permanentUrl; - - /** - * @var string - */ - protected $title; - - /** - * @var string - */ - protected $published; - - /** - * @var DateTime - */ - protected $updated; - - /** - * @var array - */ - protected $category = array(); - - /** - * @var array - */ - protected $link = array(); - - /** - * @var string - */ - protected $content = ''; - - /** - * @var string - */ - protected $newsImage; - - /** - * @var string - */ - protected $intro = ''; - - - /** - * @param string $title - * @param string $permanentUrl - * @param string $published - * @param string $updated - * @param array $category - * @param array $link - * @param string $content - * @param string $newsImage - * @param string $intro - */ - function __construct($title, $permanentUrl, $published, $updated, $category, $link, $content, $newsImage = NULL, $intro = '') { - $this->title = $title; - $this->permanentUrl = $permanentUrl; - $this->published = $published; - $this->updated = new DateTime($updated); - $this->category = $category; - $this->link = $link; - $this->content = $content; - $this->newsImage = $newsImage; - $this->intro = $intro; - } - - /** - * @return array - */ - public function getCategories() { - return $this->category; - } - - /** - * @return string - */ - public function getContent() { - return $this->content; - } - - /** - * @return string - */ - public function getId() { - return parse_url($this->getPermanentUrl(), PHP_URL_FRAGMENT); - } - - /** - * @return string - */ - public function getPermanentUrl() { - return $this->permanentUrl; - } - - /** - * @return array - */ - public function getLink() { - return $this->link; - } - - public function getNewsImage() { - return $this->newsImage; - } - - /** - * @return DateTime - */ - public function getPublishedDate() { - return new DateTime($this->published); - } - - /** - * @return string - */ - public function getPublishedString() { - return $this->published; - } - - /** - * @return mixed - */ - public function getTitle() { - return $this->title; - } - - /** - * @return DateTime - */ - public function getUpdatedDate() { - return $this->updated; - } - - /** - * @param $category - * @return bool - */ - public function hasCategory($category) { - foreach ($this->category as $cat) { - if ($cat['term'] === $category) { - return true; - } - } - return false; - } - - public function getIntroduction() { - return $this->intro; - } - -} diff --git a/Gateway/NewsFileSystemGateway.php b/Gateway/NewsFileSystemGateway.php deleted file mode 100644 index 6bc6bb2..0000000 --- a/Gateway/NewsFileSystemGateway.php +++ /dev/null @@ -1,72 +0,0 @@ -<?php - -require_once dirname(__FILE__) . '/../Entity/NewsItem.php'; -require_once dirname(__FILE__) . '/NewsGateway.php'; - -class NewsFileSystemGateway implements NewsGateway { - - /** - * @var array - */ - protected $articles; - - public function __construct() { - - $NEWS_ENTRIES = array(); - - //will overwrite $NEWS_ENTRIES. - require __DIR__ . '/../include/pregen-news.inc'; - - $this->articles = array(); - - foreach ($NEWS_ENTRIES as $article) { - $this->articles[] = new NewsItem( - $article['title'], - $article['id'], - $article['published'], - $article['updated'], - $article['category'], - $article['link'], - $article['content'], - isset($article['newsImage']) ? $article['newsImage'] : NULL, - isset($article['intro']) ? $article['intro'] : $article['content'] - ); - } - - } - - /** - * @param int $max [optional] - * @return array - */ - public function getLatestArticles($max = 5) { - return array_slice($this->articles, 0, $max); - } - - /** - * @param array $categories - * @param int $limit - * @return array - */ - public function getArticlesForCategories(array $categories, $limit = 5) { - $result = array(); - - foreach ($this->articles as $item) { - /** - * @var NewsItem $item - */ - $articleCategories = array(); - - foreach ($categories as $category) { - if ($item->hasCategory($category)) { - $result[] = $item; - } - } - - } - - return array_slice($result, 0 , $limit); - - } - -} diff --git a/Gateway/NewsGateway.php b/Gateway/NewsGateway.php deleted file mode 100644 index 9af903a..0000000 --- a/Gateway/NewsGateway.php +++ /dev/null @@ -1,20 +0,0 @@ -<?php - -interface NewsGateway { - - /** - * @abstract - * @param int $max [optional] - * @return array - */ - function getLatestArticles($max = 5); - - /** - * @abstract - * @param array $categories - * @param int $limit [optional] - * @return array - */ - function getArticlesForCategories(array $categories, $limit = 5); - -} diff --git a/View/HomepageNewsView.php b/View/HomepageNewsView.php deleted file mode 100644 index 1390814..0000000 --- a/View/HomepageNewsView.php +++ /dev/null @@ -1,95 +0,0 @@ -<?php - -class HomepageNewsView { - - /** - * @var array - */ - protected $articles; - - /** - * @param array $articles - */ - public function __construct(array $articles) { - $this->articles = $articles; - } - - public function render() { - - ob_start(); - ?> - <div id='recentNewsEntries'> - <?php - foreach ($this->articles as $article) : - /** - * @var NewsItem $article - */ - - $id = $article->getId(); - $title = $article->getTitle(); - - $publishedDate = $article->getPublishedString(); - $newsDate = $article->getPublishedDate()->format('d-M-Y'); - - $permanentLink = "#" .$id; - foreach($article->getLink() as $link) { - if ($link["rel"] === "via") { - $permanentLink = $link["href"]; - break; - } - } - - $content = $article->getContent(); - - $image = ""; - $newsImage = $article->getNewsImage(); - if(isset($newsImage["link"]) && $newsImage["content"]) { - $image = "<a href=\"{$newsImage["link"]}\">" . $this->renderImage($newsImage) . "</a>"; - } - - $event = ''; - if ($article->hasCategory('conferences') || $article->hasCategory('cfp')) { - $event = " vevent"; - } - echo "<div class='newsItem hentry{$event}'> - <div class='newsImage'>{$image}</div> - <h2 class='summary entry-title' id='$id'><a href='{$permanentLink}' rel='bookmark' class='bookmark'>{$title}</a></h2> - <div class='entry-content description'> - <abbr class='published newsdate' title='{$publishedDate}'>{$newsDate}</abbr> - {$content} - </div> - </div>"; - endforeach; - ?> - - <p class='newsArchive'><a href='/archive/'>More news »</a></p> - - </div> - - - <?php - - return ob_get_clean(); - - } - - /** - * @param array $image - * @return string - */ - protected function renderImage(array $image) { - $imageHtml = "<img src='/images/news/{$image['content']}' alt='{$image['title']}' style='float:right'"; - - //getimagesize will give the HTML width and height attributes in offset 3 of the return value - $imageDetails = @getimagesize($_SERVER['DOCUMENT_ROOT'] . '/images/news/' . $image['content']); - - if (count($imageDetails) >= 4) { - $imageHtml .= $imageDetails[3]; - } - - $imageHtml .= "/>"; - - return $imageHtml; - } - -} diff --git a/index.php b/index.php index 77731a5..f16d09d 100644 --- a/index.php +++ b/index.php @@ -50,28 +50,35 @@ include_once 'include/pregen-news.inc'; include_once 'include/version.inc'; -require_once './Gateway/NewsFileSystemGateway.php'; - -$NewsGateway = new NewsFileSystemGateway(); -$RecentNews = $NewsGateway->getArticlesForCategories(array( - "frontpage" -), 4); - -ob_start(); - -require_once './View/HomepageNewsView.php'; -$RecentNewsView = new HomepageNewsView($RecentNews); - -echo $RecentNewsView->render(); - -$news = ob_get_clean(); - -// Wrap announcements and features in a content element -$content = " -<div class='home-content'> - $news -</div> -"; +$content = "<div class='home-content'>"; +$releasenews = 0; +$frontpage = array(); +foreach($NEWS_ENTRIES as $entry) { + $maybe = false; + foreach($entry["category"] as $category) { + if ($category["term"] == "releases") { + if ($releasenews++ > 5) { + continue 2; + } + } + if ($category["term"] == "frontpage") { + $maybe = $entry; + } + } + if ($maybe) { + $frontpage[] = $maybe; + } +} +foreach($frontpage as $entry) { + $link = substr($entry["id"], 15); // Strip http://php.net/ + $content .= '<div class="newsentry">'; + $content .= '<div class="newstitle"><a href="'. $MYSITE.$link .'">' . $entry["title"] . '</a></div>'; + $content .= '<div class="newscontent">'; + $content .= $entry["content"]; + $content .= '</div>'; + $content .= '</div>'; +} +$content .= "</div>"; $intro = <<<EOF <div class="row-fluid">
diff --git a/Entity/NewsItem.php b/Entity/NewsItem.php deleted file mode 100644 index 6f1db10..0000000 --- a/Entity/NewsItem.php +++ /dev/null @@ -1,158 +0,0 @@ -<?php - -class NewsItem { - - /** - * @var string - */ - protected $permanentUrl; - - /** - * @var string - */ - protected $title; - - /** - * @var string - */ - protected $published; - - /** - * @var DateTime - */ - protected $updated; - - /** - * @var array - */ - protected $category = array(); - - /** - * @var array - */ - protected $link = array(); - - /** - * @var string - */ - protected $content = ''; - - /** - * @var string - */ - protected $newsImage; - - /** - * @var string - */ - protected $intro = ''; - - - /** - * @param string $title - * @param string $permanentUrl - * @param string $published - * @param string $updated - * @param array $category - * @param array $link - * @param string $content - * @param string $newsImage - * @param string $intro - */ - function __construct($title, $permanentUrl, $published, $updated, $category, $link, $content, $newsImage = NULL, $intro = '') { - $this->title = $title; - $this->permanentUrl = $permanentUrl; - $this->published = $published; - $this->updated = new DateTime($updated); - $this->category = $category; - $this->link = $link; - $this->content = $content; - $this->newsImage = $newsImage; - $this->intro = $intro; - } - - /** - * @return array - */ - public function getCategories() { - return $this->category; - } - - /** - * @return string - */ - public function getContent() { - return $this->content; - } - - /** - * @return string - */ - public function getId() { - return parse_url($this->getPermanentUrl(), PHP_URL_FRAGMENT); - } - - /** - * @return string - */ - public function getPermanentUrl() { - return $this->permanentUrl; - } - - /** - * @return array - */ - public function getLink() { - return $this->link; - } - - public function getNewsImage() { - return $this->newsImage; - } - - /** - * @return DateTime - */ - public function getPublishedDate() { - return new DateTime($this->published); - } - - /** - * @return string - */ - public function getPublishedString() { - return $this->published; - } - - /** - * @return mixed - */ - public function getTitle() { - return $this->title; - } - - /** - * @return DateTime - */ - public function getUpdatedDate() { - return $this->updated; - } - - /** - * @param $category - * @return bool - */ - public function hasCategory($category) { - foreach ($this->category as $cat) { - if ($cat['term'] === $category) { - return true; - } - } - return false; - } - - public function getIntroduction() { - return $this->intro; - } - -} diff --git a/Gateway/NewsFileSystemGateway.php b/Gateway/NewsFileSystemGateway.php deleted file mode 100644 index 6bc6bb2..0000000 --- a/Gateway/NewsFileSystemGateway.php +++ /dev/null @@ -1,72 +0,0 @@ -<?php - -require_once dirname(__FILE__) . '/../Entity/NewsItem.php'; -require_once dirname(__FILE__) . '/NewsGateway.php'; - -class NewsFileSystemGateway implements NewsGateway { - - /** - * @var array - */ - protected $articles; - - public function __construct() { - - $NEWS_ENTRIES = array(); - - //will overwrite $NEWS_ENTRIES. - require __DIR__ . '/../include/pregen-news.inc'; - - $this->articles = array(); - - foreach ($NEWS_ENTRIES as $article) { - $this->articles[] = new NewsItem( - $article['title'], - $article['id'], - $article['published'], - $article['updated'], - $article['category'], - $article['link'], - $article['content'], - isset($article['newsImage']) ? $article['newsImage'] : NULL, - isset($article['intro']) ? $article['intro'] : $article['content'] - ); - } - - } - - /** - * @param int $max [optional] - * @return array - */ - public function getLatestArticles($max = 5) { - return array_slice($this->articles, 0, $max); - } - - /** - * @param array $categories - * @param int $limit - * @return array - */ - public function getArticlesForCategories(array $categories, $limit = 5) { - $result = array(); - - foreach ($this->articles as $item) { - /** - * @var NewsItem $item - */ - $articleCategories = array(); - - foreach ($categories as $category) { - if ($item->hasCategory($category)) { - $result[] = $item; - } - } - - } - - return array_slice($result, 0 , $limit); - - } - -} diff --git a/Gateway/NewsGateway.php b/Gateway/NewsGateway.php deleted file mode 100644 index 9af903a..0000000 --- a/Gateway/NewsGateway.php +++ /dev/null @@ -1,20 +0,0 @@ -<?php - -interface NewsGateway { - - /** - * @abstract - * @param int $max [optional] - * @return array - */ - function getLatestArticles($max = 5); - - /** - * @abstract - * @param array $categories - * @param int $limit [optional] - * @return array - */ - function getArticlesForCategories(array $categories, $limit = 5); - -} diff --git a/View/HomepageNewsView.php b/View/HomepageNewsView.php deleted file mode 100644 index 1390814..0000000 --- a/View/HomepageNewsView.php +++ /dev/null @@ -1,95 +0,0 @@ -<?php - -class HomepageNewsView { - - /** - * @var array - */ - protected $articles; - - /** - * @param array $articles - */ - public function __construct(array $articles) { - $this->articles = $articles; - } - - public function render() { - - ob_start(); - ?> - <div id='recentNewsEntries'> - <?php - foreach ($this->articles as $article) : - /** - * @var NewsItem $article - */ - - $id = $article->getId(); - $title = $article->getTitle(); - - $publishedDate = $article->getPublishedString(); - $newsDate = $article->getPublishedDate()->format('d-M-Y'); - - $permanentLink = "#" .$id; - foreach($article->getLink() as $link) { - if ($link["rel"] === "via") { - $permanentLink = $link["href"]; - break; - } - } - - $content = $article->getContent(); - - $image = ""; - $newsImage = $article->getNewsImage(); - if(isset($newsImage["link"]) && $newsImage["content"]) { - $image = "<a href=\"{$newsImage["link"]}\">" . $this->renderImage($newsImage) . "</a>"; - } - - $event = ''; - if ($article->hasCategory('conferences') || $article->hasCategory('cfp')) { - $event = " vevent"; - } - echo "<div class='newsItem hentry{$event}'> - <div class='newsImage'>{$image}</div> - <h2 class='summary entry-title' id='$id'><a href='{$permanentLink}' rel='bookmark' class='bookmark'>{$title}</a></h2> - <div class='entry-content description'> - <abbr class='published newsdate' title='{$publishedDate}'>{$newsDate}</abbr> - {$content} - </div> - </div>"; - endforeach; - ?> - - <p class='newsArchive'><a href='/archive/'>More news »</a></p> - - </div> - - - <?php - - return ob_get_clean(); - - } - - /** - * @param array $image - * @return string - */ - protected function renderImage(array $image) { - $imageHtml = "<img src='/images/news/{$image['content']}' alt='{$image['title']}' style='float:right'"; - - //getimagesize will give the HTML width and height attributes in offset 3 of the return value - $imageDetails = @getimagesize($_SERVER['DOCUMENT_ROOT'] . '/images/news/' . $image['content']); - - if (count($imageDetails) >= 4) { - $imageHtml .= $imageDetails[3]; - } - - $imageHtml .= "/>"; - - return $imageHtml; - } - -} diff --git a/index.php b/index.php index 77731a5..f16d09d 100644 --- a/index.php +++ b/index.php @@ -50,28 +50,35 @@ include_once 'include/pregen-news.inc'; include_once 'include/version.inc'; -require_once './Gateway/NewsFileSystemGateway.php'; - -$NewsGateway = new NewsFileSystemGateway(); -$RecentNews = $NewsGateway->getArticlesForCategories(array( - "frontpage" -), 4); - -ob_start(); - -require_once './View/HomepageNewsView.php'; -$RecentNewsView = new HomepageNewsView($RecentNews); - -echo $RecentNewsView->render(); - -$news = ob_get_clean(); - -// Wrap announcements and features in a content element -$content = " -<div class='home-content'> - $news -</div> -"; +$content = "<div class='home-content'>"; +$releasenews = 0; +$frontpage = array(); +foreach($NEWS_ENTRIES as $entry) { + $maybe = false; + foreach($entry["category"] as $category) { + if ($category["term"] == "releases") { + if ($releasenews++ > 5) { + continue 2; + } + } + if ($category["term"] == "frontpage") { + $maybe = $entry; + } + } + if ($maybe) { + $frontpage[] = $maybe; + } +} +foreach($frontpage as $entry) { + $link = substr($entry["id"], 15); // Strip http://php.net/ + $content .= '<div class="newsentry">'; + $content .= '<div class="newstitle"><a href="'. $MYSITE.$link .'">' . $entry["title"] . '</a></div>'; + $content .= '<div class="newscontent">'; + $content .= $entry["content"]; + $content .= '</div>'; + $content .= '</div>'; +} +$content .= "</div>"; $intro = <<<EOF <div class="row-fluid">