2

Internal Architecture of HydePHP - Part 2: Services

 1 year ago
source link: https://hydephp.github.io/posts/internal-architecture-of-hydephp-part-2.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Internal Architecture of HydePHP - Part 2: Services

Posted May 12th, 2022 by author Caen in the category "deepdives"
Background image of futuristic circles

Image by Reto Scheiwiller. License Pixabay License

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.

1$sidebar = Hyde\Framework\Services\DocumentationSidebarService::create();
3@if($sidebar->hasCategories())
4 @foreach ($sidebar->getCategories() as $category)
5 @foreach ($sidebar->getItemsInCategory($category) as $item)
7@else
8 @foreach ($sidebar->getSidebar() as $item)

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.

2 * @example Hyde\Framework\Services\DocumentationSidebarService::create()
4public static function create(): static
6 return (new static)->createSidebar()->withoutIndex()->withoutHidden();

Next Up

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

Syntax highlighting by Torchlight.dev

End of article


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK