Internal Architecture of HydePHP - Part 2: Services

CodeWithCaen - May 12 '22 - - Dev Community

A Deep Dive into Services

Deep-Dives takes a closer look at the codebase and tries to explain what's going on.

This article is the second installation in my series on the internal architecture of HydePHP.
If you haven't read part one already, you should do that first. Here is a quick link Internal Architecture of HydePHP - Part 1: Introduction.

Taking a look at a Service

Let's take a look at a relatively new Service Class, the DocumentationSidebarService, to get a better understanding of how a Service is used through practical examples.

Here is a link to the source code on GitHub.

This Service is as you may have guessed used to manage the sidebar in documentation pages!

When using sidebar data in the Blade views, we interact with the Service to get the data we need.

You are of course free to take a look at the source code to get a better understanding of how it all fits together, however here are some examples loosely plucked from the code.

$sidebar = Hyde\Framework\Services\DocumentationSidebarService::create();

@if($sidebar->hasCategories())
    @foreach ($sidebar->getCategories() as $category)
        @foreach ($sidebar->getItemsInCategory($category) as $item)

@else
    @foreach ($sidebar->getSidebar() as $item)
Enter fullscreen mode Exit fullscreen mode

As you can see, the Service gives access to a lot of data and logic.
However, the Service does not do much of the heavy lifting. True, the Service contains methods to create and modify data,
and while it is responsible for generating the sidebar, it actually leverages other components to do the heavy lifting.

For example, the DocumentationSidebarService::create() method is used to create a new instance of the Service containing
a freshly generated sidebar, but the fetching of items to put in the sidebar is done by the CollectionService, the
actual parsing to get front matter data is done in the sidebar item model. The categorization is handled in a Concern.

/**
 * @example Hyde\Framework\Services\DocumentationSidebarService::create()
 **/
public static function create(): static
{
    return (new static)->createSidebar()->withoutIndex()->withoutHidden();
}
Enter fullscreen mode Exit fullscreen mode

Next Up

Make sure to check out the How Hyde knows what to do with your Markdown post (launching tomorrow).

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .