From 8cd1105b111b89106f24c5b50795afb5ff28a935 Mon Sep 17 00:00:00 2001 From: Thomas Lange Date: Tue, 22 Jun 2021 01:18:02 +0200 Subject: Implement new Repository and Entity classes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit adds new Repository and Entity classes which are better abstracted from the rest of the application. They dont know anymore about configuration options or how to parse to HTML because this is not the job for the ORM but for other parts of the application. The previous commits were a preparation for this big change. An entity now represents just a single record from a specific table of the database – nothing more. The repositories job is it to fetch or update records of the database and instantiate the entities. Another problem that was solved is the high amount of database queries that was needed before. For example, on the blogs home page first were all 10 latest post IDs fetched from the database and then another query was executed with "WHERE id = :id" for *each* single post?! ... This problem is solved with the new repository classes; they now use a single query to fetch and build the entities of the 10 latest posts. This change also solves the problem with database queries spread across the application and limits the exzessive use of try/catch blocks which were used before. The new classes make the whole code much cleaner. :) --- core/functions.php | 109 +++++++++++++++++++++++++++++------------------------ 1 file changed, 59 insertions(+), 50 deletions(-) (limited to 'core/functions.php') diff --git a/core/functions.php b/core/functions.php index 31a295b..55744f1 100644 --- a/core/functions.php +++ b/core/functions.php @@ -1,16 +1,20 @@ query(sprintf('SELECT COUNT(id) FROM %s', $Attribute::TABLE)); +function generateNaviTemplate(int $current, $location, $namespace): Template { + $Repository = Application::getRepository($namespace); - $lastSite = ceil($Statement->fetchColumn() / Application::get(strtoupper($namespace).'.LIST_SIZE')); + $listSize = Application::get(strtoupper($namespace).'.LIST_SIZE'); + $lastSite = ceil($Repository->getCount() / $listSize); - $PaginationTemplate = Template\Factory::build('pagination'); + $PaginationTemplate = TemplateFactory::build('pagination'); $PaginationTemplate->set('THIS', $current); $PaginationTemplate->set('LAST', $lastSite); $PaginationTemplate->set('HREF', "{$location}?site=%d"); @@ -21,29 +25,29 @@ function generateNaviTemplate(int $current, $location, $namespace): Template\Tem #=============================================================================== # Helper function to reduce duplicate code #=============================================================================== -function generatePageNaviTemplate($current): Template\Template { +function generatePageNaviTemplate($current): Template { return generateNaviTemplate($current, Application::getPageURL(), 'Page'); } #=============================================================================== # Helper function to reduce duplicate code #=============================================================================== -function generatePostNaviTemplate($current): Template\Template { +function generatePostNaviTemplate($current): Template { return generateNaviTemplate($current, Application::getPostURL(), 'Post'); } #=============================================================================== # Helper function to reduce duplicate code #=============================================================================== -function generateUserNaviTemplate($current): Template\Template { +function generateUserNaviTemplate($current): Template { return generateNaviTemplate($current, Application::getUserURL(), 'User'); } #=============================================================================== # Helper function to reduce duplicate code #=============================================================================== -function generatePageItemTemplate(Page\Item $Page, User\Item $User): Template\Template { - $Template = Template\Factory::build('page/item'); +function generatePageItemTemplate(Page $Page, User $User): Template { + $Template = TemplateFactory::build('page/item'); $Template->set('PAGE', generateItemTemplateData($Page)); $Template->set('USER', generateItemTemplateData($User)); @@ -53,8 +57,8 @@ function generatePageItemTemplate(Page\Item $Page, User\Item $User): Template\Te #=============================================================================== # Helper function to reduce duplicate code #=============================================================================== -function generatePostItemTemplate(Post\Item $Post, User\Item $User): Template\Template { - $Template = Template\Factory::build('post/item'); +function generatePostItemTemplate(Post $Post, User $User): Template { + $Template = TemplateFactory::build('post/item'); $Template->set('POST', generateItemTemplateData($Post)); $Template->set('USER', generateItemTemplateData($User)); @@ -64,8 +68,8 @@ function generatePostItemTemplate(Post\Item $Post, User\Item $User): Template\Te #=============================================================================== # Helper function to reduce duplicate code #=============================================================================== -function generateUserItemTemplate(User\Item $User): Template\Template { - $Template = Template\Factory::build('user/item'); +function generateUserItemTemplate(User $User): Template { + $Template = TemplateFactory::build('user/item'); $Template->set('USER', generateItemTemplateData($User)); return $Template; @@ -74,16 +78,16 @@ function generateUserItemTemplate(User\Item $User): Template\Template { #=============================================================================== # Helper function to reduce duplicate code #=============================================================================== -function generateItemTemplateData(Item $Item): array { - $ATTR = $Item->getAttribute()->getAll(['password']); +function generateItemTemplateData(EntityInterface $Entity): array { + $ATTR = $Entity->getAll(['password']); $ATTR = array_change_key_case($ATTR, CASE_UPPER); - $preparsed = parseContentTags($Item->get('body')); + $preparsed = parseContentTags($Entity->get('body')); return [ - 'URL' => Application::getEntityURL($Item), - 'GUID' => generatePseudoGUID($Item), - 'ARGV' => parseArguments($Item->get('argv')), + 'URL' => Application::getEntityURL($Entity), + 'GUID' => generatePseudoGUID($Entity), + 'ARGV' => parseArguments($Entity->get('argv')), 'ATTR' => $ATTR, @@ -95,11 +99,11 @@ function generateItemTemplateData(Item $Item): array { ], 'BODY' => [ - 'TEXT' => function() use($Item) { + 'TEXT' => function() use($Entity) { return $preparsed; }, - 'HTML' => function() use($Item) { - return parseEntityContent($Item); + 'HTML' => function() use($Entity) { + return parseEntityContent($Entity); } ] ]; @@ -108,12 +112,12 @@ function generateItemTemplateData(Item $Item): array { #=============================================================================== # Generate pseudo GUID for entity #=============================================================================== -function generatePseudoGUID(Item $Entity) { +function generatePseudoGUID(EntityInterface $Entity) { switch(get_class($Entity)) { - case "Page\Item": + case "Page\Entity": $attr = Application::get('PAGE.FEED_GUID'); break; - case "Post\Item": + case "Post\Entity": $attr = Application::get('POST.FEED_GUID'); break; default: @@ -134,12 +138,14 @@ function parseContentTags(string $text): string { $entity_tags = '#\{(POST|PAGE|USER)\[([0-9]+)\]\}#'; $text = preg_replace_callback($entity_tags, function($matches) { - $namespace = ucfirst(strtolower($matches[1])).'\\Factory'; + $namespace = ucfirst(strtolower($matches[1])); + $Repository = Application::getRepository($namespace); - try { - $Entity = $namespace::build($matches[2]); + if($Entity = $Repository->find($matches[2])) { return Application::getEntityURL($Entity); - } catch(Exception $Exception) { + } + + else { return '{undefined}'; } }, $text); @@ -156,15 +162,15 @@ function parseContentTags(string $text): string { #=============================================================================== # Parse entity content #=============================================================================== -function parseEntityContent(Item $Item): string { - switch($class = get_class($Item)) { - case 'Page\Item': +function parseEntityContent(EntityInterface $Entity): string { + switch($class = get_class($Entity)) { + case 'Page\Entity': $prefix = 'PAGE'; break; - case 'Post\Item': + case 'Post\Entity': $prefix = 'POST'; break; - case 'User\Item': + case 'User\Entity': $prefix = 'USER'; break; default: @@ -175,7 +181,7 @@ function parseEntityContent(Item $Item): string { $Parsedown = new Parsedown(); $Parsedown->setUrlsLinked(FALSE); - $text = parseContentTags($Item->get('body')); + $text = parseContentTags($Entity->get('body')); if(Application::get("$prefix.EMOTICONS")) { $text = parseUnicodeEmoticons($text); @@ -409,34 +415,37 @@ function generateSlug($string, $separator = '-') { # Function to get data from specific page in templates #=============================================================================== function PAGE(int $id): array { - try { - $Page = Page\Factory::build($id); + $Repository = Application::getRepository('Page'); + + if($Page = $Repository->find($id)) { return generateItemTemplateData($Page); - } catch(Page\Exception $Exception) { - return []; } + + return []; } #=============================================================================== # Function to get data from specific post in templates #=============================================================================== function POST(int $id): array { - try { - $Post = Post\Factory::build($id); + $Repository = Application::getRepository('Post'); + + if($Post = $Repository->find($id)) { return generateItemTemplateData($Post); - } catch(Post\Exception $Exception) { - return []; } + + return []; } #=============================================================================== # Function to get data from specific user in templates #=============================================================================== function USER(int $id): array { - try { - $User = User\Factory::build($id); + $Repository = Application::getRepository('User'); + + if($User = $Repository->find($id)) { return generateItemTemplateData($User); - } catch(User\Exception $Exception) { - return []; } + + return []; } -- cgit v1.2.3