-
Notifications
You must be signed in to change notification settings - Fork 567
Expand file tree
/
Copy pathComposerController.php
More file actions
107 lines (95 loc) · 3.47 KB
/
ComposerController.php
File metadata and controls
107 lines (95 loc) · 3.47 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
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since DebugKit 3.6.1
* @license https://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace DebugKit\Controller;
use Cake\View\JsonView;
use Composer\Console\Application;
use RuntimeException;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
/**
* Provides utility features need by the toolbar.
*/
class ComposerController extends DebugKitController
{
/**
* @inheritDoc
*/
public function initialize(): void
{
parent::initialize();
$this->viewBuilder()->setClassName(JsonView::class);
}
/**
* Check outdated composer dependencies
*
* @return void
* @throws \RuntimeException
*/
public function checkDependencies(): void
{
$this->request->allowMethod('post');
$input = new ArrayInput([
'command' => 'outdated',
'--no-interaction' => true,
'--direct' => filter_var($this->request->getData('direct'), FILTER_VALIDATE_BOOLEAN),
]);
$output = $this->executeComposerCommand($input);
$dependencies = array_filter(explode("\n", $output->fetch()));
$packages = [];
foreach ($dependencies as $dependency) {
if (str_contains($dependency, 'php_network_getaddresses')) {
throw new RuntimeException('You have to be connected to the internet');
}
if (str_contains($dependency, '<highlight>')) {
$packages['semverCompatible'][] = $dependency;
continue;
}
$packages['bcBreaks'][] = $dependency;
}
if (!empty($packages['semverCompatible'])) {
$packages['semverCompatible'] = trim(implode("\n", $packages['semverCompatible']));
}
if (!empty($packages['bcBreaks'])) {
$packages['bcBreaks'] = trim(implode("\n", $packages['bcBreaks']));
}
$this->viewBuilder()->setOption('serialize', ['packages']);
$this->set('packages', $packages);
}
/**
* @param \Symfony\Component\Console\Input\ArrayInput $input An array describing the command input
* @return \Symfony\Component\Console\Output\BufferedOutput Aa Console command buffered result
*/
private function executeComposerCommand(ArrayInput $input): BufferedOutput
{
$bin = implode(DIRECTORY_SEPARATOR, [ROOT, 'vendor', 'bin', 'composer']);
putenv('COMPOSER_HOME=' . $bin);
putenv('COMPOSER_CACHE_DIR=' . CACHE);
$dir = (string)getcwd();
chdir(ROOT);
$timeLimit = ini_get('max_execution_time');
set_time_limit(300);
$memoryLimit = ini_get('memory_limit');
ini_set('memory_limit', '512M');
$output = new BufferedOutput();
$application = new Application();
$application->setAutoExit(false);
$application->run($input, $output);
// Restore environment
chdir($dir);
set_time_limit((int)$timeLimit);
ini_set('memory_limit', $memoryLimit);
return $output;
}
}