1: <?php
2:
3: namespace Himedia\Padocc\Task\Base;
4:
5: use GAubry\Shell\PathStatus;
6: use Himedia\Padocc\AttributeProperties;
7: use Himedia\Padocc\Task;
8:
9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23:
24: class Link extends Task
25: {
26: 27: 28:
29: protected function init()
30: {
31: parent::init();
32:
33: $this->aAttrProperties = array(
34: 'src' => AttributeProperties::REQUIRED | AttributeProperties::FILE | AttributeProperties::DIR
35: | AttributeProperties::ALLOW_PARAMETER,
36: 'target' => AttributeProperties::FILE | AttributeProperties::DIR | AttributeProperties::REQUIRED
37: | AttributeProperties::ALLOW_PARAMETER,
38: 'server' => AttributeProperties::ALLOW_PARAMETER
39: );
40: }
41:
42: 43: 44: 45:
46: public static function getTagName ()
47: {
48: return 'link';
49: }
50:
51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61:
62: public function check ()
63: {
64: parent::check();
65:
66: list($bIsSrcRemote, $sSrcServer, ) = $this->oShell->isRemotePath($this->aAttValues['src']);
67: list($bIsDestRemote, $sDestServer, ) = $this->oShell->isRemotePath($this->aAttValues['target']);
68: if (($bIsSrcRemote xor $bIsDestRemote)
69: || ($bIsSrcRemote && $bIsDestRemote && $sSrcServer != $sDestServer)
70: ) {
71: $sMsg = 'Servers must be equals!' . ' Src=' . $this->aAttValues['src']
72: . ' Target=' . $this->aAttValues['target'];
73: throw new \DomainException($sMsg);
74: }
75:
76: if (! empty($this->aAttValues['server']) && ($bIsSrcRemote || $bIsDestRemote)) {
77: $sMsg = 'Multiple server declaration!' . ' Server=' . $this->aAttValues['server']
78: . ' Src=' . $this->aAttValues['src'] . ' Target=' . $this->aAttValues['target'];
79: throw new \DomainException($sMsg);
80: }
81:
82:
83: if (! isset($this->aAttValues['server'])) {
84: $this->aAttValues['server'] = '';
85: }
86: }
87:
88: 89: 90: 91: 92: 93:
94: protected function centralExecute ()
95: {
96: parent::centralExecute();
97: $this->getLogger()->info('+++');
98:
99:
100: $sPath = $this->aAttValues['src'];
101: if (! empty($this->aAttValues['server'])) {
102: $sPath = $this->aAttValues['server'] . ':' . $sPath;
103: }
104: $aValidSources = array(
105: PathStatus::STATUS_NOT_EXISTS,
106: PathStatus::STATUS_SYMLINKED_FILE,
107: PathStatus::STATUS_SYMLINKED_DIR,
108: PathStatus::STATUS_BROKEN_SYMLINK
109: );
110: foreach ($this->expandPath($sPath) as $sExpandedPath) {
111: if (! in_array($this->oShell->getPathStatus($sExpandedPath), $aValidSources)) {
112: $sMsg = "Source attribute must be a symlink or not exist: '" . $sExpandedPath . "'";
113: throw new \RuntimeException($sMsg);
114: }
115: }
116:
117: $sRawTargetPath = $this->aAttValues['target'];
118: if (! empty($this->aAttValues['server'])) {
119: $sRawTargetPath = $this->aAttValues['server'] . ':' . $sRawTargetPath;
120: }
121: $this->getLogger()->info("Create symlink from '$sPath' to '$sRawTargetPath'.+++");
122:
123: $aTargetPaths = $this->processPath($sRawTargetPath);
124: foreach ($aTargetPaths as $sTargetPath) {
125: list(, $sDestServer, ) = $this->oShell->isRemotePath($sTargetPath);
126: if (! empty($sDestServer)) {
127: $sDestServer .= ':';
128: }
129: list(, , $sSrcRealPath) = $this->oShell->isRemotePath($this->aAttValues['src']);
130: $sSrc = $this->processSimplePath($sDestServer . $sSrcRealPath);
131: $this->oShell->createLink($sSrc, $sTargetPath);
132: }
133: $this->getLogger()->info('------');
134: }
135: }
136: