Skip to content

Commit 5e51659

Browse files
committed
Review of Chapter 18
1 parent afbbaa2 commit 5e51659

15 files changed

Lines changed: 101 additions & 99 deletions

File tree

app/pages/6.0/18.advanced/01.custom-models/docs.md

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,49 @@
11
---
22
title: Using Custom Data Models
33
description: The dependency injector makes it easy to override entire data models in your Sprinkle.
4-
wip: true
54
---
65

7-
Extending PHP classes is a little different from extending other types of entities. You cannot simply replace a class by redefining it in a custom Sprinkle. In fact, classes with the same name in two different Sprinkles would be treated as two different fully-qualified classes per the [PSR-4 standard](http://www.php-fig.org/psr/psr-4/). For example, if I loaded the Sprinkles `Account` and `Site`, and I had the following structure:
6+
Extending PHP classes is a little different from extending other types of entities. You cannot simply replace a class by redefining it in a custom Sprinkle. In fact, classes with the same name in two different Sprinkles would be treated as two different fully-qualified classes per the [PSR-4 standard](http://www.php-fig.org/psr/psr-4/). For example, if the `Account` Sprinkles and your `app` have the following structure:
87

98
```
10-
sprinkles
11-
── account
12-
│  └── src
13-
│  └── Database
14-
└── Models
15-
│  └── User.php
16-
├── site
17-
└── src
18-
│  └── Database
19-
└── Models
20-
│  └── User.php
9+
account
10+
── src
11+
└── Database
12+
└── Models
13+
└── User.php
14+
15+
app
16+
└── src
17+
└── Database
18+
└── Models
19+
└── User.php
2120
```
2221

23-
...then `User.php` in `site` would *not* override `User.php` in `account`. Rather, I'd have two different classes because both classes would have two different **namespace** : `\UserFrosting\Sprinkle\Account\Database\Models\User` and `\UserFrosting\Sprinkle\Site\Database\Models\User`.
22+
...then `User.php` in `app` would *not* override `User.php` in `account`. Rather, you'd have two different classes because both classes would have two different **namespace** : `\UserFrosting\Sprinkle\Account\Database\Models\User` and `\UserFrosting\Sprinkle\App\Database\Models\User`.
2423

2524
To actually override and replace the functionality of a class, we have two tools available:
2625

2726
## Class Inheritance
2827

29-
We could, for example, define our `User` class in the `site` Sprinkle to inherit from the `User` class in `account` using the `extends` keyword:
28+
We could, for example, define our `User` class in the `app` Sprinkle to inherit from the `User` class in `account` using the `extends` keyword:
3029

3130
**app/src/Database/Models/User.php** :
3231
```php
3332
<?php
3433

35-
namespace \UserFrosting\Sprinkle\MySprinkle\Database\Models;
34+
namespace \UserFrosting\Sprinkle\App\Database\Models;
3635

3736
class User extends \UserFrosting\Sprinkle\Account\Database\Models\User
3837
{
3938
// ...
4039
}
4140
```
4241

43-
Now, we can start using `\UserFrosting\Sprinkle\Site\Database\Models\User` to extend the functionality provided by the `User` class in the `Account` sprinkle.
42+
Now, we can start using `\UserFrosting\Sprinkle\App\Database\Models\User` to extend the functionality provided by the `User` class in the `Account` sprinkle.
4443

4544
## Dynamic Model Mapper
4645

47-
Of course, the limitations of object-oriented inheritance becomes clear when you want to change the behavior of the original class in other places where it has been used. For example, if I extended `Account\Database\Models\User` and redefined the `onLogin` method in my `Site\Database\Models\User` class, this would let me use `Site\Database\Models\User` going forward in any code I write in the `site` Sprinkle. However, it wouldn't affect references to `User` in the `account` Sprinkle - they would still be referring to the base class.
46+
Of course, the limitations of object-oriented inheritance becomes clear when you want to change the behavior of the original class in other places where it has been used. For example, if I extended `Account\Database\Models\User` and redefined the `onLogin` method in my `App\Database\Models\User` class, this would let me use `App\Database\Models\User` going forward in any code I write in the `app` Sprinkle. However, it wouldn't affect references to `User` in the `account` Sprinkle - they would still be referring to the base class.
4847

4948
To allow this sort of "_retroactive extendability_", the Dependency Injector can be used to resolves interface identifiers to specific class names at runtime [through Interface Binding and custom Autowiring](/dependency-injection/the-di-container#binding-interfaces).
5049

@@ -81,16 +80,15 @@ public function __invoke(Request $request, Response $response): Response
8180

8281
The following interface-model association are defined by default in the *Account* sprinkle :
8382

84-
| Interface | Model |
85-
| --------------------------------------------------------------------------------- | ------------------------------------------------------------- |
86-
| `UserFrosting\Sprinkle\Account\Database\Models\Interfaces\ActivityInterface` | `UserFrosting\Sprinkle\Account\Database\Models\Activity` |
87-
| `UserFrosting\Sprinkle\Account\Database\Models\Interfaces\GroupInterface` | `UserFrosting\Sprinkle\Account\Database\Models\Group` |
88-
| `UserFrosting\Sprinkle\Account\Database\Models\Interfaces\PasswordResetInterface` | `UserFrosting\Sprinkle\Account\Database\Models\PasswordReset` |
89-
| `UserFrosting\Sprinkle\Account\Database\Models\Interfaces\PermissionInterface` | `UserFrosting\Sprinkle\Account\Database\Models\Permission` |
90-
| `UserFrosting\Sprinkle\Account\Database\Models\Interfaces\PersistenceInterface` | `UserFrosting\Sprinkle\Account\Database\Models\Persistence` |
91-
| `UserFrosting\Sprinkle\Account\Database\Models\Interfaces\RoleInterface` | `UserFrosting\Sprinkle\Account\Database\Models\Role` |
92-
| `UserFrosting\Sprinkle\Account\Database\Models\Interfaces\UserInterface` | `UserFrosting\Sprinkle\Account\Database\Models\User` |
93-
| `UserFrosting\Sprinkle\Account\Database\Models\Interfaces\VerificationInterface` | `UserFrosting\Sprinkle\Account\Database\Models\Verification` |
83+
| Interface | Model |
84+
| -------------------------------------------------------------------------------------- | ------------------------------------------------------------------ |
85+
| `UserFrosting\Sprinkle\Account\Database\Models\Interfaces\ActivityInterface` | `UserFrosting\Sprinkle\Account\Database\Models\Activity` |
86+
| `UserFrosting\Sprinkle\Account\Database\Models\Interfaces\GroupInterface` | `UserFrosting\Sprinkle\Account\Database\Models\Group` |
87+
| `UserFrosting\Sprinkle\Account\Database\Models\Interfaces\PermissionInterface` | `UserFrosting\Sprinkle\Account\Database\Models\Permission` |
88+
| `UserFrosting\Sprinkle\Account\Database\Models\Interfaces\PersistenceInterface` | `UserFrosting\Sprinkle\Account\Database\Models\Persistence` |
89+
| `UserFrosting\Sprinkle\Account\Database\Models\Interfaces\RoleInterface` | `UserFrosting\Sprinkle\Account\Database\Models\Role` |
90+
| `UserFrosting\Sprinkle\Account\Database\Models\Interfaces\UserInterface` | `UserFrosting\Sprinkle\Account\Database\Models\User` |
91+
| `UserFrosting\Sprinkle\Account\Database\Models\Interfaces\UserVerificationInterface` | `UserFrosting\Sprinkle\Account\Database\Models\UserVerification` |
9492

9593
## Overwriting existing map
9694

app/pages/6.0/18.advanced/03.application-lifecycle/docs.md renamed to app/pages/6.0/18.advanced/02.application-lifecycle/docs.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
22
title: Application Lifecycle
33
description: Each Sprinkle may define a bootstrapper class that allows it to hook into various stages of the UserFrosting application lifecycle.
4-
wip: true
54
---
65

76
Every time UserFrosting is booted up to handle a request, it goes through its **application lifecycle**. This process includes loading the resources and [services](/services) in your Sprinkles, setting up the [Slim application](https://www.slimframework.com/docs/v3/objects/application.html), registering middleware, and setting up your [routes](/routes-and-controllers/front-controller).

0 commit comments

Comments
 (0)