Overview

Namespaces

  • GAubry
    • ErrorHandler
    • Helpers
    • Logger
    • Shell
  • Himedia
    • Padocc
      • DB
      • Minifier
      • Numbering
      • Properties
      • Task
        • Base
        • Extended
  • None
  • Psr
    • Log

Classes

  • Backup
  • Call
  • Composer
  • Copy
  • Environment
  • ExternalProperty
  • FillTemplate
  • HTTP
  • Link
  • MkDir
  • Project
  • Property
  • Rename
  • Sync
  • Target
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  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:  * Crée un lien symbolique.
 11:  * À inclure dans une tâche env ou target.
 12:  *
 13:  * Attributs :
 14:  * - 'src'
 15:  * - 'target' : la cible n'a pas forcément besoin d'exister, ce qui créera un lien cassé.
 16:  *   Ceci peut-être intéressant lorsque l'on prépare un déploiement dans un fichier temporaire
 17:  *   avant d'effectuer la synchronisation finale vers tous les serveurs (cf. l'exemple ci-dessous).
 18:  * - 'server' : à utiliser si l'on doit créer des mêmes liens sur plusieurs serveurs
 19:  *
 20:  * Exemple : <link src="${TMPDIR}/inc/config.php" target="../../../config/www.twenga/config.php" />
 21:  *
 22:  * @author Geoffroy AUBRY <gaubry@hi-media.com>
 23:  */
 24: class Link extends Task
 25: {
 26:     /**
 27:      * {@inheritdoc}
 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:      * {@inheritdoc}
 44:      * @codeCoverageIgnore
 45:      */
 46:     public static function getTagName ()
 47:     {
 48:         return 'link';
 49:     }
 50: 
 51:     /**
 52:      * Vérifie au moyen de tests basiques que la tâche peut être exécutée.
 53:      * Lance une exception si tel n'est pas le cas.
 54:      *
 55:      * Comme toute les tâches sont vérifiées avant que la première ne soit exécutée,
 56:      * doit permettre de remonter au plus tôt tout dysfonctionnement.
 57:      * Appelé avant la méthode execute().
 58:      *
 59:      * @throws \UnexpectedValueException en cas d'attribut ou fichier manquant
 60:      * @throws \DomainException en cas de valeur non permise
 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:         // Valeur par défaut :
 83:         if (! isset($this->aAttValues['server'])) {
 84:             $this->aAttValues['server'] = '';
 85:         }
 86:     }
 87: 
 88:     /**
 89:      * Phase de traitements centraux de l'exécution de la tâche.
 90:      * Elle devrait systématiquement commencer par "parent::centralExecute();".
 91:      * Appelé par execute().
 92:      * @see execute()
 93:      */
 94:     protected function centralExecute ()
 95:     {
 96:         parent::centralExecute();
 97:         $this->getLogger()->info('+++');
 98: 
 99:         // La source doit être un lien symbolique ou ne pas exister :
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: 
Platform for Automatized Deployments with pOwerful Concise Configuration API documentation generated by ApiGen 2.8.0