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

RonasIT / laravel-entity-generator / 16143130415

08 Jul 2025 12:24PM UTC coverage: 100.0%. Remained the same
16143130415

Pull #164

github

web-flow
Merge 8c78f97eb into befbe86b8
Pull Request #164: feat: test with php8.4

905 of 905 relevant lines covered (100.0%)

11.41 hits per line

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

100.0
/src/Generators/ControllerGenerator.php
1
<?php
2

3
namespace RonasIT\Support\Generators;
4

5
use Illuminate\Contracts\Filesystem\FileNotFoundException;
6
use RonasIT\Support\Exceptions\ClassAlreadyExistsException;
7
use RonasIT\Support\Exceptions\ClassNotExistsException;
8
use RonasIT\Support\Events\SuccessCreateMessage;
9

10
class ControllerGenerator extends EntityGenerator
11
{
12
    public function generate(): void
13
    {
14
        if ($this->classExists('controllers', "{$this->model}Controller")) {
18✔
15
            $this->throwFailureException(
2✔
16
                ClassAlreadyExistsException::class,
2✔
17
                "Cannot create {$this->model}Controller cause {$this->model}Controller already exists.",
2✔
18
                "Remove {$this->model}Controller.",
2✔
19
            );
2✔
20
        }
21

22
        if (!$this->classExists('services', "{$this->model}Service")) {
16✔
23
            $this->throwFailureException(
2✔
24
                ClassNotExistsException::class,
2✔
25
                "Cannot create {$this->model}Controller cause {$this->model}Service does not exists.",
2✔
26
                "Create a {$this->model}Service by himself.",
2✔
27
            );
2✔
28
        }
29

30
        if (!$this->isStubExists('controller')) {
14✔
31
            return;
2✔
32
        }
33

34
        $controllerContent = $this->getControllerContent($this->model);
12✔
35

36
        $this->saveClass('controllers', "{$this->model}Controller", $controllerContent);
12✔
37

38
        $this->createRoutes();
12✔
39

40
        event(new SuccessCreateMessage("Created a new Controller: {$this->model}Controller"));
10✔
41
    }
42

43
    protected function getControllerContent($model): string
44
    {
45
        return $this->getStub('controller', [
12✔
46
            'entity' => $model,
12✔
47
            'requestsFolder' => $model,
12✔
48
            'namespace' => $this->getOrCreateNamespace('controllers'),
12✔
49
            'requestsNamespace' => $this->getOrCreateNamespace('requests'),
12✔
50
            'resourcesNamespace' => $this->getOrCreateNamespace('resources'),
12✔
51
            'servicesNamespace' => $this->getOrCreateNamespace('services'),
12✔
52
        ]);
12✔
53
    }
54

55
    protected function createRoutes(): void
56
    {
57
        $routesPath = base_path($this->paths['routes']);
12✔
58

59
        if (!file_exists($routesPath)) {
12✔
60
            $this->throwFailureException(
2✔
61
                FileNotFoundException::class,
2✔
62
                "Not found file with routes.",
2✔
63
                "Create a routes file on path: '{$routesPath}'.",
2✔
64
            );
2✔
65
        }
66

67
        if ($this->isStubExists('routes') && $this->isStubExists('use_routes')) {
10✔
68
            $this->addUseController($routesPath);
6✔
69
            $this->addRoutes($routesPath);
6✔
70
        }
71
    }
72

73
    protected function addRoutes($routesPath): string
74
    {
75
        $routesContent = $this->getStub('routes', [
6✔
76
            'entity' => $this->model,
6✔
77
            'entities' => $this->getTableName($this->model, '-'),
6✔
78
        ]);
6✔
79

80
        $routes = explode("\n", $routesContent);
6✔
81
        $routes = array_slice($routes, 1, array_key_last($routes) - 1);
6✔
82

83
        foreach ($routes as $route) {
6✔
84
            if (!empty($route)) {
6✔
85
                $route = trim($route);
6✔
86

87
                $createMessage = "Created a new Route: $route";
6✔
88

89
                event(new SuccessCreateMessage($createMessage));
6✔
90
            }
91
        }
92

93
        return file_put_contents($routesPath, "\n\n{$routesContent}", FILE_APPEND);
6✔
94
    }
95

96
    protected function addUseController(string $routesPath): void
97
    {
98
        $routesFileContent = file_get_contents($routesPath);
6✔
99

100
        $stub = $this->getStub('use_routes', [
6✔
101
            'namespace' => $this->getOrCreateNamespace('controllers'),
6✔
102
            'entity' => $this->model
6✔
103
        ]);
6✔
104

105
        $routesFileContent = preg_replace('/\<\?php[^A-Za-z]*/', "<?php\n\n{$stub}", $routesFileContent);
6✔
106

107
        file_put_contents($routesPath, $routesFileContent);
6✔
108
    }
109
}
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

© 2025 Coveralls, Inc