DEV Community

Cover image for Internal Architecture of HydePHP - Part 2: Services
CodeWithCaen
CodeWithCaen

Posted on • Edited on • Originally published at hydephp.com

5 3

Internal Architecture of HydePHP - Part 2: Services

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).

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay