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 Himedia\Padocc\AttributeProperties;
  6: use Himedia\Padocc\Task;
  7: 
  8: /**
  9:  * Exécute l'outil de gestion de dépendances PHP composer.
 10:  * À inclure dans une tâche env ou target.
 11:  *
 12:  * Attributs :
 13:  * - 'dir' : répertoire d'où exécuter composer
 14:  * - 'options' : options à transmettre à la commande 'composer install', par défaut '--no-dev'
 15:  *
 16:  * Exemple : <composer dir="${TMPDIR}" options="--no-dev" />
 17:  *
 18:  * @author Geoffroy AUBRY <gaubry@hi-media.com>
 19:  */
 20: class Composer extends Task
 21: {
 22:     /**
 23:      * {@inheritdoc}
 24:      */
 25:     protected function init()
 26:     {
 27:         parent::init();
 28: 
 29:         $this->aAttrProperties = array(
 30:             'dir' => AttributeProperties::DIR | AttributeProperties::REQUIRED
 31:                 | AttributeProperties::ALLOW_PARAMETER,
 32:             'options' => 0
 33:         );
 34:     }
 35: 
 36:     /**
 37:      * {@inheritdoc}
 38:      * @codeCoverageIgnore
 39:      */
 40:     public static function getTagName()
 41:     {
 42:         return 'composer';
 43:     }
 44: 
 45:     /**
 46:      * Vérifie au moyen de tests basiques que la tâche peut être exécutée.
 47:      * Lance une exception si tel n'est pas le cas.
 48:      *
 49:      * Comme toute les tâches sont vérifiées avant que la première ne soit exécutée,
 50:      * doit permettre de remonter au plus tôt tout dysfonctionnement.
 51:      * Appelé avant la méthode execute().
 52:      *
 53:      * @throws \UnexpectedValueException en cas d'attribut ou fichier manquant
 54:      * @throws \DomainException en cas de valeur non permise
 55:      */
 56:     public function check ()
 57:     {
 58:         parent::check();
 59: 
 60:         if (empty($this->aAttValues['options'])) {
 61:             $this->aAttValues['options'] = '--no-dev';
 62:         }
 63:     }
 64: 
 65:     /**
 66:      * Phase de traitements centraux de l'exécution de la tâche.
 67:      * Elle devrait systématiquement commencer par "parent::centralExecute();".
 68:      * Appelé par execute().
 69:      * @see execute()
 70:      */
 71:     protected function centralExecute ()
 72:     {
 73:         parent::centralExecute();
 74:         $this->getLogger()->info('+++');
 75: 
 76:         $sInstallCmdPattern = '%1$s install --working-dir "%2$s" %3$s';
 77:         $sWGetCmd = 'wget -q --no-check-certificate http://getcomposer.org/installer -O - | php';
 78:         $sCURLCmd = 'curl -sS https://getcomposer.org/installer | php';
 79:         $sCheckCmd = 'which composer 1>/dev/null 2>&1 && echo -n 1 || echo -n 0; '
 80:               . 'which wget     1>/dev/null 2>&1 && echo -n 1 || echo -n 0; '
 81:               . 'which curl     1>/dev/null 2>&1 && echo 1    || echo 0';
 82: 
 83:         $aDirs = $this->processPath($this->aAttValues['dir']);
 84:         foreach ($aDirs as $sDir) {
 85:             list(, , $sLocalPath) = $this->oShell->isRemotePath($sDir);
 86:             $aResult = $this->oShell->execSSH($sCheckCmd, $sDir);
 87:             $isComposerInstalled = (substr($aResult[0], 0, 1) === '1');
 88:             $isWGetInstalled = (substr($aResult[0], 1, 1) === '1');
 89:             $isCURLInstalled = (substr($aResult[0], 2, 1) === '1');
 90: 
 91:             // Config:
 92:             if ($isComposerInstalled) {
 93:                 $sComposerBin = 'composer';
 94:                 $sDownloadCmd = '';
 95:             } elseif ($isWGetInstalled) {
 96:                 $sComposerBin = 'php composer.phar';
 97:                 $sDownloadCmd = $sWGetCmd;
 98:             } elseif ($isCURLInstalled) {
 99:                 $sComposerBin = 'php composer.phar';
100:                 $sDownloadCmd = $sCURLCmd;
101:             } else {
102:                 $sMsg = 'Composer is not installed, but nor are both wget and curl to install it!';
103:                 throw new \RuntimeException($sMsg);
104:             }
105: 
106:             // Optional installation:
107:             if (! empty($sDownloadCmd)) {
108:                 $this->getLogger()->info('Install composer:+++');
109:                 $aResult = $this->oShell->execSSH($sDownloadCmd, $sDir);
110:                 $this->getLogger()->info(implode("\n", $aResult) . '---');
111:             }
112: 
113:             // Execution:
114:             $this->getLogger()->info("Execute composer on '$sDir':+++");
115:             $sCmd = sprintf($sInstallCmdPattern, $sComposerBin, $sLocalPath, $this->aAttValues['options']);
116:             $aResult = $this->oShell->execSSH($sCmd, $sDir);
117:             $this->getLogger()->info(implode("\n", $aResult) . '---');
118:         }
119: 
120:         $this->getLogger()->info('---');
121:     }
122: }
123: 
Platform for Automatized Deployments with pOwerful Concise Configuration API documentation generated by ApiGen 2.8.0