getDestinationPath();
foreach ($this->files as $file) {
$this->mirrorFile($file);
}
foreach ($this->directories as $directory) {
$this->mirrorDirectory($directory);
}
foreach ($this->wildcards as $wildcard) {
$this->mirrorWildcard($wildcard);
}
$this->output->writeln('Mirror complete!');
}
protected function mirrorFile($file)
{
$this->output->writeln(sprintf(' - Mirroring: %s', $file));
$src = base_path().'/'.$file;
$dest = $this->getDestinationPath().'/'.$file;
if (!File::isFile($src) || File::isFile($dest)) {
return false;
}
$this->mirror($src, $dest);
}
protected function mirrorDirectory($directory)
{
$this->output->writeln(sprintf(' - Mirroring: %s', $directory));
$src = base_path().'/'.$directory;
$dest = $this->getDestinationPath().'/'.$directory;
if (!File::isDirectory($src) || File::isDirectory($dest)) {
return false;
}
if (!File::isDirectory(dirname($dest))) {
File::makeDirectory(dirname($dest), 0755, true);
}
$this->mirror($src, $dest);
}
protected function mirrorWildcard($wildcard)
{
if (strpos($wildcard, '*') === false) {
return $this->mirrorDirectory($wildcard);
}
list($start, $end) = explode('*', $wildcard, 2);
$startDir = base_path().'/'.$start;
if (!File::isDirectory($startDir)) {
return false;
}
foreach (File::directories($startDir) as $directory) {
$this->mirrorWildcard($start.basename($directory).$end);
}
}
protected function mirror($src, $dest)
{
if ($this->option('relative')) {
$src = $this->getRelativePath($dest, $src);
if (strpos($src, '../') === 0) {
$src = rtrim(substr($src, 3), '/');
}
}
symlink($src, $dest);
}
protected function getDestinationPath()
{
if ($this->destinationPath !== null) {
return $this->destinationPath;
}
$destPath = $this->argument('destination');
if (realpath($destPath) === false) {
$destPath = base_path() . '/' . $destPath;
}
if (!File::isDirectory($destPath)) {
File::makeDirectory($destPath, 0755, true);
}
$destPath = realpath($destPath);
$this->output->writeln(sprintf('Destination: %s', $destPath));
return $this->destinationPath = $destPath;
}
protected function getRelativePath($from, $to)
{
$from = str_replace('\\', '/', $from);
$to = str_replace('\\', '/', $to);
$dir = explode('/', is_file($from) ? dirname($from) : rtrim($from, '/'));
$file = explode('/', $to);
while ($dir && $file && ($dir[0] == $file[0])) {
array_shift($dir);
array_shift($file);
}
return str_repeat('../', count($dir)) . implode('/', $file);
}
/**
* Get the console command arguments.
*/
protected function getArguments()
{
return [
['destination', InputArgument::REQUIRED, 'The destination path relative to the current directory. Eg: public/'],
];
}
/**
* Get the console command options.
*/
protected function getOptions()
{
return [
['relative', null, InputOption::VALUE_NONE, 'Create symlinks relative to the public directory.'],
];
}
}