PageRenderTime 883ms CodeModel.GetById 23ms RepoModel.GetById 2ms app.codeStats 0ms

/lib/plugins/sfSympalUpgradePlugin/lib/sfSympalUpgradeFromWeb.class.php

https://github.com/slemoigne/sympal
PHP | 92 lines | 74 code | 18 blank | 0 comment | 3 complexity | 668f280ed6ecd261c1d67ce04d09ffdb MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. class sfSympalUpgradeFromWeb extends sfSympalProjectUpgrade
  3. {
  4. private
  5. $_currentVersion,
  6. $_latestVersion,
  7. $_filesystem;
  8. public function __construct(ProjectConfiguration $configuration, sfEventDispatcher $dispatcher, sfFormatter $formatter)
  9. {
  10. parent::__construct($configuration, $dispatcher, $formatter);
  11. $this->_filesystem = new sfFilesystem($dispatcher, $formatter);
  12. $this->_currentVersion = sfSympalConfig::getCurrentVersion();;
  13. }
  14. public function hasNewVersion()
  15. {
  16. return version_compare($this->getLatestVersion(), $this->_currentVersion, '>');
  17. }
  18. public function getCurrentVersion()
  19. {
  20. return sfSympalConfig::getCurrentVersion();
  21. }
  22. protected function _urlExists($url)
  23. {
  24. $ch = curl_init();
  25. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  26. curl_setopt($ch, CURLOPT_URL, $url);
  27. $result = curl_exec($ch);
  28. $info = curl_getinfo($ch);
  29. curl_close($ch);
  30. return ($info['http_code'] == 404 ? false : true);
  31. }
  32. public function getUpgradeCommands()
  33. {
  34. $pluginDir = $this->_configuration->getPluginConfiguration('sfSympalPlugin')->getRootDir();
  35. $backupDir = dirname($pluginDir);
  36. $url = sprintf('http://svn.symfony-project.org/plugins/sfSympalPlugin/tags/%s', $this->getLatestVersion());
  37. if ($this->_urlExists($url) === false)
  38. {
  39. $url = 'http://svn.symfony-project.org/plugins/sfSympalPlugin/trunk';
  40. }
  41. $commands = array();
  42. $commands['cd'] = sprintf('cd %s', dirname($pluginDir));
  43. $commands['backup'] = sprintf('mv sfSympalPlugin %s/sfSympalPlugin_%s', $backupDir, $this->_currentVersion);
  44. $commands['download'] = sprintf('svn co %s %s', $url, $pluginDir);
  45. return $commands;
  46. }
  47. public function download()
  48. {
  49. $this->logSection('sympal', 'Updating Sympal code...');
  50. $commands = $this->getUpgradeCommands();
  51. try {
  52. $result = $this->_filesystem->execute(implode('; ', $commands));
  53. } catch (Exception $e) {
  54. throw new sfException('A problem occurred updating Sympal code.');
  55. }
  56. $this->logSection('sympal', 'Sympal code updated successfully...');
  57. }
  58. protected function _doUpgrade()
  59. {
  60. $this->_runUpgrades();
  61. sfSympalConfig::writeSetting('current_version', $this->getLatestVersion());
  62. }
  63. public function getLatestVersion()
  64. {
  65. if (!$this->_latestVersion)
  66. {
  67. $this->logSection('sympal', 'Checking for new version of Sympal!');
  68. $code = file_get_contents('http://svn.symfony-project.org/plugins/sfSympalPlugin/trunk/config/sfSympalPluginConfiguration.class.php');
  69. preg_match_all("/const VERSION = '(.*)';/", $code, $matches);
  70. $this->_latestVersion = $matches[1][0];
  71. }
  72. return $this->_latestVersion;
  73. }
  74. }