-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel_Service_Provider.php
More file actions
88 lines (76 loc) · 1.85 KB
/
Copy pathModel_Service_Provider.php
File metadata and controls
88 lines (76 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
/**
* Model_Register class file.
*
* @package Mantle
*/
namespace Mantle\Database;
use Mantle\Database\Model\Model;
use Mantle\Database\Model\Relations\Relation;
use Mantle\Framework\Manifest\Model_Manifest;
use Mantle\Support\Attributes\Action;
use Mantle\Support\Service_Provider;
/**
* Model Service Provider
*/
class Model_Service_Provider extends Service_Provider {
/**
* Models to register for the application.
*
* @var string[]
*/
protected $models = [];
/**
* Register the service provider.
*/
public function register(): void {}
/**
* Bootstrap the service provider.
*/
public function boot(): void {
Model::set_event_dispatcher( $this->app['events'] );
// Allow the configuration to disable discovery.
if ( $this->app['config']->get( 'models.disable_discovery', false ) ) {
return;
}
$configuration = (array) $this->app['config']->get( 'models.register', [] );
// Allows models to always be booted on each request to register whatever side-effects they desire.
$this->set_models_to_register(
array_merge(
$configuration,
$this->app[ Model_Manifest::class ]->models()
)
);
if ( empty( $this->models ) ) {
return;
}
foreach ( $this->models as $model ) {
if ( class_exists( $model ) ) {
$model::boot_if_not_booted();
}
}
}
/**
* Set the models to register.
*
* @param string[] $models Models to register.
*/
public function set_models_to_register( array $models ): void {
$this->models = array_unique( $models );
}
/**
* Register the internal taxonomy for post <--> post relationships.
*/
#[Action( 'init', 5 )]
public static function register_internal_taxonomy(): void {
register_taxonomy(
Relation::RELATION_TAXONOMY,
array_keys( get_post_types() ),
[
'public' => false,
'rewrite' => false,
'show_in_rest' => false,
]
);
}
}