partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

VersionService.php (4210B)


      1 <?php
      2 
      3 namespace PartKeepr\CoreBundle\Services;
      4 
      5 use PartKeepr\CoreBundle\PartKeeprVersion;
      6 use PartKeepr\RemoteFileLoader\RemoteFileLoaderFactory;
      7 use Symfony\Component\Translation\TranslatorInterface;
      8 
      9 class VersionService
     10 {
     11     /**
     12      * @var SystemNoticeService
     13      */
     14     private $systemNoticeService;
     15 
     16     /**
     17      * @var string
     18      */
     19     private $versionURI = 'http://www.partkeepr.org/versions.json';
     20 
     21     /**
     22      * @var TranslatorInterface
     23      */
     24     private $translator;
     25 
     26     /**
     27      * @var string
     28      */
     29     private $version;
     30 
     31     /**
     32      * @var RemoteFileLoaderFactory
     33      */
     34     private $remoteFileLoader;
     35 
     36     public function __construct(SystemNoticeService $systemNoticeService, TranslatorInterface $translator, RemoteFileLoaderFactory $remoteFileLoader)
     37     {
     38         $this->systemNoticeService = $systemNoticeService;
     39         $this->translator = $translator;
     40         $this->remoteFileLoader = $remoteFileLoader;
     41 
     42         $this->setVersion(PartKeeprVersion::PARTKEEPR_VERSION);
     43     }
     44 
     45     /**
     46      * Extracts the current commit from GIT.
     47      *
     48      * @return string
     49      */
     50     public function extractGITCommit()
     51     {
     52         $result = shell_exec("git rev-parse HEAD");
     53 
     54         return trim($result);
     55     }
     56 
     57     /**
     58      * Extracts the current short commit from GIT.
     59      *
     60      * @return string
     61      */
     62     public function extractShortGITCommit()
     63     {
     64         $result = shell_exec("git rev-parse --short HEAD");
     65 
     66         return trim($result);
     67     }
     68 
     69     /**
     70      * Sets the version string.
     71      *
     72      * @param $version string The version
     73      */
     74     public function setVersion($version)
     75     {
     76         $this->version = $version;
     77     }
     78 
     79     /**
     80      * Returns the current version string.
     81      *
     82      * @return string The version
     83      */
     84     public function getVersion()
     85     {
     86         return $this->version;
     87     }
     88 
     89     public function setVersionURI($versionURI)
     90     {
     91         $this->versionURI = $versionURI;
     92     }
     93 
     94     public function getCanonicalVersion()
     95     {
     96         if ($this->getVersion() === '{V_GIT}') {
     97             return 'GIT development version Commit '.$this->extractGITCommit()." Short Commit ".$this->extractShortGITCommit();
     98         } else {
     99             return $this->getVersion();
    100         }
    101     }
    102 
    103     /**
    104      * Checks against the versions at partkeepr.org.
    105      *
    106      * If a newer version was found, create a system notice entry.
    107      */
    108     public function doVersionCheck()
    109     {
    110         if ($this->getVersion() === '{V_GIT}') {
    111             return;
    112         }
    113 
    114         if (substr($this->getVersion(), 0, 17) === 'partkeepr-nightly') {
    115             return;
    116         }
    117 
    118         $latestVersion = $this->getLatestVersion();
    119 
    120         if ($latestVersion === false) {
    121             return;
    122         }
    123 
    124         if (version_compare($this->getVersion(), $latestVersion['version'], '<')) {
    125             $this->systemNoticeService->createUniqueSystemNotice(
    126                 'PARTKEEPR_VERSION_'.$latestVersion['version'],
    127                 $this->translator->trans(
    128                     'New PartKeepr Version %version% available',
    129                     ['%version%' => $latestVersion['version']]
    130                 ),
    131                 $this->translator->trans('PartKeepr Version %version% changelog:', [
    132                     '%version%' => $latestVersion['version']."\n\n".
    133                         $latestVersion['changelog'],
    134                 ])
    135             );
    136         }
    137     }
    138 
    139     /**
    140      * Returns the latest version information from partkeepr.org.
    141      *
    142      * @return array|bool
    143      */
    144     public function getLatestVersion()
    145     {
    146         $data = $this->remoteFileLoader->createLoader()->load($this->versionURI);
    147         $versions = json_decode($data, true);
    148 
    149         if (!is_array($versions)) {
    150             return false;
    151         }
    152 
    153         $latestVersionEntry = $versions[0];
    154 
    155         if (!array_key_exists('version', $latestVersionEntry)) {
    156             return false;
    157         }
    158 
    159         if (!array_key_exists('changelog', $latestVersionEntry)) {
    160             return ['version' => $latestVersionEntry['version'], 'changelog' => ''];
    161         } else {
    162             return ['version' => $latestVersionEntry['version'], 'changelog' => $latestVersionEntry['changelog']];
    163         }
    164     }
    165 }