diff --git a/cli/Valet/Site.php b/cli/Valet/Site.php index 52bd8f2..fb8c91a 100644 --- a/cli/Valet/Site.php +++ b/cli/Valet/Site.php @@ -22,6 +22,43 @@ function __construct(Configuration $config, CommandLine $cli, Filesystem $files) $this->config = $config; } + /** + * Get the name of the site. + * + * @param string|null $name + * @return string + */ + private function getRealSiteName($name) + { + if (! is_null($name)) { + return $name; + } + + if (is_string($link = $this->getLinkNameByCurrentDir())) { + return $link; + } + + return basename(getcwd()); + } + + /** + * Get link name based on the current directory. + * + * @return null|string + */ + private function getLinkNameByCurrentDir() + { + $count = count($links = $this->links()->where('path', getcwd())); + + if ($count == 1) { + return $links->shift()['site']; + } + + if ($count > 1) { + throw new DomainException("There are {$count} links related to the current directory, please specify the name: valet unlink ."); + } + } + /** * Get the real hostname for the given path, checking links. * @@ -106,7 +143,12 @@ function getLinks($path, $certs) $secured = $certs->has($site); $url = ($secured ? 'https': 'http').'://'.$site.'.'.$config['domain']; - return [$site, $secured ? ' X': '', $url, $path]; + return [ + 'site' => $site, + 'secured' => $secured ? ' X': '', + 'url' => $url, + 'path' => $path, + ]; }); } @@ -118,9 +160,13 @@ function getLinks($path, $certs) */ function unlink($name) { + $name = $this->getRealSiteName($name); + if ($this->files->exists($path = $this->sitesPath().'/'.$name)) { $this->files->unlink($path); } + + return $name; } /** diff --git a/cli/valet.php b/cli/valet.php index 24b1b94..ada3c7d 100755 --- a/cli/valet.php +++ b/cli/valet.php @@ -112,9 +112,7 @@ * Unlink a link from the Valet links directory. */ $app->command('unlink [name]', function ($name) { - Site::unlink($name = $name ?: basename(getcwd())); - - info('The ['.$name.'] symbolic link has been removed.'); + info('The ['.Site::unlink($name).'] symbolic link has been removed.'); })->descriptions('Remove the specified Valet link'); /**