Issues (413)

app/Generators/BindingsGenerator.php (4 issues)

1
<?php
2
3
namespace Yeelight\Generators;
4
5
/**
6
 * Class BindingsGenerator
7
 *
8
 * @category Yeelight
9
 *
10
 * @package Yeelight\Generators
11
 *
12
 * @author Sheldon Lee <[email protected]>
13
 *
14
 * @license https://opensource.org/licenses/MIT MIT
15
 *
16
 * @link https://www.yeelight.com
17
 */
18
class BindingsGenerator extends Generator
19
{
20
    /**
21
     * The placeholder for repository bindings.
22
     *
23
     * @var string
24
     */
25
    public $bindPlaceholder = '//:end-bindings:';
26
    /**
27
     * Get stub name.
28
     *
29
     * @var string
30
     */
31
    protected $stub = 'bindings/bindings';
32
33
    /**
34
     * Run
35
     *
36
     * @return int|void
37
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
38
     */
39
    public function run()
40
    {
41
42
        // Add entity repository binding to the repository service provider
43
        $provider = \File::get($this->getPath());
44
        $repositoryInterface = '\\'.$this->getRepository().'::class';
45
        $repositoryEloquent = '\\'.$this->getEloquentRepository().'::class';
46
        \File::put($this->getPath(), str_replace($this->bindPlaceholder, "\$this->app->bind({$repositoryInterface}, $repositoryEloquent);".PHP_EOL.'        '.$this->bindPlaceholder, $provider));
47
    }
48
49
    /**
50
     * Get destination path for generated file.
51
     *
52
     * @return string
53
     */
54
    public function getPath()
55
    {
56
        return $this->getBasePath() .
57
            '/Providers/' .
58
            parent::getConfigGeneratorClassPath(
0 ignored issues
show
Are you sure parent::getConfigGenerat...PathConfigNode(), true) of type Illuminate\Config\Repository|mixed can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
            /** @scrutinizer ignore-type */ parent::getConfigGeneratorClassPath(
Loading history...
59
                $this->getPathConfigNode(),
60
                true
61
            ) .
62
            '.php';
63
    }
64
65
    /**
66
     * Get base path of destination file.
67
     *
68
     * @return string
69
     */
70
    public function getBasePath()
71
    {
72
        return config('repository.generator.basePath', app_path());
73
    }
74
75
    /**
76
     * Get generator path config node.
77
     *
78
     * @return string
79
     */
80
    public function getPathConfigNode()
81
    {
82
        return 'provider';
83
    }
84
85
    /**
86
     * Gets repository full class name.
87
     *
88
     * @return string
89
     */
90
    public function getRepository()
91
    {
92
        $repositoryGenerator = new RepositoryInterfaceGenerator(
93
            [
94
                'name' => $this->name,
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on Yeelight\Generators\BindingsGenerator. Since you implemented __get, consider adding a @property annotation.
Loading history...
95
            ]
96
        );
97
98
        $repository = $repositoryGenerator->getRootNamespace().'\\'.$repositoryGenerator->getName();
99
100
        return str_replace([
101
            '\\',
102
            '/',
103
        ], '\\', $repository).'Repository';
104
    }
105
106
    /**
107
     * Gets eloquent repository full class name.
108
     *
109
     * @return string
110
     */
111
    public function getEloquentRepository()
112
    {
113
        $repositoryGenerator = new RepositoryEloquentGenerator(
114
            [
115
                'name' => $this->name,
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on Yeelight\Generators\BindingsGenerator. Since you implemented __get, consider adding a @property annotation.
Loading history...
116
            ]
117
        );
118
119
        $repository = $repositoryGenerator->getRootNamespace().'\\'.$repositoryGenerator->getName();
120
121
        return str_replace([
122
            '\\',
123
            '/',
124
        ], '\\', $repository).'RepositoryEloquent';
125
    }
126
127
    /**
128
     * Get root namespace.
129
     *
130
     * @return string
131
     */
132
    public function getRootNamespace()
133
    {
134
        return parent::getRootNamespace().parent::getConfigGeneratorClassPath($this->getPathConfigNode());
0 ignored issues
show
Are you sure parent::getConfigGenerat...s->getPathConfigNode()) of type Illuminate\Config\Repository|mixed can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

134
        return parent::getRootNamespace()./** @scrutinizer ignore-type */ parent::getConfigGeneratorClassPath($this->getPathConfigNode());
Loading history...
135
    }
136
137
    /**
138
     * Get array replacements.
139
     *
140
     * @return array
141
     */
142
    public function getReplacements()
143
    {
144
        return array_merge(
145
            parent::getReplacements(),
146
            [
147
                'repository' => $this->getRepository(),
148
                'eloquent' => $this->getEloquentRepository(),
149
                'placeholder' => $this->bindPlaceholder,
150
            ]
151
        );
152
    }
153
}
154