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/namespace/Application.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/namespace/Application.php')
-rw-r--r-- | core/namespace/Application.php | 24 |
1 files changed, 20 insertions, 4 deletions
diff --git a/core/namespace/Application.php b/core/namespace/Application.php index f9a8196..7473e1f 100644 --- a/core/namespace/Application.php +++ b/core/namespace/Application.php @@ -6,6 +6,7 @@ class Application { #=============================================================================== private static $Database; private static $Language; + private static $repositories = []; #=============================================================================== # Configuration array @@ -68,6 +69,21 @@ class Application { } #=============================================================================== + # Return singleton repository instance + #=============================================================================== + public function getRepository(string $namespace): Repository { + $identifier = strtolower($namespace); + $repository = "$namespace\Repository"; + + if(!isset(self::$repositories[$identifier])) { + $Repository = new $repository(self::getDatabase()); + self::$repositories[$identifier] = $Repository; + } + + return self::$repositories[$identifier]; + } + + #=============================================================================== # Return unique CSRF token for the current session #=============================================================================== public static function getSecurityToken(): string { @@ -142,15 +158,15 @@ class Application { #=============================================================================== # Return absolute URL of a specifc entity #=============================================================================== - public function getEntityURL(Item $Entity) { + public function getEntityURL(EntityInterface $Entity) { switch($class = get_class($Entity)) { - case 'Page\Item': + case 'Page\Entity': $attr = self::get('PAGE.SLUG_URLS') ? 'slug' : 'id'; return self::getPageURL($Entity->get($attr).'/'); - case 'Post\Item': + case 'Post\Entity': $attr = self::get('POST.SLUG_URLS') ? 'slug' : 'id'; return self::getPostURL($Entity->get($attr).'/'); - case 'User\Item': + case 'User\Entity': $attr = self::get('USER.SLUG_URLS') ? 'slug' : 'id'; return self::getUserURL($Entity->get($attr).'/'); default: |