1 | <?php |
||
2 | |||
3 | namespace Blok\LaravelPackageGenerator\Commands; |
||
4 | |||
5 | use Blok\LaravelPackageGenerator\Commands\Traits\ChangesComposerJson; |
||
6 | use Blok\LaravelPackageGenerator\Commands\Traits\InteractsWithComposer; |
||
7 | use Blok\LaravelPackageGenerator\Commands\Traits\InteractsWithGit; |
||
8 | use Blok\LaravelPackageGenerator\Commands\Traits\InteractsWithUser; |
||
9 | use Exception; |
||
10 | use Illuminate\Console\Command; |
||
11 | use Illuminate\Support\Str; |
||
12 | |||
13 | class PackageClone extends Command |
||
14 | { |
||
15 | use InteractsWithComposer; |
||
16 | use InteractsWithGit; |
||
17 | use InteractsWithUser; |
||
18 | use ChangesComposerJson; |
||
19 | |||
20 | /** |
||
21 | * The name and signature of the console command. |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $signature = 'package:clone |
||
26 | {url : The url of the Github repository} |
||
27 | {vendor? : The vendor part of the namespace} |
||
28 | {package? : The name of package for the namespace} |
||
29 | {--b|branch=master : The branch to clone} |
||
30 | {--i|interactive : Interactive mode}'; |
||
31 | |||
32 | /** |
||
33 | * The console command description. |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $description = 'Clone an existing package.'; |
||
38 | |||
39 | /** |
||
40 | * Execute the console command. |
||
41 | * |
||
42 | * @return mixed |
||
43 | */ |
||
44 | public function handle() |
||
45 | { |
||
46 | $url = $this->argument('url'); |
||
47 | |||
48 | try { |
||
49 | list($vendorFolderName, $packageFolderName) = $this->getVendorAndFolderName($url); |
||
50 | } catch (Exception $e) { |
||
51 | $this->error($e->getMessage()); |
||
52 | |||
53 | return -1; |
||
54 | } |
||
55 | |||
56 | $vendor = $this->getVendor() ?: Str::title($vendorFolderName); |
||
57 | $package = $this->getPackage() ?: Str::studly($packageFolderName); |
||
58 | |||
59 | $relPackagePath = "workbench/$vendorFolderName/$packageFolderName"; |
||
60 | $packagePath = base_path($relPackagePath); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
61 | |||
62 | try { |
||
63 | $this->cloneRepo($url, $packagePath, $this->option('branch')); |
||
64 | $this->registerPackage($vendor, $package, $relPackagePath); |
||
65 | $this->composerUpdatePackage($vendorFolderName, $packageFolderName); |
||
66 | $this->composerDumpAutoload(); |
||
67 | |||
68 | $this->info('Finished.'); |
||
69 | } catch (Exception $e) { |
||
70 | $this->error($e->getMessage()); |
||
71 | |||
72 | return -1; |
||
73 | } |
||
74 | |||
75 | return 0; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Get vendor and package folder name. |
||
80 | * |
||
81 | * @param $url |
||
82 | * |
||
83 | * @return array |
||
84 | */ |
||
85 | protected function getVendorAndFolderName($url) |
||
86 | { |
||
87 | if (Str::contains($url, '@')) { |
||
88 | $vendorAndPackage = explode(':', $url); |
||
89 | |||
90 | $vendorAndPackage = explode('/', $vendorAndPackage[1]); |
||
91 | |||
92 | return [ |
||
93 | $vendorAndPackage[0], |
||
94 | Str::replaceLast('.git', '', $vendorAndPackage[1]), |
||
95 | ]; |
||
96 | } |
||
97 | |||
98 | $urlParts = explode('/', $url); |
||
99 | |||
100 | return [$urlParts[3], $urlParts[4]]; |
||
101 | } |
||
102 | } |
||
103 |