• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

conedevelopment / root / 15084089635

17 May 2025 10:00AM UTC coverage: 77.93% (+0.04%) from 77.891%
15084089635

push

github

web-flow
Modernize back-end.yml (#240)

3291 of 4223 relevant lines covered (77.93%)

36.04 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

59.26
/src/Resources/Resources.php
1
<?php
2

3
namespace Cone\Root\Resources;
4

5
use Cone\Root\Exceptions\ResourceResolutionException;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Arr;
9
use Illuminate\Support\Collection;
10
use Illuminate\Support\Facades\App;
11
use Illuminate\Support\Str;
12
use ReflectionClass;
13
use Symfony\Component\Finder\Finder;
14

15
class Resources extends Collection
16
{
17
    /**
18
     * Discover the resources in the given paths.
19
     */
20
    public function discoverIn(string|array $paths): void
198✔
21
    {
22
        foreach ((array) $paths as $path) {
198✔
23
            if (is_dir($path)) {
198✔
24
                $this->discover($path);
175✔
25
            }
26
        }
27
    }
28

29
    /**
30
     * Discover and register the resources.
31
     */
32
    protected function discover(string $path): void
175✔
33
    {
34
        $namespace = App::getNamespace();
175✔
35

36
        foreach ((new Finder)->in($path)->files() as $resource) {
175✔
37
            $resource = str_replace(
×
38
                ['/', '.php'],
×
39
                ['\\', ''],
×
40
                Str::after($resource->getPathname(), App::path().DIRECTORY_SEPARATOR)
×
41
            );
×
42

43
            $resource = $namespace.$resource;
×
44

45
            if (is_subclass_of($resource, Resource::class) && (new ReflectionClass($resource))->isInstantiable()) {
×
46
                $this->register(new $resource);
×
47
            }
48
        }
49
    }
50

51
    /**
52
     * Register the given resource into the collection.
53
     */
54
    public function register(array|Resource $resources): void
198✔
55
    {
56
        foreach (Arr::wrap($resources) as $resource) {
198✔
57
            $this->put($resource->getKey(), $resource);
198✔
58
        }
59
    }
60

61
    /**
62
     * Get the registered resource for the given model.
63
     */
64
    public function forModel(string|Model $model): ?Resource
1✔
65
    {
66
        $model = is_string($model) ? $model : $model::class;
1✔
67

68
        return $this->first(static fn (Resource $resource): bool => $resource->getModel() === $model);
1✔
69
    }
70

71
    /**
72
     * Resolve the resource registered with the given key.
73
     */
74
    public function resolve(string $key): Resource
36✔
75
    {
76
        if (! $this->has($key)) {
36✔
77
            throw new ResourceResolutionException;
×
78
        }
79

80
        return $this->get($key);
36✔
81
    }
82

83
    /**
84
     * Filter the authorized resources.
85
     */
86
    public function authorized(Request $request): static
×
87
    {
88
        return $this->filter->authorized($request)->values();
×
89
    }
90
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc