Skip to content

Commit

Permalink
Begin document kernel lifecycle
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Nov 3, 2022
1 parent 3956065 commit e3cce2d
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions docs/advanced-features/autodiscovery.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,47 @@ as a singleton into the application service container.
At this point you might be wondering why we're talking about the kernel when this article is about autodiscovery.
Well, as you'll see in just a moment, the kernel is responsible for initiating the autodiscovery process.
The kernel is also where the discovered data is stored in memory, so it's important to understand how it works.

### The kernel lifecycle

Now that we know the role of the HydeKernel, let's take a look at its lifecycle. The kernel is "lazy-booted", meaning
that the all the heavy lifting only happens when you actually need it. Once booted, the kernel data will stay in memory
until the application is terminated.

The kernel data is primarily stored in three collections that get generated during the kernel's boot process.
Let's take a look at the kernel's boot method to see how this works.

```php
public function boot(): void
{
$this->booted = true;

$this->files = FileCollection::boot($this);
$this->pages = PageCollection::boot($this);
$this->routes = RouteCollection::boot($this);
}
```

Here you'll see that we boot the three collections. This is where all the autodiscovery magic happens!
We'll take a closer look at each of these in a second, but first, here's how the "lazy-booting" works.

```php
// This will boot the kernel if it hasn't been booted yet
protected function needsToBeBooted(): void
{
if (! $this->booted) {
$this->boot();
}
}

// And here's an example of how it's used
public function pages(): PageCollection
{
$this->needsToBeBooted();

return $this->pages;
}
```

Yeah, it's really unglamorous I know. But it works! Having it like this will ensure that any time you call `Hyde::pages()`,
that underlying collection will always have been booted and be ready to use.

0 comments on commit e3cce2d

Please sign in to comment.