forked from stephenr85/kentico-cloud-sample-app-php
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Installer.php
98 lines (87 loc) · 3.04 KB
/
Installer.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
<?php
/**
* When serving Kontent.ai PHP Sample Application using `php artisan serve`
* command, the combination of Laravel (5.3+) and of a package
* kub-at/php-simple-html-dom-parser exhibits an issue where laravel gets stuck
* in an infinite loop of calls.
*/
define("PACKAGE_TO_FIX" , "kub-at/php-simple-html-dom-parser");
define("MINIMUM_REQUIRED_LINES_IN_FILE", 1000);
/**
* Recursively retrieves nested files with .php extension from given directory.
*/
function getAllPhpFiles($dir, &$results = array())
{
$files = scandir($dir);
foreach ($files as $key => $value)
{
$path = realpath($dir . DIRECTORY_SEPARATOR . $value);
if (!is_dir($path))
{
$fileInfo = pathinfo($path);
if ($fileInfo['extension'] == 'php')
{
$results[] = $path;
}
}
elseif ($value != "." && $value != "..")
{
getAllPhpFiles($path, $results);
}
}
return $results;
}
/**
* Counts how many actual lines of code there are in the specified file.
*/
function countFileLines($file)
{
$fileAPI = new \SplFileObject($file, 'r');
$fileAPI->seek(PHP_INT_MAX);
return $fileAPI->key() + 1;
}
/**
* Searches file for occurrence of 'function __destruct' and replaces it with
* improbably manually selectable name. This prevents combination of Laravel
* and kub-at/php-simple-html-dom-parser-package to become stuck in an endless
* loop of __destruct calls by renaming __destruct function.
*/
function removeDestructorFunction($file)
{
try {
$fc = file_get_contents($file);
if (empty($fc)){
showErrorFixingMessage();
return;
}
$fc = str_replace('function __destruct', 'function __m3TlMNSgdA__CS2yxVRIAO_destructor_removed', $fc);
file_put_contents($file, $fc);
} catch (Exception $e) {
showErrorFixingMessage();
}
}
function showErrorFixingMessage()
{
echo "=====================================================================";
echo "ERROR: Fixing kub-at/php-simple-html-dom-parser package. " . PHP_EOL;
echo "Please manually modify file: " . PHP_EOL;
echo "$file" . PHP_EOL ;
echo "And rename all occurences of __destruct() function." . PHP_EOL;
echo "=====================================================================";
}
echo "Installer will now remove destructors from kub-AT/php-simple-html-dom-parser package." . PHP_EOL;
foreach (getAllPhpFiles('vendor' . DIRECTORY_SEPARATOR . PACKAGE_TO_FIX) as $file)
{
// Lets base this on reasonable thought that HTML parser will never fit
// into less than 1000 lines of code and ignore all files that contain
// less lines of code. Note that this approach assumes very specific
// kub-at/php-simple-html-dom-parser package structure and will not work
// out of the box for other packages.
if (countFileLines($file) > MINIMUM_REQUIRED_LINES_IN_FILE)
{
echo "Updating: " . basename($file) . PHP_EOL;
removeDestructorFunction($file);
}
}
echo "Done."
?>