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:  * Synchronise efficacement (rsync Shell) et avec suppression le contenu d'un répertoire à l'intérieur d'un autre.
 11:  * À inclure dans une tâche env ou target.
 12:  *
 13:  * Attributs :
 14:  * - 'src'
 15:  * - 'destdir'
 16:  * - 'include'
 17:  * - 'exclude' : à noter que systématiquement sont exclus '.bzr/', '.cvsignore', '.git/', '.gitignore',
 18:  *   '.svn/', 'cvslog.*', 'CVS' et 'CVS.adm'
 19:  *
 20:  * Exemples :
 21:  * <sync src="${TMPDIR}/" destdir="${WEB_SERVERS}:${BASEDIR}" exclude="v3/css v3/js v4/css v4/js" />
 22:  * <sync src="prod@fs3:/home/prod/twenga_files/merchant_logos/"
 23:  *     destdir="${TMPDIR}/img/sites" include="*.png" exclude="*" />
 24:  *
 25:  * @author Geoffroy AUBRY <gaubry@hi-media.com>
 26:  */
 27: class Sync extends Task
 28: {
 29:     /**
 30:      * {@inheritdoc}
 31:      */
 32:     protected function init()
 33:     {
 34:         parent::init();
 35: 
 36:         $this->aAttrProperties = array(
 37:             'src' => AttributeProperties::SRC_PATH | AttributeProperties::FILEJOKER | AttributeProperties::REQUIRED
 38:                 | AttributeProperties::ALLOW_PARAMETER,
 39:             'destdir' => AttributeProperties::DIR | AttributeProperties::REQUIRED
 40:                 | AttributeProperties::ALLOW_PARAMETER,
 41:             // TODO AttributeProperties::DIRJOKER abusif ici, mais à cause du multivalué :
 42:             'include' => AttributeProperties::FILEJOKER | AttributeProperties::DIRJOKER,
 43:             // TODO AttributeProperties::DIRJOKER abusif ici, mais à cause du multivalué :
 44:             'exclude' => AttributeProperties::FILEJOKER | AttributeProperties::DIRJOKER,
 45:         );
 46:     }
 47: 
 48:     /**
 49:      * {@inheritdoc}
 50:      * @codeCoverageIgnore
 51:      */
 52:     public static function getTagName ()
 53:     {
 54:         return 'sync';
 55:     }
 56: 
 57:     /**
 58:      * Vérifie au moyen de tests basiques que la tâche peut être exécutée.
 59:      * Lance une exception si tel n'est pas le cas.
 60:      *
 61:      * Comme toute les tâches sont vérifiées avant que la première ne soit exécutée,
 62:      * doit permettre de remonter au plus tôt tout dysfonctionnement.
 63:      * Appelé avant la méthode execute().
 64:      *
 65:      * @throws \UnexpectedValueException en cas d'attribut ou fichier manquant
 66:      * @throws \DomainException en cas de valeur non permise
 67:      */
 68:     public function check ()
 69:     {
 70:         parent::check();
 71: 
 72:         if (preg_match('#\*|\?|/$#', $this->aAttValues['src']) === 0
 73:             && $this->oShell->getPathStatus($this->aAttValues['src']) === PathStatus::STATUS_DIR
 74:         ) {
 75:             $this->aAttValues['destdir'] .= '/' . substr(strrchr($this->aAttValues['src'], '/'), 1);
 76:             $this->aAttValues['src'] .= '/';
 77:         }
 78:     }
 79: 
 80:     /**
 81:      * Phase de traitements centraux de l'exécution de la tâche.
 82:      * Elle devrait systématiquement commencer par "parent::centralExecute();".
 83:      * Appelé par execute().
 84:      * @see execute()
 85:      */
 86:     protected function centralExecute ()
 87:     {
 88:         parent::centralExecute();
 89:         $sMsg = "+++Synchronize '" . $this->aAttValues['src'] . "' with '" . $this->aAttValues['destdir'] . "'+++";
 90:         $this->getLogger()->info($sMsg);
 91: 
 92:         // include / exclude :
 93:         $aIncludedPaths = (empty($this->aAttValues['include'])
 94:                           ? array()
 95:                           : explode(' ', $this->aAttValues['include']));
 96:         $aExcludedPaths = (empty($this->aAttValues['exclude'])
 97:                           ? array()
 98:                           : explode(' ', $this->aAttValues['exclude']));
 99: 
100:         list($bIsDestRemote, $sDestServer, $sDestRawPath) = $this->oShell->isRemotePath($this->aAttValues['destdir']);
101:         $sDestPath = ($bIsDestRemote ? '[]:' . $sDestRawPath : $sDestRawPath);
102:         foreach ($this->processPath($sDestPath) as $sDestRealPath) {
103:             $aResults = $this->oShell->sync(
104:                 $this->processSimplePath($this->aAttValues['src']),
105:                 $this->processSimplePath($sDestRealPath),
106:                 $this->processPath($sDestServer),
107:                 $aIncludedPaths,
108:                 $aExcludedPaths
109:             );
110:             foreach ($aResults as $sResult) {
111:                 $this->getLogger()->info($sResult);
112:             }
113:         }
114:         $this->getLogger()->info('------');
115:     }
116: }
117: 
Platform for Automatized Deployments with pOwerful Concise Configuration API documentation generated by ApiGen 2.8.0