Skip to content

Commit

Permalink
Merge pull request #104 from joomlatools/feature/103-relative
Browse files Browse the repository at this point in the history
Merge feature/103-relative
  • Loading branch information
Steven Rombauts authored Jun 11, 2019
2 parents 782ea08 + 975fd05 commit 3af5126
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Joomlatools\Console\Command\Extension\Iterator;

use Joomlatools\Console\Command\Extension\Symlink;
use Joomlatools\Console\Joomla\Util;
use Symfony\Component\Console\Output\OutputInterface;

Expand Down Expand Up @@ -59,6 +60,8 @@ public function createLink($source, $target)
{
if (!file_exists($target))
{
$source = Symlink::buildSymlinkPath($source, $target);

if (!is_null($this->_output) && $this->_output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
$this->_output->writeln(" * creating link: `$target` -> `$source`");
}
Expand Down
45 changes: 43 additions & 2 deletions src/Joomlatools/Console/Command/Extension/Symlink.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ class Symlink extends AbstractSite
protected $symlink = array();
protected $projects = array();

protected static $_symlinkers = array();

protected static $_symlinkers = array();
protected static $_dependencies = array();
protected static $_relative = false;

public static function registerDependencies($project, array $dependencies)
{
Expand Down Expand Up @@ -70,6 +70,12 @@ protected function configure()
InputOption::VALUE_REQUIRED,
'Directory where your custom projects reside',
sprintf('%s/Projects', trim(`echo ~`))
)
->addOption(
'relative',
'r',
InputOption::VALUE_NONE,
'Use relative paths to the site root instead of absolute paths.'
);
}

Expand Down Expand Up @@ -110,6 +116,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->projects = array_unique(array_merge($this->projects, $this->_getDependencies($symlink)));
}

static::$_relative = $input->getOption('relative') === true;

$this->check($input, $output);
$this->symlinkProjects($input, $output);
}
Expand Down Expand Up @@ -210,4 +218,37 @@ protected function _getDependencies($project)

return $projects;
}

public static function buildSymlinkPath($source, $target)
{
$source = realpath($source);

if (static::$_relative === true)
{
$separator = DIRECTORY_SEPARATOR;
$from = is_dir($target) ? $target : dirname($target);

// In some cases a path that has been concatenated from
// different strings contains double forward slashes.
// Make sure to replace these so we don't get incorrect paths:
$from = str_replace($separator.$separator, $separator, $from);
$source = str_replace($separator.$separator, $separator, $source);

$partsFrom = explode($separator, rtrim($from, $separator));
$partsTo = explode($separator, rtrim($source, $separator));

while(count($partsFrom) && count($partsTo) && ($partsFrom[0] == $partsTo[0]))
{
array_shift($partsFrom);
array_shift($partsTo);
}

$prefix = str_repeat(sprintf('..%s', $separator), count($partsFrom));
$suffix = implode($separator, $partsTo);

$source = $prefix . $suffix;
}

return $source;
}
}
4 changes: 4 additions & 0 deletions src/Joomlatools/Console/Symlinkers/joomlatools-components.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@

if (!file_exists($code_destination))
{
$project = Extension\Symlink::buildSymlinkPath($project, $code_destination);

if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
$output->writeln(" * creating link `$code_destination` -> $project");
}
Expand All @@ -69,6 +71,8 @@

if (is_dir($media) && !file_exists($target))
{
$media = Extension\Symlink::buildSymlinkPath($media, $target);

if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
$output->writeln(" * creating link `$target` -> $media");
}
Expand Down
2 changes: 2 additions & 0 deletions src/Joomlatools/Console/Symlinkers/joomlatools-framework.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
{
if (is_dir($from) && !file_exists($to))
{
$from = Extension\Symlink::buildSymlinkPath($from, $to);

if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
$output->writeln(" * creating link `$to` -> $from");
}
Expand Down

0 comments on commit 3af5126

Please sign in to comment.