Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
Release.php
1 <?php
2 /**
3  * Gentics Portal.Node PHP
4  * Author & Copyright (c) by Gentics Software GmbH
5  * sales@gentics.com
6  * http://www.gentics.com
7  * Licenses can be found in the LICENSE.txt file in the root-folder of this installation
8  * You must not use this software without a valid license agreement.
9  *
10  */
11 class Release extends CComponent
12 {
13  //folder where release will be saved
14  public static $temporaryDir = '/tmp';
15 
16  private $_zipLocation;
17 
18  private $_currentVersion;
19 
20  private $_invitationMessage;
21 
22  private $_finishMessage;
23 
24  private $_zipArchive;
25 
26  /**
27  * Constructor
28  *
29  * @param string $zipLocation location of release zip archive
30  */
31  public function __construct($zipLocation)
32  {
33  $this->_zipLocation = $zipLocation;
34  }
35 
36  /**
37  * Public getter
38  *
39  * @return string
40  */
41  public function getZipLocation()
42  {
43  return $this->_zipLocation;
44  }
45 
46  /**
47  * Open zip archive
48  *
49  * @throws Exception
50  *
51  * @return mixed
52  */
53  private function _getZipArchive()
54  {
55  if ($this->_zipArchive === null) {
56  $this->_zipArchive = new ZipArchive();
57  if (($errorCode = $this->_zipArchive->open($this->_zipLocation, ZIPARCHIVE::CHECKCONS)) !== true) {
58  throw new Exception("Invalid zip file. Error: $errorCode");
59  }
60  }
61 
62  return $this->_zipArchive;
63  }
64 
65  /**
66  * Retrieve release version from archive
67  *
68  * @return mixed
69  */
70  public function getVersion()
71  {
72  if ($this->_currentVersion === null) {
73  $zip = $this->_getZipArchive();
74  $tmpFile = Yii::getPathOfAlias('common.runtime.release_main_config');
75  file_put_contents($tmpFile, $zip->getFromName('frontend/config/main.php'));
76  $updatesConfig = include $tmpFile;
77  $this->_currentVersion = $updatesConfig['modules']['updates']['currentVersion'];
78  unlink($tmpFile);
79  }
80  return $this->_currentVersion;
81  }
82 
83  /**
84  * Retrieve invitation message from archive
85  *
86  * @return mixed
87  */
88  public function getInvitationMessage()
89  {
90  if ($this->_invitationMessage === null) {
91  $zip = $this->_getZipArchive();
92  $this->_invitationMessage = $zip->getFromName('common/runtime/invitationMessage');
93  }
94 
95  return $this->_invitationMessage;
96  }
97 
98  /**
99  * Retrieve finish message from archive
100  *
101  * @return mixed
102  */
103  public function getFinishMessage()
104  {
105  if ($this->_finishMessage === null) {
106  $zip = $this->_getZipArchive();
107  $this->_finishMessage = $zip->getFromName('common/runtime/finishMessage');
108  }
109 
110  return $this->_finishMessage;
111  }
112 
113 
114  /**
115  * Update project placed in $destination folder from release archive
116  *
117  * @param string $destination destination folder
118  *
119  * @return void
120  */
121  public function update($destination)
122  {
123  ZipHelper::extract($this->getZipLocation(), realpath($destination));
124  unlink($destination . '/common/runtime/invitationMessage');
125  unlink($destination . '/common/runtime/finishMessage');
126  }
127 
128  /**
129  * Create release of current project. Compress all necessary data into archive
130  *
131  * @param string $location release folder
132  * @param array $exclude files/folder which need to be excluded
133  * @param string $invitationMessage message which will be displayed before update process
134  * @param string $finishMessage message which will be displayed after update process
135  *
136  * @return Release
137  */
138  public static function make($location, $exclude = array(), $invitationMessage = '', $finishMessage = '')
139  {
140  $zipPath = self::$temporaryDir . '/' . self::releaseName();
141  @unlink($zipPath);
143  array(
144  array(
145  'path' => $location,
146  'exclude' => $exclude
147  ),
148  'common/runtime/invitationMessage' => $invitationMessage,
149  'common/runtime/finishMessage' => $finishMessage
150  ), $zipPath
151  );
152 
153  return new Release($zipPath);
154  }
155 
156 
157  /**
158  * Returns release name
159  *
160  * @static
161  *
162  * @return string
163  */
164  public static function releaseName()
165  {
166  $date = date('Y_m_d');
167  return "release_{$date}_v" . Yii::app()->getModule('updates')->currentVersion . ".zip";
168  }
169 
170  /**
171  * Runs migrate command
172  *
173  * @static
174  *
175  * @return void
176  */
177  public static function migrate()
178  {
179  //Yii::import('system.cli.commands.MigrateCommand');
180  //$command = new MigrateCommand('', new CConsoleCommandRunner());
181  //$command->actionUp(array());
182  }
183 
184 
185  /**
186  * Backup project folder
187  *
188  * @param string $destination folder where backup will be saved
189  *
190  * @return bool
191  */
192  public static function backupProject($destination = '/tmp')
193  {
194  $date = date('Y-m-d');
195  $zipPath = $destination . '/' . "gentics_portal_node_php_{$date}_v" . Yii::app()->getModule('updates')->currentVersion . '.bak.zip';
196  @unlink($zipPath);
197  $dbDumper = new DbDumper();
198  $dump = $dbDumper->getDump();
200  array(
201  array(
202  'path' => Yii::getPathOfAlias('site'),
203  'exclude' => array(
204  'common/runtime',
205  'custom/runtime',
206  'frontend/runtime',
207  'frontend/www/assets'
208  )
209  ),
210  "db_dump_$date.sql" => $dump
211  ),
212  $zipPath
213  );
214 
215  return $zipPath;
216  }
217 }