Issues (26)

src/Skeleton/MakeSkeletonCommand.php (8 issues)

1
<?php
2
3
namespace CleaniqueCoders\Console\Skeleton;
4
5
use CleaniqueCoders\Console\Commander;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
class MakeSkeletonCommand extends Commander
11
{
12
    /**
13
     * Configure the command options.
14
     */
15
    protected function configure()
16
    {
17
        $this
18
            ->setName('skeleton')
19
            ->setDescription('Create a new Laravel Package')
20
            ->addArgument('vendor', InputArgument::REQUIRED)
21
            ->addArgument('package', InputArgument::REQUIRED)
22
            ->addArgument('path', InputArgument::OPTIONAL);
23
    }
24
25
    /**
26
     * Execute the command.
27
     */
28
    protected function execute(InputInterface $input, OutputInterface $output)
29
    {
30
        $this->vendor            = $vendor            = $input->getArgument('vendor');
0 ignored issues
show
Bug Best Practice introduced by
The property vendor does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
31
        $this->package           = $package           = $input->getArgument('package');
0 ignored issues
show
Bug Best Practice introduced by
The property package does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
32
        $this->path              = $path              = $input->getArgument('path') ? $input->getArgument('path') : getcwd();
0 ignored issues
show
Bug Best Practice introduced by
The property path does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
33
        $this->path              = $path              = ('.' == $path) ? getcwd() : $path;
34
        $this->directory         = $directory         = $path . DIRECTORY_SEPARATOR . $this->getDirectoryName($vendor, $package);
0 ignored issues
show
Bug Best Practice introduced by
The property directory does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
Are you sure $path of type null|string|string[] 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

34
        $this->directory         = $directory         = /** @scrutinizer ignore-type */ $path . DIRECTORY_SEPARATOR . $this->getDirectoryName($vendor, $package);
Loading history...
35
        $this->package_path      = $this->directory . DIRECTORY_SEPARATOR;
0 ignored issues
show
Bug Best Practice introduced by
The property package_path does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
36
        $this->package_src_path  = $this->package_path . 'src' . DIRECTORY_SEPARATOR;
0 ignored issues
show
Bug Best Practice introduced by
The property package_src_path does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
37
        $this->package_test_path = $this->package_path . 'tests' . DIRECTORY_SEPARATOR;
0 ignored issues
show
Bug Best Practice introduced by
The property package_test_path does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
38
39
        $output->writeln('<info>Creating your Laravel Package Skeleton...</info>');
40
41
        /* 0. Verify Directory */
42
        $this->verifyPackageDoesntExist($directory);
43
44
        /* 1. Copy Stub */
45
        $this->copyStubs();
46
47
        /* 2. Update Package Name and Autoload */
48
        $this->updatePackageNameAndAutoload();
49
50
        /* 3. Update Service Provider  */
51
        $this->updateServiceProvider();
52
53
        /* 4. Update Facade */
54
        $this->updateFacade();
55
56
        /* 5. Update TestCase.php */
57
        $this->updateTestCase();
58
59
        /* 6. Update README.md */
60
        $this->updateReadme();
61
62
        /* 7. Initialise Git, install composer dependencies */
63
        $this->initRepository();
64
65
        $output->writeln('<info>Your package directory name: ' . $directory . '</info>');
66
        $output->writeln('<comment>Your Laravel Package Skeleton is ready!</comment>');
67
68
        return 0;
69
    }
70
71
    private function replace($these, $withThese, $file)
72
    {
73
        $this->filesystem->put(
74
            $file,
75
            str_replace(
76
                $these,
77
                $withThese,
78
                $this->filesystem->get($file)
79
            )
80
        );
81
    }
82
83
    /** 1. Copy Stub */
84
    private function copyStubs()
85
    {
86
        $stubsDir = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'stubs';
87
        $this->filesystem->copyDirectory($stubsDir, $this->directory);
88
    }
89
90
    /** 2. Update Package Name and Autoload */
91
    private function updatePackageNameAndAutoload()
92
    {
93
        $composerJson = $this->package_path . 'composer.json';
94
        $this->replace(
95
            [
96
                'DummyPackageName',
97
                'DummyAutoLoad',
98
                'PackagerDummyServiceProvider',
99
            ],
100
            [
101
                $this->getQualifiedPackageName($this->vendor, $this->package),
102
                $this->getAutoLoadName($this->vendor, $this->package),
103
                $this->getQualifiedClassName($this->package) . 'ServiceProvider',
104
            ],
105
            $composerJson
106
        );
107
    }
108
109
    /** 3. Update Service Provider  */
110
    private function updateServiceProvider()
111
    {
112
        $dummyProvider   = $this->package_src_path . 'PackagerDummyServiceProvider.php';
113
        $packageProvider = $this->package_src_path . $this->getQualifiedClassName($this->package) . 'ServiceProvider.php';
114
        $this->filesystem->move($dummyProvider, $packageProvider, true);
115
        $this->replace(
116
            [
117
                'DummyNamespace',
118
                'DummyClassName',
119
            ],
120
            [
121
                $this->getQualifiedNamespace($this->vendor, $this->package),
122
                $this->getQualifiedClassName($this->package),
123
            ],
124
            $packageProvider
125
        );
126
    }
127
128
    /** 4. Update Facade  */
129
    private function updateFacade()
130
    {
131
        $dummyFacade = $this->package_src_path . 'PackagerDummyFacade.php';
132
        $facade      = $this->package_src_path . $this->getQualifiedClassName($this->package) . 'Facade.php';
133
134
        $this->filesystem->move($dummyFacade, $facade, true);
135
        $this->replace(
136
            [
137
                'PackageName',
138
                'DummyNamespace',
139
                'DummyClassName',
140
                'FacadeName',
141
            ],
142
            [
143
                $this->getPackageName($this->package),
144
                $this->getQualifiedNamespace($this->vendor, $this->package),
145
                $this->getQualifiedClassName($this->package),
146
                $this->getQualifiedFacadeName($this->package),
147
            ],
148
            $facade
149
        );
150
    }
151
152
    /** 5. Update TestCase.php */
153
    private function updateTestCase()
154
    {
155
        $files = [
156
            $this->package_test_path . 'TestCase.php',
157
            $this->package_test_path . 'Traits' . DIRECTORY_SEPARATOR . 'SeedTrait.php',
158
            $this->package_test_path . 'Traits' . DIRECTORY_SEPARATOR . 'TestCaseTrait.php',
159
            $this->package_test_path . 'Traits' . DIRECTORY_SEPARATOR . 'UserTrait.php',
160
        ];
161
162
        foreach ($files as $file) {
163
            $this->replace(
164
                [
165
                    'DummyNamespace',
166
                    'DummyClassName',
167
                ],
168
                [
169
                    $this->getQualifiedNamespace($this->vendor, $this->package),
170
                    $this->getQualifiedClassName($this->package),
171
                ],
172
                $file
173
            );
174
        }
175
    }
176
177
    /** 6. Update README.md */
178
    private function updateReadme()
179
    {
180
        $this->replace(
181
            [
182
                'DummyPackageName',
183
                'PackageName',
184
                'DummyNamespace',
185
                'DummyClassName',
186
                'FacadeName',
187
            ],
188
            [
189
                $this->getQualifiedPackageName($this->vendor, $this->package),
190
                $this->getPackageName($this->package),
191
                $this->getQualifiedNamespace($this->vendor, $this->package),
192
                $this->getQualifiedClassName($this->package),
193
                $this->getQualifiedFacadeName($this->package),
194
            ],
195
            $this->package_path . 'README.md'
196
        );
197
    }
198
199
    /** 7. Initialise Git, install composer dependencies */
200
    private function initRepository()
201
    {
202
        chdir($this->directory);
203
        $this->gitInit();
204
        $this->composerUpdate();
205
        $this->gitCommitUpdateDependecies();
206
    }
207
}
208