1: <?php
2:
3: namespace Himedia\Padocc\Task\Base;
4:
5: use Himedia\Padocc\AttributeProperties;
6: use Himedia\Padocc\DIContainerInterface;
7: use Himedia\Padocc\Task\WithProperties;
8:
9: /**
10: * Tâche mère d'un fichier XML.
11: * Contient des tags env ou target.
12: *
13: * Attribut 'name' doit être identique au nom contenu dans la configuration XML.
14: *
15: * Exemple : <project name="rts">...</project>
16: *
17: * @author Original Author Geoffroy AUBRY <gaubry@hi-media.com>
18: * @author Another Author Tony Caron <caron.tony@gmail.com>
19: */
20: class Project extends WithProperties
21: {
22: /**
23: * Tâche appelée.
24: * @var Environment
25: */
26: private $oBoundTask;
27:
28: /**
29: * @var string Selected environment.
30: */
31: public $sEnvName;
32:
33: /**
34: * Constructor.
35: *
36: * @param \SimpleXMLElement|string $sXmlProject XML project path or XML data
37: * @param string $sEnvName Selected environment.
38: * @param DIContainerInterface $oDIContainer Service container.
39: *
40: * @throws \UnexpectedValueException si fichier XML du projet non trouvé
41: * @throws \UnexpectedValueException si environnement non trouvé ou non unique
42: */
43: public function __construct ($sXmlProject, $sEnvName, DIContainerInterface $oDIContainer)
44: {
45: $this->sEnvName = $sEnvName;
46:
47: if ($sXmlProject instanceof \SimpleXMLElement) {
48: $oSXEProject = $sXmlProject;
49: } else {
50: $oSXEProject = self::getSXEProject($sXmlProject);
51: }
52:
53: parent::__construct($oSXEProject, $this, $oDIContainer);
54: }
55:
56: /**
57: * {@inheritdoc}
58: *
59: * @throws \UnexpectedValueException si fichier XML du projet non trouvé
60: * @throws \UnexpectedValueException si environnement non trouvé ou non unique
61: */
62: protected function init()
63: {
64: parent::init();
65:
66: $this->aAttrProperties = array_merge(
67: $this->aAttrProperties,
68: array('name' => AttributeProperties::REQUIRED)
69: );
70:
71: // Crée une instance de la tâche environnement appelée :
72: $aTargets = $this->oProject->getSXE()->xpath("env[@name='" . $this->sEnvName . "']");
73: if (count($aTargets) !== 1) {
74: $sMsg = "Environment '$this->sEnvName' not found or not unique in this project!";
75: throw new \UnexpectedValueException($sMsg);
76: }
77:
78: $this->oBoundTask = new Environment($aTargets[0], $this->oProject, $this->oDIContainer);
79: }
80:
81: /**
82: * {@inheritdoc}
83: * @codeCoverageIgnore
84: */
85: public static function getTagName ()
86: {
87: return 'project';
88: }
89:
90: /**
91: * Retourne une instance SimpleXMLElement du projet spécifié.
92: *
93: * @param string $sXmlProject XML project path or XML data
94: * @throws \UnexpectedValueException si XML du projet mal formaté
95: * @return \SimpleXMLElement instance du projet spécifié
96: */
97: public static function getSXEProject ($sXmlProject)
98: {
99: $bIsURL = (substr($sXmlProject, 0, 5) != '<?xml');
100: try {
101: $oSXE = new \SimpleXMLElement($sXmlProject, null, $bIsURL);
102: } catch (\Exception $oException) {
103: throw new \UnexpectedValueException("Bad project definition: '$sXmlProject'", 1, $oException);
104: }
105: return $oSXE;
106: }
107:
108: /**
109: * Vérifie au moyen de tests basiques que la tâche peut être exécutée.
110: * Lance une exception si tel n'est pas le cas.
111: *
112: * Comme toute les tâches sont vérifiées avant que la première ne soit exécutée,
113: * doit permettre de remonter au plus tôt tout dysfonctionnement.
114: * Appelé avant la méthode execute().
115: *
116: * @throws \UnexpectedValueException en cas d'attribut ou fichier manquant
117: * @throws \DomainException en cas d'attribut non permis
118: * @see self::$aAttributeProperties
119: */
120: public function check()
121: {
122: parent::check();
123: $this->getLogger()->info('+++');
124: foreach ($this->aAttValues as $sAttribute => $sValue) {
125: if (! empty($sValue) && $sAttribute !== 'name') {
126: $this->getLogger()->info("Attribute: $sAttribute = '$sValue'");
127: }
128: }
129: $this->getLogger()->info('---');
130: }
131:
132: /**
133: * Prépare la tâche avant exécution : vérifications basiques, analyse des serveurs concernés...
134: */
135: public function setUp ()
136: {
137: parent::setUp();
138: $this->oBoundTask->setUp();
139: }
140:
141: /**
142: * Phase de pré-traitements de l'exécution de la tâche.
143: * Elle devrait systématiquement commencer par "parent::preExecute();".
144: * Appelé par execute().
145: * @see execute()
146: */
147: protected function preExecute ()
148: {
149: parent::preExecute();
150: $this->getLogger()->info('+++');
151: $this->oShell->mkdir($this->oProperties->getProperty('tmpdir'));
152: $this->getLogger()->info('---');
153: }
154:
155: /**
156: * Phase de traitements centraux de l'exécution de la tâche.
157: * Elle devrait systématiquement commencer par "parent::centralExecute();".
158: * Appelé par execute().
159: * @see execute()
160: */
161: protected function centralExecute ()
162: {
163: parent::centralExecute();
164: $this->oBoundTask->execute();
165: }
166:
167: /**
168: * Phase de post-traitements de l'exécution de la tâche.
169: * Elle devrait systématiquement finir par "parent::postExecute();".
170: * Appelé par execute().
171: * @see execute()
172: */
173: protected function postExecute()
174: {
175: $this->getLogger()->info('+++');
176: $this->oShell->remove($this->oProperties->getProperty('tmpdir'));
177: $this->getLogger()->info('---');
178: parent::postExecute();
179: }
180:
181: /**
182: * Retourne le contenu XML de la tâche.
183: * @return \SimpleXMLElement le contenu XML de la tâche.
184: */
185: public function getSXE ()
186: {
187: return $this->oXMLTask;
188: }
189: }
190: