magallanes/Mage/Task/BuiltIn/Filesystem/LinkSharedFilesTask.php

118 lines
3.4 KiB
PHP
Raw Normal View History

<?php
2014-05-23 03:56:05 +02:00
namespace Mage\Task\BuiltIn\Filesystem;
use Mage\Task\AbstractTask;
use Mage\Task\Releases\IsReleaseAware;
use Mage\Task\SkipException;
use Symfony\Component\Filesystem\Filesystem;
2014-11-05 13:36:14 +01:00
/**
* Class LinkSharedFilesTask
*
* @package Mage\Task\BuiltIn\Filesystem
* @author Andrey Kolchenko <a.j.kolchenko@baltsoftservice.ru>
*/
class LinkSharedFilesTask extends AbstractTask implements IsReleaseAware
{
2014-11-05 14:42:13 +01:00
/**
* Linked folders parameter name
*/
const LINKED_FILES = 'linked_files';
2014-11-05 13:36:14 +01:00
/**
* Linked folders parameter name
*/
const LINKED_FOLDERS = 'linked_folders';
/**
* Linking strategy parameter name
*/
const LINKED_STRATEGY = 'linking_strategy';
2014-11-05 13:36:14 +01:00
/**
* Absolute linked strategy
*/
const ABSOLUTE_LINKING = 'absolute';
2014-11-05 13:36:14 +01:00
/**
* Relative linked strategy
*/
const RELATIVE_LINKING = 'relative';
2014-11-05 13:36:14 +01:00
/**
* @var array
*/
2014-11-05 14:39:43 +01:00
private static $linkingStrategies = array(
self::ABSOLUTE_LINKING,
self::RELATIVE_LINKING
);
2014-11-05 13:36:14 +01:00
/**
* Returns the Title of the Task
2014-11-05 13:36:14 +01:00
*
* @return string
*/
public function getName()
{
return 'Linking files/folders from the shared folder into the current release [built-in]';
}
/**
* Runs the task
*
* @return boolean
* @throws SkipException
*/
public function run()
{
2014-11-05 14:42:13 +01:00
$linkedEntities = array_merge(
$this->getParameter(self::LINKED_FILES, array()),
$this->getParameter(self::LINKED_FOLDERS, array())
);
2014-11-05 14:42:13 +01:00
if (empty($linkedEntities)) {
throw new SkipException('No files and folders configured for sym-linking.');
}
$remoteDirectory = rtrim($this->getConfig()->deployment('to'), '/') . '/';
$sharedFolderPath = $remoteDirectory . $this->getParameter('shared', 'shared');
$releasesDirectoryPath = $remoteDirectory . $this->getConfig()->release('directory', 'releases');
$currentCopy = $releasesDirectoryPath . '/' . $this->getConfig()->getReleaseId();
$fileSystem = new Filesystem();
foreach ($linkedEntities as $ePath) {
2014-11-05 14:39:43 +01:00
list($entityPath, $strategy) = $this->getPath($ePath);
if ($strategy === self::RELATIVE_LINKING) {
$dirName = dirname($currentCopy . '/' . $entityPath);
$target = $fileSystem->makePathRelative($sharedFolderPath, $dirName) . $entityPath;
} else {
$target = $sharedFolderPath . '/' . $entityPath;
}
$command = 'mkdir -p ' . escapeshellarg(dirname($target));
$this->runCommandRemote($command);
$command = 'ln -nfs ' . escapeshellarg($target) . ' ' . escapeshellarg($currentCopy . '/' . $entityPath);
$this->runCommandRemote($command);
}
return true;
}
2014-11-05 14:39:43 +01:00
/**
* @param array|string $linkedEntity
*
* @return array [$path, $strategy]
*/
private function getPath($linkedEntity)
{
$linkingStrategy = $this->getParameter(self::LINKED_STRATEGY, self::ABSOLUTE_LINKING);
if (is_array($linkedEntity)) {
list($path, $strategy) = each($linkedEntity);
if (!in_array($strategy, self::$linkingStrategies)) {
$strategy = $linkingStrategy;
}
} else {
$strategy = $linkingStrategy;
$path = $linkedEntity;
}
return [$path, $strategy];
}
2014-11-05 13:36:14 +01:00
}