PageRenderTime 39ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/sympal/sympal
PHP | 113 lines | 77 code | 18 blank | 18 comment | 3 complexity | e75ae45df9440e4e11f068399d4ae20a MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * Upgrade class handling the download
  4. *
  5. * @package sfSympalUpgradePlugin
  6. * @subpackage task
  7. * @author Jonathan H. Wage <jonwage@gmail.com>
  8. * @author Ryan Weaver <ryan.weaver@iostudio.com>
  9. * @since 2010-03-26
  10. * @version svn:$Id$ $Author$
  11. */
  12. class sfSympalUpgradeFromWeb extends sfSympalProjectUpgrade
  13. {
  14. private
  15. $_currentVersion,
  16. $_latestVersion,
  17. $_filesystem;
  18. public function __construct(ProjectConfiguration $configuration, sfEventDispatcher $dispatcher, sfFormatter $formatter)
  19. {
  20. parent::__construct($configuration, $dispatcher, $formatter);
  21. $this->_filesystem = new sfFilesystem($dispatcher, $formatter);
  22. $this->_currentVersion = sfSympalConfig::getCurrentVersion();;
  23. }
  24. public function hasNewVersion()
  25. {
  26. return version_compare($this->getLatestVersion(), $this->_currentVersion, '>');
  27. }
  28. public function getCurrentVersion()
  29. {
  30. return sfSympalConfig::getCurrentVersion();
  31. }
  32. protected function _urlExists($url)
  33. {
  34. $ch = curl_init();
  35. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  36. curl_setopt($ch, CURLOPT_URL, $url);
  37. $result = curl_exec($ch);
  38. $info = curl_getinfo($ch);
  39. curl_close($ch);
  40. return ($info['http_code'] == 404 ? false : true);
  41. }
  42. public function getUpgradeCommands()
  43. {
  44. $pluginDir = $this->_configuration->getPluginConfiguration('sfSympalPlugin')->getRootDir();
  45. $backupDir = dirname($pluginDir);
  46. $url = sprintf('http://svn.symfony-project.org/plugins/sfSympalPlugin/tags/%s', $this->getLatestVersion());
  47. if ($this->_urlExists($url) === false)
  48. {
  49. $url = 'http://svn.symfony-project.org/plugins/sfSympalPlugin/trunk';
  50. }
  51. $commands = array();
  52. $commands['cd'] = sprintf('cd %s', dirname($pluginDir));
  53. $commands['backup'] = sprintf('mv sfSympalPlugin %s/sfSympalPlugin_%s', $backupDir, $this->_currentVersion);
  54. $commands['download'] = sprintf('svn co %s %s', $url, $pluginDir);
  55. return $commands;
  56. }
  57. /**
  58. * Downloads and unpackages the new code
  59. *
  60. * This should be run before upgrade() (else upgrade won't run the new code).
  61. */
  62. public function download()
  63. {
  64. $this->logSection('sympal', 'Downloading and installing Sympal code...');
  65. $commands = $this->getUpgradeCommands();
  66. try
  67. {
  68. $result = $this->_filesystem->execute(implode('; ', $commands));
  69. }
  70. catch (Exception $e)
  71. {
  72. throw new sfException('A problem occurred updating the Sympal code: ' . (string) $e);
  73. }
  74. $this->logSection('sympal', 'Sympal code updated successfully...');
  75. }
  76. /**
  77. * Runs the upgrade and then writes the latest version to config
  78. */
  79. protected function _doUpgrade()
  80. {
  81. parent::_doUpgrade();
  82. sfSympalConfig::writeSetting('current_version', $this->getLatestVersion());
  83. }
  84. public function getLatestVersion()
  85. {
  86. if (!$this->_latestVersion)
  87. {
  88. $this->logSection('sympal', 'Checking for new version of Sympal!');
  89. $code = file_get_contents('http://svn.symfony-project.org/plugins/sfSympalPlugin/trunk/config/sfSympalPluginConfiguration.class.php');
  90. preg_match_all("/const VERSION = '(.*)';/", $code, $matches);
  91. $this->_latestVersion = $matches[1][0];
  92. }
  93. return $this->_latestVersion;
  94. }
  95. }