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

118 lines
3.6 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;
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 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()
{
$linkedFiles = $this->getParameter('linked_files', array());
$linkedFolders = $this->getParameter(self::LINKED_FOLDERS, array());
$linkingStrategy = $this->getParameter(self::LINKED_STRATEGY, self::ABSOLUTE_LINKING);
2014-11-05 13:36:14 +01:00
$linkedEntities = array_merge($linkedFiles, $linkedFolders);
if (empty($linkedFiles) && empty($linkedFolders)) {
throw new SkipException('No files and folders configured for sym-linking.');
}
$sharedFolderName = $this->getParameter('shared', 'shared');
$sharedFolderPath = rtrim($this->getConfig()->deployment('to'), '/') . '/' . $sharedFolderName;
$releasesDirectory = $this->getConfig()->release('directory', 'releases');
$releasesDirectoryPath = rtrim($this->getConfig()->deployment('to'), '/') . '/' . $releasesDirectory;
$currentCopy = $releasesDirectoryPath . '/' . $this->getConfig()->getReleaseId();
2014-11-05 13:36:14 +01:00
$relativeDiffPath = str_replace($this->getConfig()->deployment('to'), '', $currentCopy) . '/';
foreach ($linkedEntities as $ePath) {
2014-11-05 14:39:43 +01:00
list($entityPath, $strategy) = $this->getPath($ePath);
$sharedEntityLinkedPath = "$sharedFolderPath/$entityPath";
2014-11-05 13:36:14 +01:00
if ($strategy == self::RELATIVE_LINKING) {
$parentFolderPath = dirname($entityPath);
2014-11-05 13:36:14 +01:00
$relativePath = $parentFolderPath == '.' ? $relativeDiffPath : $relativeDiffPath . $parentFolderPath . '/';
$sharedEntityLinkedPath = ltrim(
preg_replace('/(\w+\/)/', '../', $relativePath),
'/'
) . $sharedFolderName . '/' . $entityPath;
}
$command = "ln -nfs $sharedEntityLinkedPath $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
}