Issues (197)

src/Commands/BindingsCommand.php (1 issue)

1
<?php
2
namespace Salah3id\Domains\Commands;
3
4
use File;
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Collection;
7
use Salah3id\Domains\Repository\Generators\BindingsGenerator;
8
use Salah3id\Domains\Repository\Generators\FileAlreadyExistsException;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputOption;
11
use Salah3id\Domains\Traits\DomainCommandTrait;
12
13
/**
14
 * Class BindingsCommand
15
 * @package Salah3id\Domains\Commands
16
 * @author Anderson Andrade <[email protected]>
17
 */
18
class BindingsCommand extends Command
19
{
20
21
    use DomainCommandTrait;
22
23
    /**
24
     * The name of command.
25
     *
26
     * @var string
27
     */
28
    protected $name = 'domain:repo-bindings';
29
30
    /**
31
     * The description of command.
32
     *
33
     * @var string
34
     */
35
    protected $description = 'Add repository bindings to service provider.';
36
37
    /**
38
     * The type of class being generated.
39
     *
40
     * @var string
41
     */
42
    protected $type = 'Bindings';
43
44
    /**
45
     * Execute the command.
46
     *
47
     * @see fire()
48
     * @return void
49
     */
50
    public function handle(){
51
        $this->laravel->call([$this, 'fire'], func_get_args());
52
    }
53
54
    /**
55
     * Execute the command.
56
     *
57
     * @return void
58
     */
59
    public function fire()
60
    {
61
        if($this->argument('domain') && $this->argument('domain-path')) {
62
            $domain = $this->argument('domain');
63
            $domainPath = $this->argument('domain-path');
64
        } else {
65
            $domain = $this->getDomainNameForRepo();
66
            $domainPath = $this->laravel['domains']->getDomainPath($domain);
67
        }
68
        
69
        try {
70
            $bindingGenerator = new BindingsGenerator([
71
                'name' => $this->argument('name'),
72
                'force' => $this->option('force'),
73
            ],$domain,$domainPath);
74
            // generate repository service provider
75
            if (!file_exists($bindingGenerator->getPath())) {
76
                $this->call('domain:make-provider', [
77
                    'name' => $bindingGenerator->getConfigGeneratorClassPath($bindingGenerator->getPathConfigNode()),
78
                    'domain' => $domain
79
                ]);
80
                // placeholder to mark the place in file where to prepend repository bindings
81
                $provider = File::get($bindingGenerator->getPath());
82
                File::put($bindingGenerator->getPath(), vsprintf(str_replace('//', '%s', $provider), [
83
                    '//',
84
                    $bindingGenerator->bindPlaceholder
85
                ]));
86
            }
87
            $bindingGenerator->run();
88
            $this->info($this->type . ' created successfully.');
89
        } catch (FileAlreadyExistsException $e) {
90
            $this->error($this->type . ' already exists!');
91
92
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type void.
Loading history...
93
        }
94
    }
95
96
97
    /**
98
     * The array of command arguments.
99
     *
100
     * @return array
101
     */
102
    public function getArguments()
103
    {
104
        return [
105
            [
106
                'name',
107
                InputArgument::REQUIRED,
108
                'The name of model for which the controller is being generated.',
109
                null
110
            ],
111
            ['domain', InputArgument::OPTIONAL, 'The name of domain will be used.'],
112
            ['domain-path', InputArgument::OPTIONAL, 'The path of domain will be used.'],
113
        ];
114
    }
115
116
117
    /**
118
     * The array of command options.
119
     *
120
     * @return array
121
     */
122
    public function getOptions()
123
    {
124
        return [
125
            [
126
                'force',
127
                'f',
128
                InputOption::VALUE_NONE,
129
                'Force the creation if file already exists.',
130
                null
131
            ],
132
        ];
133
    }
134
}
135