-
Notifications
You must be signed in to change notification settings - Fork 1
/
ApiAncestryControllerTrait.php
54 lines (45 loc) · 1.39 KB
/
ApiAncestryControllerTrait.php
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
<?php
namespace Esensi\Core\Traits;
use Esensi\Contracts\PackagedInterface;
use Esensi\Contracts\RepositoryInjectedInterface;
/**
* Trait implementation of API ancestry controller interface.
*
*/
trait ApiAncestryControllerTrait
{
/**
* Get the API ancestor controller class
* of the current controller class.
*
* @return \Esensi\Core\Http\Apis\Api
*/
public function api()
{
// Make a copy of the parent class
$class = get_parent_class();
$parent = app($class);
// Copy over the packaged properties
if ($this instanceof PackagedInterface) {
$parent->setUI( $this->getUI() );
$parent->setPackage( $this->getPackage() );
$parent->setNamespacing( $this->getNamespacing() );
}
// Copy over the injected repositories
if ($this instanceof RepositoryInjectedInterface) {
foreach ($this->repositories as $name => $repository) {
$parent->setRepository($repository, $name);
}
}
// Return first ApiController ancestor found
if (str_contains($class, 'Api')) {
return $parent;
}
// Recursively look up the parent class
if (method_exists($parent, 'api')) {
return $parent->api();
}
// Return the parent class found already
return $parent;
}
}