forked from php-censor/php-censor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildInterpolator.php
More file actions
114 lines (100 loc) · 4.5 KB
/
Copy pathBuildInterpolator.php
File metadata and controls
114 lines (100 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
declare(strict_types=1);
namespace PHPCensor\Helper;
use Exception;
use PHPCensor\Model\Build as BaseBuild;
use PHPCensor\Store\EnvironmentStore;
use PHPCensor\Store\Factory;
/**
* The BuildInterpolator class replaces variables in a string with build-specific information.
*/
class BuildInterpolator
{
/**
* An array of key => value pairs that will be used for
* interpolation and environment variables
*
* @var mixed[]
*
* @see setupInterpolationVars()
*/
protected $interpolationVars = [];
/**
* Sets the variables that will be used for interpolation.
*
* @param BaseBuild $build
* @param $url
*
* @throws Exception
*/
public function setupInterpolationVars(BaseBuild $build, $url)
{
$this->interpolationVars = [];
$this->interpolationVars['%COMMIT_ID%'] = $build->getCommitId();
$this->interpolationVars['%SHORT_COMMIT_ID%'] = \substr($build->getCommitId(), 0, 7);
$this->interpolationVars['%PROJECT_ID%'] = $build->getProjectId();
$this->interpolationVars['%BUILD_ID%'] = $build->getId();
$this->interpolationVars['%COMMITTER_EMAIL%'] = $build->getCommitterEmail();
$this->interpolationVars['%COMMIT_MESSAGE%'] = $build->getCommitMessage();
$this->interpolationVars['%COMMIT_LINK%'] = $build->getCommitLink();
$this->interpolationVars['%PROJECT_TITLE%'] = $build->getProjectTitle();
$this->interpolationVars['%PROJECT_LINK%'] = $url . 'project/view/' . $build->getProjectId();
$this->interpolationVars['%BUILD_PATH%'] = $build->getBuildPath();
$this->interpolationVars['%BUILD_LINK%'] = $url . 'build/view/' . $build->getId();
$this->interpolationVars['%BRANCH%'] = $build->getBranch();
$this->interpolationVars['%BRANCH_LINK%'] = $build->getBranchLink();
$environmentId = $build->getEnvironmentId();
$environment = null;
if ($environmentId) {
/** @var EnvironmentStore $environmentStore */
$environmentStore = Factory::getStore('Environment');
$environmentObject = $environmentStore->getById($environmentId);
if ($environmentObject) {
$environment = $environmentObject->getName();
}
}
$this->interpolationVars['%ENVIRONMENT%'] = $environment;
\putenv('PHP_CENSOR=1');
\putenv('PHP_CENSOR_COMMIT_ID=' . $this->interpolationVars['%COMMIT_ID%']);
\putenv('PHP_CENSOR_SHORT_COMMIT_ID=' . $this->interpolationVars['%SHORT_COMMIT_ID%']);
\putenv('PHP_CENSOR_COMMITTER_EMAIL=' . $this->interpolationVars['%COMMITTER_EMAIL%']);
\putenv('PHP_CENSOR_COMMIT_MESSAGE=' . $this->interpolationVars['%COMMIT_MESSAGE%']);
\putenv('PHP_CENSOR_COMMIT_LINK=' . $this->interpolationVars['%COMMIT_LINK%']);
\putenv('PHP_CENSOR_PROJECT_ID=' . $this->interpolationVars['%PROJECT_ID%']);
\putenv('PHP_CENSOR_PROJECT_TITLE=' . $this->interpolationVars['%PROJECT_TITLE%']);
\putenv('PHP_CENSOR_PROJECT_LINK=' . $this->interpolationVars['%PROJECT_LINK%']);
\putenv('PHP_CENSOR_BUILD_ID=' . $this->interpolationVars['%BUILD_ID%']);
\putenv('PHP_CENSOR_BUILD_PATH=' . $this->interpolationVars['%BUILD_PATH%']);
\putenv('PHP_CENSOR_BUILD_LINK=' . $this->interpolationVars['%BUILD_LINK%']);
\putenv('PHP_CENSOR_BRANCH=' . $this->interpolationVars['%BRANCH%']);
\putenv('PHP_CENSOR_BRANCH_LINK=' . $this->interpolationVars['%BRANCH_LINK%']);
\putenv('PHP_CENSOR_ENVIRONMENT=' . $this->interpolationVars['%ENVIRONMENT%']);
}
/**
* @param string $input
*
* @return string
*/
private function realtimeInterpolate($input)
{
$input = \str_replace('%CURRENT_DATE%', \date('Y-m-d'), $input);
$input = \str_replace('%CURRENT_TIME%', \date('H-i-s'), $input);
$input = \str_replace('%CURRENT_DATETIME%', \date('Y-m-d_H-i-s'), $input);
return $input;
}
/**
* Replace every occurrence of the interpolation vars in the given string
* Example: "This is build %BUILD_ID%" => "This is build 182"
*
* @param string $input
*
* @return string
*/
public function interpolate($input)
{
$input = $this->realtimeInterpolate($input);
$keys = \array_keys($this->interpolationVars);
$values = \array_values($this->interpolationVars);
return \str_replace($keys, $values, $input);
}
}