Passed
Pull Request — 2.x (#1446)
by Harings
09:04
created

GeneratePackageCommand::writeComposerJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 13
rs 9.9666
1
<?php
2
3
namespace A17\Twill\Commands;
4
5
use A17\Twill\Commands\Traits\HandlesStubs;
6
use Illuminate\Console\Command;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, A17\Twill\Commands\Command. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
use Illuminate\Support\Facades\File;
8
use Illuminate\Support\Str;
9
10
class GeneratePackageCommand extends Command
11
{
12
    use HandlesStubs;
13
14
    protected $signature = 'twill:make:package';
15
16
    protected $description = 'Make a new twill package';
17
    /**
18
     * @var string
19
     */
20
    private $packageName;
21
22
    /**
23
     * @var string
24
     */
25
    private $packageVendor;
26
27
    /**
28
     * @var string
29
     */
30
    private $homepage;
31
32
    /**
33
     * @var string
34
     */
35
    private $licence;
36
37
    /**
38
     * @var string
39
     */
40
    private $psr4Base;
41
42
    /**
43
     * @var string
44
     */
45
    private $targetDirectory;
46
47
    public function handle(): void
48
    {
49
        $this->packageName = $this->ask('What\'s the package name', 'twill-extension');
50
        $this->packageVendor = $this->ask('What\'s the package vendor', 'area17');
51
        $this->homepage = $this->ask(
52
            'The package homepage',
53
            'https://github.com/' . $this->packageVendor . '/' . $this->packageName
54
        );
55
        $this->licence = $this->askWithCompletion('What is the licence', ['MIT', 'Apache-2.0'], 'MIT');
56
        $this->psr4Base = $this->ask('The psr4 base name', Str::studly($this->packageName));
57
        $this->targetDirectory = $this->ask(
58
            'Where should we put the package',
59
            base_path('packages') . '/' . $this->packageName
60
        );
61
62
        $this->generatePackage();
63
    }
64
65
    protected function generatePackage(): void
66
    {
67
        File::ensureDirectoryExists($this->targetDirectory);
68
69
        $this->writeComposerJson();
70
        $this->writeServiceProvider();
71
        $this->displayMessage();
72
    }
73
74
    protected function writeComposerJson(): void
75
    {
76
        $stub = file_get_contents(__DIR__ . '/stubs/package/package-composer-json.stub');
77
78
        $stub = $this->replaceVariables([
79
            'name' => $this->packageVendor . '/' . $this->packageName,
80
            'homepage' => $this->homepage,
81
            'licence' => $this->licence,
82
            'namespace' => $this->psr4Base,
83
            'providerName' => $this->getProviderName(),
84
        ], $stub);
85
86
        file_put_contents($this->targetDirectory . '/composer.json', $stub);
87
    }
88
89
    protected function writeServiceProvider(): void
90
    {
91
        File::ensureDirectoryExists($this->targetDirectory . '/src');
92
93
        $stub = file_get_contents(__DIR__ . '/stubs/package/package-service-provider.stub');
94
        $providerName = $this->getProviderName();
95
96
        $stub = $this->replaceVariables([
97
            'namespace' => $this->psr4Base,
98
            'providerName' => $providerName,
99
        ], $stub);
100
101
        file_put_contents($this->targetDirectory . "/src/{$providerName}ServiceProvider.php", $stub);
102
    }
103
104
    protected function displayMessage(): void
105
    {
106
        $stub = file_get_contents(__DIR__ . '/stubs/package/instructions.stub');
107
108
        $path = $this->targetDirectory;
109
110
        if (Str::startsWith($path, base_path())) {
111
            $path = '.' . Str::replaceFirst(base_path(), '', $path);
112
        }
113
114
        $stub = $this->replaceVariables([
115
            'path' => $path,
116
            'vendor' => $this->packageVendor,
117
            'name' => $this->packageName,
118
            'namespace' => $this->getProviderName(),
119
        ], $stub);
120
121
        $this->line($stub);
122
    }
123
124
    protected function getProviderName()
125
    {
126
        return Str::afterLast($this->psr4Base, '\\');
127
    }
128
}
129