1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: pedro |
5
|
|
|
* Date: 29/11/16 |
6
|
|
|
* Time: 14:11 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Classes; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use Classes\Update\Content\GitHub; |
13
|
|
|
use Classes\Update\Version; |
14
|
|
|
|
15
|
|
|
require_once 'Update/Content/GitHub.php'; |
16
|
|
|
require_once 'Update/ProgressBar.php'; |
17
|
|
|
|
18
|
|
|
class Update |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
private static $fileName = "orm-generator"; |
22
|
|
|
private static $separador = "-"; |
23
|
|
|
private static $extencion = ".phar"; |
24
|
|
|
private $versionUpdate; |
25
|
|
|
private $tempFileName; |
26
|
|
|
/** |
27
|
|
|
* @type GitHub |
28
|
|
|
*/ |
29
|
|
|
private $objGitHub; |
30
|
|
|
|
31
|
|
|
public function __construct ( $version = null ) |
32
|
|
|
{ |
33
|
|
|
$this->objGitHub = GitHub::getInstance (); |
|
|
|
|
34
|
|
|
if ( is_null ( $version ) ) |
35
|
|
|
{ |
36
|
|
|
$version = $this->objGitHub->getLastVersion (); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$this->versionUpdate = $version; |
40
|
|
|
$this->tempFileName = self::$fileName |
41
|
|
|
. self::$separador |
42
|
|
|
. $this->versionUpdate |
43
|
|
|
. self::$extencion; |
44
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function update () |
48
|
|
|
{ |
49
|
|
|
if ( Version::HasNewVersion () && ! Version::equalVersion ( $this->versionUpdate ) |
50
|
|
|
) |
51
|
|
|
{ |
52
|
|
|
$content = $this->objGitHub->getContent ( $this->objGitHub->getLastPhar () , true ); |
53
|
|
|
if ( $content ) |
54
|
|
|
{ |
55
|
|
|
$this->objGitHub->putContent ( $this->tempFileName , $content ); |
56
|
|
|
} |
57
|
|
|
} else |
58
|
|
|
{ |
59
|
|
|
throw new \Exception ( "\033[0;31mError: Esta versão é a atual\033[0m\n" ); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function downloadVersion ( $version ) |
66
|
|
|
{ |
67
|
|
|
if ( Version::existVersion ( $version ) && ! Version::equalVersion ( $version ) ) |
68
|
|
|
{ |
69
|
|
|
$content = $this->objGitHub->getContent ( $this->objGitHub->getPharByVersion ( $version ) , true ); |
70
|
|
|
|
71
|
|
|
if ( $content ) |
72
|
|
|
{ |
73
|
|
|
$this->objGitHub->putContent ( $this->tempFileName , $content ); |
74
|
|
|
} |
75
|
|
|
} else |
76
|
|
|
{ |
77
|
|
|
if ( ! Version::existVersion ( $version ) ) |
78
|
|
|
{ |
79
|
|
|
throw new \Exception ( "\033[0;31mError: Esta versão não existe\033[0m\n" ); |
80
|
|
|
} |
81
|
|
|
throw new \Exception ( "\033[0;31mError: Esta versão é a atual\033[0m\n" ); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function modifyTempName () |
88
|
|
|
{ |
89
|
|
|
if ( file_exists ( realpath ( $this->tempFileName ) ) ) |
90
|
|
|
{ |
91
|
|
|
$fileName = realpath ( self::$fileName . self::$extencion ); |
92
|
|
|
if ( file_exists ( $fileName ) ) |
93
|
|
|
{ |
94
|
|
|
unlink ( $fileName ); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
chmod ( $this->tempFileName , 0777 ); |
98
|
|
|
rename ( $this->tempFileName , self::$fileName . self::$extencion ); |
99
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
} |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.