Skip to content

Commit 32bde9d

Browse files
committed
Adding DevCommand
1 parent 4f6b9af commit 32bde9d

File tree

3 files changed

+152
-2
lines changed

3 files changed

+152
-2
lines changed

src/Commands/DevCommand.php

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
<?php
2+
3+
namespace Binaryk\LaravelRestify\Commands;
4+
5+
use Faker\Generator as Faker;
6+
use Illuminate\Console\Command;
7+
use Illuminate\Console\ConfirmableTrait;
8+
use Illuminate\Database\ConnectionResolverInterface as Resolver;
9+
use Illuminate\Support\Str;
10+
use Symfony\Component\Process\Process;
11+
12+
class DevCommand extends Command
13+
{
14+
use ConfirmableTrait;
15+
16+
protected $signature = 'restify:dev {--path= : The path to the root local directory.}
17+
{--git : Use the latest vcs git repository}
18+
';
19+
20+
protected $description = 'Add laravel-restify from a local directory.';
21+
22+
/** * @var Faker */
23+
private $faker;
24+
25+
public function __construct(Resolver $resolver, Faker $faker)
26+
{
27+
parent::__construct();
28+
$this->resolver = $resolver;
29+
$this->faker = $faker;
30+
}
31+
32+
public function handle()
33+
{
34+
if (!$this->confirmToProceed()) {
35+
return true;
36+
}
37+
38+
$this->addRepositoryToRootComposer();
39+
40+
$this->info('Added local path to repositories.');
41+
42+
$this->addPackageToRootComposer();
43+
44+
$this->info('Package added to the root composer as *.');
45+
46+
$this->composerUpdate();
47+
48+
$this->info('Composer updated.');
49+
50+
return 0;
51+
}
52+
53+
protected function addPackageToRootComposer()
54+
{
55+
$composer = json_decode(file_get_contents(base_path('composer.json')), true);
56+
57+
$composer['require']['binaryk/laravel-restify'] = $this->resolveVersion();
58+
59+
file_put_contents(
60+
base_path('composer.json'),
61+
json_encode($composer, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
62+
);
63+
}
64+
65+
protected function addRepositoryToRootComposer()
66+
{
67+
$composer = json_decode(file_get_contents(base_path('composer.json')), true);
68+
69+
if (array_key_exists('repositories', $composer)) {
70+
$composer['repositories'] = collect($composer['repositories'])->filter(function ($repository) {
71+
if (!array_key_exists('url', $repository)) {
72+
return true;
73+
}
74+
75+
$pathIsAlreadyInRepositories = Str::contains(
76+
$repository['url'],
77+
$this->resolvePath()
78+
);
79+
80+
if ($pathIsAlreadyInRepositories) {
81+
return false;
82+
}
83+
84+
return ! Str::contains($repository['url'], 'laravel-restify');
85+
})->values()->toArray();
86+
}
87+
88+
if ($this->option('git')) {
89+
$composer['repositories'][] = [
90+
'type' => 'vcs',
91+
'url' => $this->option('path') ?: '[email protected]:BinarCode/laravel-restify.git'
92+
];
93+
} else {
94+
$composer['repositories'][] = [
95+
'type' => 'path',
96+
'url' => $this->option('path') ?: '../../binarcode/laravel-restify'
97+
];
98+
}
99+
100+
101+
file_put_contents(
102+
base_path('composer.json'),
103+
json_encode($composer, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
104+
);
105+
}
106+
107+
108+
protected function composerUpdate()
109+
{
110+
$this->executeCommand('composer update binaryk/laravel-restify', getcwd());
111+
}
112+
113+
protected function executeCommand($command, $path)
114+
{
115+
$process = (Process::fromShellCommandline($command, $path))->setTimeout(null);
116+
117+
if ('\\' !== DIRECTORY_SEPARATOR && file_exists('/dev/tty') && is_readable('/dev/tty')) {
118+
$process->setTty(true);
119+
}
120+
121+
$process->run(function ($type, $line) {
122+
$this->output->write($line);
123+
});
124+
}
125+
126+
public function resolveVersion(): string
127+
{
128+
return $this->option('git')
129+
? '3.x-dev'
130+
: '*';
131+
}
132+
133+
public function resolvePath()
134+
{
135+
return $this->option('path') ?: $this->resolveDefaultPath();
136+
}
137+
138+
private function resolveDefaultPath()
139+
{
140+
return $this->option('git')
141+
? '[email protected]:BinarCode/laravel-restify.git'
142+
: '../../binarcode/laravel-restify';
143+
}
144+
}

src/Http/Controllers/ProfileController.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,16 @@ public function guessRepository(RestifyRequest $request): ?Repository
4040
{
4141
$repository = $request->repository('users');
4242

43-
return null;
44-
4543
if (! $repository) {
4644
return null;
4745
}
4846

47+
if (method_exists($repository, 'canUseForProfile')) {
48+
if (! call_user_func($repository, 'canUseForProfile', $request)) {
49+
return null;
50+
}
51+
}
52+
4953
$user = tap(RepositorySearchService::instance()->search(
5054
$request, $repository
5155
), function ($query) use ($request, $repository) {

src/LaravelRestifyServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Binaryk\LaravelRestify\Commands\ActionCommand;
66
use Binaryk\LaravelRestify\Commands\BaseRepositoryCommand;
77
use Binaryk\LaravelRestify\Commands\CheckPassport;
8+
use Binaryk\LaravelRestify\Commands\DevCommand;
89
use Binaryk\LaravelRestify\Commands\PolicyCommand;
910
use Binaryk\LaravelRestify\Commands\Refresh;
1011
use Binaryk\LaravelRestify\Commands\RepositoryCommand;
@@ -57,6 +58,7 @@ public function register()
5758
$this->commands([
5859
RepositoryCommand::class,
5960
ActionCommand::class,
61+
DevCommand::class,
6062
]);
6163
}
6264

0 commit comments

Comments
 (0)