This repository has been archived by the owner on Nov 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcomposer-cmd.php
142 lines (121 loc) · 3.18 KB
/
composer-cmd.php
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
declare(strict_types=1);
use Treo\Composer\PostUpdate;
use Treo\Core\Utils\Util;
/**
* Class ComposerCmd
*
* @author r.ratsun@treolabs.com
*/
class ComposerCmd
{
const DIFF_PATH = 'data/composer-diff';
/**
* Before update
*/
public static function preUpdate(): void
{
if (class_exists(Util::class)) {
// delete diff cache
Util::removedir(self::DIFF_PATH);
}
}
/**
* After update
*/
public static function postUpdate(): void
{
// change directory
chdir(dirname(__FILE__));
// set the include_path
set_include_path(dirname(__FILE__));
// autoload
require_once 'vendor/autoload.php';
// run post update actions
(new PostUpdate())->run();
}
/**
* After package install
*
* @param mixed $event
*
* @return void
*/
public static function postPackageInstall($event): void
{
try {
$name = $event->getOperation()->getPackage()->getName();
} catch (\Throwable $e) {
}
if (isset($name)) {
self::createPackageActionFile($name, 'install');
}
}
/**
* @param mixed $event
*
* @return void
*/
public static function postPackageUpdate($event): void
{
// get composer update pretty line
$prettyLine = (string)$event->getOperation();
preg_match_all("/^Updating (.*) \((.*)\) to (.*) \((.*)\)$/", $prettyLine, $matches);
if (count($matches) == 5) {
self::createPackageActionFile($matches[1][0], 'update', $matches[2][0] . '_' . $matches[4][0]);
}
}
/**
* Before package uninstall
*
* @param mixed $event
*
* @return void
*/
public static function prePackageUninstall($event): void
{
try {
$name = $event->getOperation()->getPackage()->getName();
} catch (\Throwable $e) {
}
if (isset($name)) {
self::createPackageActionFile($name, 'delete');
}
}
/**
* @param string $name
* @param string $dir
* @param string $content
*
* @return bool
*/
protected static function createPackageActionFile(string $name, string $dir, string $content = ''): bool
{
// find composer.json file
$file = "vendor/$name/composer.json";
if (!file_exists($file)) {
return false;
}
// try to parse composer.json file
try {
$data = json_decode(file_get_contents($file), true);
} catch (\Throwable $e) {
return false;
}
// exit if is not treo package
if (!isset($data['extra']['treoId'])) {
return false;
}
// prepare dir path
$dirPath = self::DIFF_PATH . "/$dir";
// create dir if it needs
if (!file_exists($dirPath)) {
mkdir($dirPath, 0755, true);
}
// prepare content
$content = (empty($content)) ? $name : $name . '_' . $content;
// save
file_put_contents("$dirPath/{$data['extra']['treoId']}.txt", $content);
return true;
}
}