Skip to content

Commit 6fb70f1

Browse files
committed
mark as deprecated: \optimistex\deploy\ShellHelper; \optimistex\deploy\LogHelper
1 parent 17e88e7 commit 6fb70f1

File tree

4 files changed

+179
-107
lines changed

4 files changed

+179
-107
lines changed

DeployApplication.php

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: optimistex
5+
* Date: 18.10.17
6+
* Time: 0:49
7+
*/
8+
9+
namespace optimistex\deploy;
10+
11+
/**
12+
* Main class for deploying
13+
* Example for use it:
14+
*
15+
* ```php
16+
* (new DeployApplication('security_key'))->run();
17+
* ```
18+
*
19+
* @package optimistex\deploy
20+
*/
21+
class DeployApplication
22+
{
23+
/**
24+
* Private key for protection
25+
* @var string
26+
*/
27+
private $securityKey;
28+
29+
/**
30+
* Path to log file
31+
* @var string
32+
*/
33+
private $log_file;
34+
35+
/** @var boolean */
36+
private $hasAccess;
37+
38+
public function __construct($securityKey, $project_root = '.', $log_file = 'git-deploy-log.txt')
39+
{
40+
$this->securityKey = $securityKey;
41+
$this->log_file = getcwd() . '/' . $log_file;
42+
chdir($project_root);
43+
putenv('HOME=' . getcwd());
44+
}
45+
46+
/**
47+
* Fastest executing for typical cases
48+
* @param array $customCommands
49+
*/
50+
public function run(array $customCommands = [])
51+
{
52+
$this->begin();
53+
$this->execute($customCommands);
54+
$this->end();
55+
}
56+
57+
public function begin()
58+
{
59+
if ($this->checkSecurity()) {
60+
$this->log('SESSION START');
61+
}
62+
}
63+
64+
public function execute(array $customCommands = [])
65+
{
66+
if (!$this->checkSecurity()) {
67+
return;
68+
}
69+
if (empty($customCommands)) {
70+
$this->log($this->exec([
71+
'git branch',
72+
'git pull',
73+
]));
74+
} else {
75+
$this->log($this->exec($customCommands));
76+
}
77+
}
78+
79+
public function end()
80+
{
81+
if ($this->checkSecurity()) {
82+
$this->log('SESSION END');
83+
}
84+
if (file_exists($this->log_file)) {
85+
echo '<h1>LOG </h1><pre>';
86+
echo file_get_contents($this->log_file);
87+
echo '</pre>';
88+
} else {
89+
echo 'log not found';
90+
}
91+
}
92+
93+
94+
/**
95+
* Returning an absolute path to "php". It is useful, cause just "php" not working!
96+
* @return string
97+
*/
98+
public function php()
99+
{
100+
if (defined('PHP_BINDIR') && PHP_BINDIR) {
101+
return PHP_BINDIR . '/php';
102+
}
103+
if (defined('PHP_BINARY') && PHP_BINARY) {
104+
return PHP_BINARY . '/php';
105+
}
106+
if (defined('PHP_BINDER') && PHP_BINDER) {
107+
return PHP_BINDER . '/php';
108+
}
109+
return 'php';
110+
}
111+
112+
private function checkSecurity()
113+
{
114+
if ($this->hasAccess === null) {
115+
$this->hasAccess = false;
116+
if (isset($_GET['key']) && !empty($_GET['key'])) {
117+
if ($this->securityKey === $_GET['key']) {
118+
$this->log('ACCESS IS OBTAINED');
119+
$this->hasAccess = true;
120+
} else {
121+
$this->log(
122+
'DENY << ://' . ($_SERVER['HTTP_HOST'] ?? 'unknown-domain') . ($_SERVER['REQUEST_URI'] ?? '')
123+
);
124+
}
125+
}
126+
}
127+
return $this->hasAccess;
128+
}
129+
130+
private function exec(array $commands)
131+
{
132+
$res = "Executing shell commands:\n";
133+
foreach ($commands as $command) {
134+
$response = [];
135+
exec($command . ' 2>&1', $response, $error_code);
136+
if ($error_code > 0 && empty($response)) {
137+
$response = array('Error: ' . $error_code);
138+
}
139+
$response = implode("\n", $response);
140+
$res .= "$ $command \n{$response}\n" . ($response ? "\n" : '');
141+
}
142+
return $res;
143+
}
144+
145+
private function log($message)
146+
{
147+
if (empty($this->log_file)) {
148+
return;
149+
}
150+
151+
static $is_first = true;
152+
if ($is_first) {
153+
file_put_contents($this->log_file, "\n==============================\n", FILE_APPEND | LOCK_EX);
154+
$is_first = false;
155+
}
156+
157+
$datetime = date('Y.m.d H:i:s');
158+
file_put_contents($this->log_file, $datetime . "\t" . $message . "\n", FILE_APPEND | LOCK_EX);
159+
flush();
160+
}
161+
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "optimistex/git-auto-deploy-ex",
3-
"version": "1.1.0.1",
3+
"version": "1.1.0.2",
44
"type": "library",
55
"description": "The little project for auto-deploying projects to a hosting",
66
"keywords": [

shell_lib.php

Lines changed: 2 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* 'ls'
2020
* ]);
2121
* ```
22+
* @deprecated
2223
*/
2324
class ShellHelper
2425
{
@@ -64,6 +65,7 @@ public static function php()
6465
* LogHelper::init('log_file.txt');
6566
* LogHelper::log('message for logging');
6667
* ```
68+
* @deprecated
6769
*/
6870
class LogHelper
6971
{
@@ -91,105 +93,3 @@ public static function log($message)
9193
flush();
9294
}
9395
}
94-
95-
/**
96-
* Main class for deploying
97-
* Example for use it:
98-
*
99-
* ```php
100-
* (new DeployApplication('security_key'))->run();
101-
* ```
102-
*
103-
* @package optimistex\deploy
104-
*/
105-
class DeployApplication
106-
{
107-
/**
108-
* Private key for protection
109-
* @var string
110-
*/
111-
private $securityKey;
112-
113-
/**
114-
* Path to log file
115-
* @var string
116-
*/
117-
private $log_file;
118-
119-
/** @var boolean */
120-
private $hasAccess;
121-
122-
public function __construct($securityKey, $project_root = '.', $log_file = 'git-deploy-log.txt')
123-
{
124-
$this->securityKey = $securityKey;
125-
$this->log_file = getcwd() . '/' . $log_file;
126-
chdir($project_root);
127-
putenv("HOME=" . getcwd());
128-
LogHelper::init($this->log_file);
129-
}
130-
131-
/**
132-
* Fastest executing for typical cases
133-
* @param array $customCommands
134-
*/
135-
public function run(array $customCommands = [])
136-
{
137-
$this->begin();
138-
$this->execute($customCommands);
139-
$this->end();
140-
}
141-
142-
public function begin()
143-
{
144-
if ($this->checkSecurity()) {
145-
LogHelper::log('SESSION START');
146-
}
147-
}
148-
149-
public function execute(array $customCommands = [])
150-
{
151-
if (!$this->checkSecurity()) {
152-
return;
153-
}
154-
if (empty($customCommands)) {
155-
LogHelper::log(ShellHelper::exec([
156-
'git branch',
157-
'git pull',
158-
]));
159-
} else {
160-
LogHelper::log(ShellHelper::exec($customCommands));
161-
}
162-
}
163-
164-
public function end()
165-
{
166-
if ($this->checkSecurity()) {
167-
LogHelper::log('SESSION END');
168-
}
169-
if (file_exists($this->log_file)) {
170-
echo '<h1>LOG </h1><pre>';
171-
echo file_get_contents($this->log_file);
172-
echo '</pre>';
173-
} else {
174-
echo 'log not found';
175-
}
176-
}
177-
178-
private function checkSecurity()
179-
{
180-
if ($this->hasAccess === null) {
181-
$this->hasAccess = false;
182-
if (isset($_GET['key']) && !empty($_GET['key'])) {
183-
if ($this->securityKey === $_GET['key']) {
184-
LogHelper::log('ACCESS IS OBTAINED');
185-
$this->hasAccess = true;
186-
} else {
187-
LogHelper::log(
188-
'DENY << ://' . ($_SERVER['HTTP_HOST'] ?? 'unknown-domain') . ($_SERVER['REQUEST_URI'] ?? '')
189-
);
190-
}
191-
}
192-
}
193-
return $this->hasAccess;
194-
}
195-
}

tests/DeployApplicationTest.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function testKeyInvalid()
4646
$this->assertRegExp('/====+.+DENY.+test.domain/s', $log);
4747
}
4848

49-
public function testKeyValid1()
49+
public function testKeyValidDefault()
5050
{
5151
$_GET['key'] = '123';
5252
$_SERVER['HTTP_HOST'] = 'test.domain';
@@ -59,16 +59,27 @@ public function testKeyValid1()
5959
$this->assertRegExp('/.+ACCESS IS OBTAINED.+Executing shell commands.+\$ git branch.+\$ git pull/si', $log);
6060
}
6161

62-
public function testKeyValid2()
62+
public function testKeyValidCustom()
6363
{
6464
$_GET['key'] = '123';
6565
$_SERVER['HTTP_HOST'] = 'test.domain';
6666
$app = new DeployApplication('123', '.', $this->fileName);
6767

6868
$this->assertFileNotExists($this->fileName);
69-
$app->execute(['echo testing_echo']);
69+
$app->execute([
70+
'echo testing_echo',
71+
$app->php() . ' -v'
72+
]);
7073
$this->assertFileExists($this->fileName);
7174
$log = file_get_contents($this->fileName);
72-
$this->assertRegExp('/.+ACCESS IS OBTAINED.+Executing shell commands.+\$ echo testing_echo.+testing_echo/si', $log);
75+
$this->assertRegExp(
76+
'/.+ACCESS IS OBTAINED.+' .
77+
'Executing shell commands.+' .
78+
'\$ echo testing_echo.+' .
79+
'testing_echo.+' .
80+
'\$ .*php -v.+' .
81+
'php ' . PHP_VERSION . '.+/si',
82+
$log
83+
);
7384
}
7485
}

0 commit comments

Comments
 (0)