diff options
author | Thomas Lange <code@nerdmind.de> | 2021-06-22 01:18:02 +0200 |
---|---|---|
committer | Thomas Lange <code@nerdmind.de> | 2021-06-22 01:46:42 +0200 |
commit | 8cd1105b111b89106f24c5b50795afb5ff28a935 (patch) | |
tree | c789faac9de4d83cb409c45779e76be8938a4d66 /core/include/post/main.php | |
parent | 7937df540b7d70b2bb87797442d0a0a0df197133 (diff) | |
download | blog-8cd1105b111b89106f24c5b50795afb5ff28a935.tar.gz blog-8cd1105b111b89106f24c5b50795afb5ff28a935.tar.xz blog-8cd1105b111b89106f24c5b50795afb5ff28a935.zip |
Implement new Repository and Entity classes
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. :)
Diffstat (limited to 'core/include/post/main.php')
-rw-r--r-- | core/include/post/main.php | 113 |
1 files changed, 53 insertions, 60 deletions
diff --git a/core/include/post/main.php b/core/include/post/main.php index 57cc402..86008f6 100644 --- a/core/include/post/main.php +++ b/core/include/post/main.php @@ -1,79 +1,72 @@ <?php #=============================================================================== -# Get instances +# Get repositories #=============================================================================== -$Database = Application::getDatabase(); -$Language = Application::getLanguage(); +$PostRepository = Application::getRepository('Post'); +$UserRepository = Application::getRepository('User'); #=============================================================================== -# TRY: Post\Exception +# Try to find post by slug URL or unique ID #=============================================================================== -try { - if(Application::get('POST.SLUG_URLS')) { - $Post = Post\Factory::buildBySlug($param); +if(Application::get('POST.SLUG_URLS')) { + if(!$Post = $PostRepository->findBy('slug', $param)) { + if($Post = $PostRepository->find($param)) { + HTTP::redirect(Application::getEntityURL($Post)); + } } +} - else { - $Post = Post\Factory::build($param); +else { + if(!$Post = $PostRepository->find($param)) { + if($Post = $PostRepository->findBy('slug', $param)) { + HTTP::redirect(Application::getEntityURL($Post)); + } } +} - $User = User\Factory::build($Post->get('user')); - - $post_data = generateItemTemplateData($Post); - $user_data = generateItemTemplateData($User); - - #=============================================================================== - # Add post data for previous and next post - #=============================================================================== - try { - $PrevPost = Post\Factory::build($Post->getPrevID()); - $post_data['PREV'] = generateItemTemplateData($PrevPost); - } catch(Post\Exception $Exception){} - - try { - $NextPost = Post\Factory::build($Post->getNextID()); - $post_data['NEXT'] = generateItemTemplateData($NextPost); - } catch(Post\Exception $Exception){} - - #=============================================================================== - # Build document - #=============================================================================== - $PostTemplate = Template\Factory::build('post/main'); - $PostTemplate->set('POST', $post_data); - $PostTemplate->set('USER', $user_data); +#=============================================================================== +# Throw 404 error if post could not be found +#=============================================================================== +if(!isset($Post)) { + Application::error404(); +} - $MainTemplate = Template\Factory::build('main'); - $MainTemplate->set('HTML', $PostTemplate); - $MainTemplate->set('HEAD', [ - 'NAME' => $post_data['ATTR']['NAME'], - 'DESC' => description($post_data['BODY']['HTML'](), Application::get('POST.DESCRIPTION_SIZE')), - 'PERM' => $post_data['URL'], - 'OG_IMAGES' => $post_data['FILE']['LIST'] - ]); +#=============================================================================== +# Generate template data +#=============================================================================== +$User = $UserRepository->find($Post->get('user')); +$post_data = generateItemTemplateData($Post); +$user_data = generateItemTemplateData($User); - # Get access to the current item data from main template - $MainTemplate->set('TYPE', 'POST'); - $MainTemplate->set('POST', $post_data); - $MainTemplate->set('USER', $user_data); +#=============================================================================== +# Add template data for previous and next post +#=============================================================================== +if($PrevPost = $PostRepository->findPrev($Post)) { + $post_data['PREV'] = generateItemTemplateData($PrevPost); +} - echo $MainTemplate; +if($NextPost = $PostRepository->findNext($Post)) { + $post_data['NEXT'] = generateItemTemplateData($NextPost); } #=============================================================================== -# CATCH: Post\Exception +# Build document #=============================================================================== -catch(Post\Exception $Exception) { - try { - if(Application::get('POST.SLUG_URLS') === FALSE) { - $Post = Post\Factory::buildBySlug($param); - } else { - $Post = Post\Factory::build($param); - } +$PostTemplate = Template\Factory::build('post/main'); +$PostTemplate->set('POST', $post_data); +$PostTemplate->set('USER', $user_data); - HTTP::redirect(Application::getEntityURL($Post)); - } +$MainTemplate = Template\Factory::build('main'); +$MainTemplate->set('TYPE', 'POST'); +$MainTemplate->set('POST', $post_data); +$MainTemplate->set('USER', $user_data); +$MainTemplate->set('HTML', $PostTemplate); +$MainTemplate->set('HEAD', [ + 'NAME' => $post_data['ATTR']['NAME'], + 'DESC' => description($post_data['BODY']['HTML'](), + Application::get('POST.DESCRIPTION_SIZE')), + 'PERM' => $post_data['URL'], + 'OG_IMAGES' => $post_data['FILE']['LIST'] +]); - catch(Post\Exception $Exception) { - Application::error404(); - } -} +echo $MainTemplate; |