Skip to content

Commit 21fb0a3

Browse files
authored
Merge pull request #824 from jrbarnard/feature/links-with-parked
Add 'parked' command to show all sites which have been parked
2 parents beda122 + b16559a commit 21fb0a3

File tree

3 files changed

+188
-3
lines changed

3 files changed

+188
-3
lines changed

cli/Valet/Site.php

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,33 @@ function links()
109109

110110
$certs = $this->getCertificates($certsPath);
111111

112-
return $this->getLinks($this->sitesPath(), $certs);
112+
return $this->getSites($this->sitesPath(), $certs);
113+
}
114+
115+
/**
116+
* Pretty print out all parked links in Valet
117+
*
118+
* @return \Illuminate\Support\Collection
119+
*/
120+
function parked()
121+
{
122+
$certsPath = VALET_HOME_PATH.'/Certificates';
123+
124+
$this->files->ensureDirExists($certsPath, user());
125+
126+
$certs = $this->getCertificates($certsPath);
127+
128+
$config = $this->config->read();
129+
$parkedLinks = collect();
130+
foreach ($config['paths'] as $path) {
131+
if ($path === $this->sitesPath()) {
132+
continue;
133+
}
134+
135+
$parkedLinks = $parkedLinks->merge($this->getSites($path, $certs));
136+
}
137+
138+
return $parkedLinks;
113139
}
114140

115141
/**
@@ -139,18 +165,39 @@ function getCertificates($path)
139165
}
140166

141167
/**
142-
* Get list of links and present them formatted.
168+
* @deprecated Use getSites instead which works for both normal and symlinked paths.
143169
*
144170
* @param string $path
145171
* @param \Illuminate\Support\Collection $certs
146172
* @return \Illuminate\Support\Collection
147173
*/
148174
function getLinks($path, $certs)
175+
{
176+
return $this->getSites($path, $certs);
177+
}
178+
179+
/**
180+
* Get list of sites and return them formatted
181+
* Will work for symlink and normal site paths
182+
*
183+
* @param $path
184+
* @param $certs
185+
*
186+
* @return \Illuminate\Support\Collection
187+
*/
188+
function getSites($path, $certs)
149189
{
150190
$config = $this->config->read();
151191

152192
return collect($this->files->scandir($path))->mapWithKeys(function ($site) use ($path) {
153-
return [$site => $this->files->readLink($path.'/'.$site)];
193+
$sitePath = $path.'/'.$site;
194+
195+
if ($this->files->isLink($sitePath)) {
196+
$realPath = $this->files->readLink($sitePath);
197+
} else {
198+
$realPath = $this->files->realpath($sitePath);
199+
}
200+
return [$site => $realPath];
154201
})->map(function ($path, $site) use ($certs, $config) {
155202
$secured = $certs->has($site);
156203
$url = ($secured ? 'https': 'http').'://'.$site.'.'.$config['tld'];

cli/valet.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,15 @@
101101
info(($path === null ? "This" : "The [{$path}]") . " directory has been added to Valet's paths.");
102102
})->descriptions('Register the current working (or specified) directory with Valet');
103103

104+
/**
105+
* Get all the current sites within paths parked with 'park {path}'
106+
*/
107+
$app->command('parked', function () {
108+
$parked = Site::parked();
109+
110+
table(['Site', 'SSL', 'URL', 'Path'], $parked->all());
111+
})->descriptions('Display all the current sites within parked paths');
112+
104113
/**
105114
* Remove the current working directory from the paths configuration.
106115
*/

tests/SiteTest.php

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,135 @@ public function test_get_certificates_will_return_with_multi_segment_tld()
5050
}
5151

5252

53+
public function test_get_sites_will_return_if_secured()
54+
{
55+
$files = Mockery::mock(Filesystem::class);
56+
$dirPath = '/Users/usertest/parkedpath';
57+
$files->shouldReceive('scandir')
58+
->once()
59+
->with($dirPath)
60+
->andReturn(['sitetwo', 'sitethree']);
61+
$files->shouldReceive('isLink')
62+
->andReturn(false);
63+
$files->shouldReceive('realpath')
64+
->twice()
65+
->andReturn($dirPath . '/sitetwo', $dirPath . '/sitethree');
66+
67+
$config = Mockery::mock(Configuration::class);
68+
$config->shouldReceive('read')
69+
->once()
70+
->andReturn(['tld' => 'local']);
71+
72+
swap(Filesystem::class, $files);
73+
swap(Configuration::class, $config);
74+
75+
/** @var Site $site */
76+
$site = resolve(Site::class);
77+
78+
$certs = Mockery::mock(\Illuminate\Support\Collection::class);
79+
$certs->shouldReceive('has')
80+
->twice()
81+
->with(Mockery::on(function ($arg) {
82+
return $arg === 'sitetwo' || $arg === 'sitethree';
83+
}))
84+
->andReturn(false, true);
85+
86+
$sites = $site->getSites($dirPath, $certs);
87+
88+
$this->assertCount(2, $sites);
89+
$this->assertSame([
90+
'site' => 'sitetwo',
91+
'secured' => '',
92+
'url' => 'http://sitetwo.local',
93+
'path' => $dirPath . '/sitetwo',
94+
], $sites->first());
95+
$this->assertSame([
96+
'site' => 'sitethree',
97+
'secured' => ' X',
98+
'url' => 'https://sitethree.local',
99+
'path' => $dirPath . '/sitethree',
100+
], $sites->last());
101+
}
102+
103+
104+
public function test_get_sites_will_work_with_non_symlinked_path()
105+
{
106+
$files = Mockery::mock(Filesystem::class);
107+
$dirPath = '/Users/usertest/parkedpath';
108+
$files->shouldReceive('scandir')
109+
->once()
110+
->with($dirPath)
111+
->andReturn(['sitetwo']);
112+
$files->shouldReceive('isLink')
113+
->once()
114+
->with($dirPath . '/sitetwo')
115+
->andReturn(false);
116+
$files->shouldReceive('realpath')
117+
->once()
118+
->with($dirPath . '/sitetwo')
119+
->andReturn($dirPath . '/sitetwo');
120+
121+
$config = Mockery::mock(Configuration::class);
122+
$config->shouldReceive('read')
123+
->once()
124+
->andReturn(['tld' => 'local']);
125+
126+
swap(Filesystem::class, $files);
127+
swap(Configuration::class, $config);
128+
129+
/** @var Site $site */
130+
$site = resolve(Site::class);
131+
132+
$sites = $site->getSites($dirPath, collect());
133+
$this->assertCount(1, $sites);
134+
$this->assertSame([
135+
'site' => 'sitetwo',
136+
'secured' => '',
137+
'url' => 'http://sitetwo.local',
138+
'path' => $dirPath . '/sitetwo',
139+
], $sites->first());
140+
}
141+
142+
143+
public function test_get_sites_will_work_with_symlinked_path()
144+
{
145+
$files = Mockery::mock(Filesystem::class);
146+
$dirPath = '/Users/usertest/apath';
147+
$files->shouldReceive('scandir')
148+
->once()
149+
->with($dirPath)
150+
->andReturn(['siteone']);
151+
$files->shouldReceive('isLink')
152+
->once()
153+
->with($dirPath . '/siteone')
154+
->andReturn(true);
155+
$files->shouldReceive('readLink')
156+
->once()
157+
->with($dirPath . '/siteone')
158+
->andReturn($linkedPath = '/Users/usertest/linkedpath/siteone');
159+
160+
$config = Mockery::mock(Configuration::class);
161+
$config->shouldReceive('read')
162+
->once()
163+
->andReturn(['tld' => 'local']);
164+
165+
swap(Filesystem::class, $files);
166+
swap(Configuration::class, $config);
167+
168+
/** @var Site $site */
169+
$site = resolve(Site::class);
170+
171+
$sites = $site->getSites($dirPath, collect());
172+
$this->assertCount(1, $sites);
173+
$this->assertSame([
174+
'site' => 'siteone',
175+
'secured' => '',
176+
'url' => 'http://siteone.local',
177+
'path' => $linkedPath,
178+
], $sites->first());
179+
}
180+
181+
53182
public function test_symlink_creates_symlink_to_given_path()
54183
{
55184
$files = Mockery::mock(Filesystem::class);

0 commit comments

Comments
 (0)