Skip to content
This repository was archived by the owner on Dec 1, 2020. It is now read-only.

Commit bbad8f6

Browse files
committed
Update built-ins against multiple PHP versions, incl. PHP 7.3.
This commit also reworks the Dockerfile to pull multiple versions of PHP and rebuild the syntax against each one.
1 parent ca9a285 commit bbad8f6

11 files changed

+431
-290
lines changed

Dockerfile

-19
This file was deleted.

README.md

+8-5
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,17 @@ Developing
105105

106106
When you install `php.vim` using your preferred installation method, all the needed files are already in place.
107107

108-
If you have a more recent version of PHP installed locally (with all of the PHP extensions you want/need), you may use the provided `Dockerfile` to rebuild the syntax file:
108+
If you wish to rebuild the syntax file with a more recent version of PHP available on the [Docker Hub], you should use the provided `Dockerfile` to do so:
109109

110110
```bash
111-
docker build -t stanangeloff/php.vim --no-cache --force-rm .
112-
docker run --rm -i -v "$PWD":/var/php -t stanangeloff/php.vim > /tmp/php.vim && cat /tmp/php.vim | sed 's/\x0D$//' > syntax/php.vim
113-
docker rmi stanangeloff/php.vim
111+
docker build --no-cache --force-rm -f attic/Dockerfile -t php.vim .
112+
cat syntax/php.vim | docker run --rm -i php.vim > syntax/php.vim.new
113+
docker rmi php.vim
114+
mv syntax/php.vim.new syntax/php.vim
114115
```
115116

116-
NOTE: The `sed` command strips out Windows line endings. If the updated syntax file fails to load and is corrupted, try loading `syntax/php.vim` in your favourite editor and ensure line endings are set to Unix.
117+
NOTE: If the updated syntax file fails to load and is corrupted, try loading `syntax/php.vim` in your favourite editor and ensure line endings are set to Unix `\n`.
118+
117119

118120
[php.vim-garvin]: https://github.com/vim-scripts/php.vim--Garvin
119121
[php.vim-original]: http://www.vim.org/scripts/script.php?script_id=2874
@@ -123,3 +125,4 @@ NOTE: The `sed` command strips out Windows line endings. If the updated syntax f
123125
[php.vim-source]: https://github.com/StanAngeloff/php.vim/blob/master/syntax/php.vim#L35
124126
[Blade]: https://github.com/jwalton512/vim-blade
125127
[syntax-groups]: https://github.com/StanAngeloff/php.vim/blob/41c36f7f/syntax/php.vim#L804
128+
[Docker Hub]: https://docs.docker.com/samples/library/php/

attic/0-bootstrap.inc.php

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
error_reporting(E_ALL | E_DEPRECATED | E_STRICT);
4+
ini_set('display_errors', 'On');
5+
6+
date_default_timezone_set('UTC');

attic/10-collect.php

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* Script to update Vim's PHP syntax file.
4+
*
5+
* @author Stan Angeloff <[email protected]>
6+
*
7+
* @author Paul Garvin <[email protected]>
8+
* @copyright Copyright 2009 Paul Garvin
9+
* @license http://www.opensource.org/licenses/mit-license.php MIT License
10+
*
11+
* Loosely based on Paul Garvin <[email protected]> original script.
12+
*
13+
* For the full copyright and license information,
14+
* please view the LICENSE file that was distributed with this source code.
15+
*/
16+
17+
require __DIR__ . '/0-bootstrap.inc.php';
18+
19+
# Parse the configuration file associated with this script.
20+
$configuration = parse_ini_file(__DIR__ . '/syntax.ini', /* $process_sections = */ true);
21+
22+
# Process extensions and serialize built-in functions, constants, classes and interfaces.
23+
$extensions = array();
24+
25+
foreach ($configuration['extensions'] as $extensionName => $isEnabled) {
26+
if (! $isEnabled) {
27+
continue;
28+
}
29+
30+
try {
31+
$reflect = new \ReflectionExtension($extensionName);
32+
33+
$collected = array(
34+
'name' => $reflect->getName(),
35+
'versions' => array(sprintf('%d.%d.%d', PHP_MAJOR_VERSION, PHP_MINOR_VERSION, PHP_RELEASE_VERSION)),
36+
'classes' => array(),
37+
'functions' => array_keys($reflect->getFunctions()),
38+
'constants' => array_diff(array_keys($reflect->getConstants()), array('TRUE', 'FALSE', 'NULL')),
39+
);
40+
41+
foreach ($reflect->getClasses() as $extensionClass) {
42+
$collected['classes'][] = $extensionClass->getName();
43+
$collected['constants'] = array_unique(array_merge($collected['constants'], array_keys($extensionClass->getConstants())));
44+
}
45+
46+
$extensions[$extensionName] = $collected;
47+
} catch (ReflectionException $e) {
48+
file_put_contents('php://stderr', sprintf('[ERROR] [PHP %d.%d] %\'.12s: %s.' . PHP_EOL, PHP_MAJOR_VERSION, PHP_MINOR_VERSION, $extensionName, rtrim($e->getMessage(), ' ?!.')));
49+
}
50+
}
51+
52+
echo serialize($extensions) . PHP_EOL;

attic/20-generate.php

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
require __DIR__ . '/0-bootstrap.inc.php';
4+
5+
$extensions = [];
6+
$versions = [];
7+
8+
while (false !== $line = fgets(STDIN)) {
9+
$unserialized = unserialize($line, ['allowed_classes' => false]);
10+
foreach ($unserialized as $extension => &$collected) {
11+
if (! isset($extensions[$extension])) {
12+
$extensions[$extension] = $collected;
13+
continue;
14+
}
15+
16+
$extensions[$extension] = array(
17+
'name' => $extensions[$extension]['name'],
18+
'versions' => array_merge($extensions[$extension]['versions'], $collected['versions']),
19+
'classes' => array_merge($extensions[$extension]['classes'], $collected['classes']),
20+
'functions' => array_merge($extensions[$extension]['functions'], $collected['functions']),
21+
'constants' => array_merge($extensions[$extension]['constants'], $collected['constants']),
22+
);
23+
}
24+
}
25+
26+
foreach ($extensions as &$collected) {
27+
$collected['classes'] = array_unique($collected['classes']);
28+
$collected['functions'] = array_unique($collected['functions']);
29+
$collected['constants'] = array_unique($collected['constants']);
30+
31+
sort($collected['classes'], SORT_NATURAL);
32+
sort($collected['functions'], SORT_NATURAL);
33+
sort($collected['constants'], SORT_NATURAL);
34+
35+
$versions = array_merge($versions, $collected['versions']);
36+
}
37+
38+
$versions = array_unique($versions);
39+
sort($versions, SORT_NATURAL);
40+
41+
$blocks = array(
42+
'extensions' => array(),
43+
'last-modified' => sprintf(
44+
'%s, PHP %s',
45+
date('r' /* RFC 2822 */),
46+
implode(/* $glue = */ ', ', $versions)
47+
),
48+
);
49+
50+
$blocks['extensions'][] = 'if ! exists("g:php_syntax_extensions_enabled")';
51+
$blocks['extensions'][] = sprintf(' let g:php_syntax_extensions_enabled = ["%s"]', implode(/* $glue = */ '", "', array_map('strtolower', array_keys($extensions))));
52+
$blocks['extensions'][] = 'endif';
53+
54+
$blocks['extensions'][] = 'if ! exists("g:php_syntax_extensions_disabled")';
55+
$blocks['extensions'][] = ' let g:php_syntax_extensions_disabled = []';
56+
$blocks['extensions'][] = 'endif';
57+
58+
$ifExtensionEnabled = function ($extensionName) {
59+
return sprintf(
60+
'if ' .
61+
'index(g:php_syntax_extensions_enabled, "%1$s") >= 0 && ' .
62+
'index(g:php_syntax_extensions_disabled, "%1$s") < 0 && ' .
63+
'( ! exists("b:php_syntax_extensions_enabled") || index(b:php_syntax_extensions_enabled, "%1$s") >= 0) && ' .
64+
'( ! exists("b:php_syntax_extensions_disabled") || index(b:php_syntax_extensions_disabled, "%1$s") < 0)',
65+
strtolower($extensionName)
66+
);
67+
};
68+
69+
$blocks['extensions'][] = 'syn case match';
70+
71+
foreach ($extensions as $extension) {
72+
if (! count($extension['constants'])) {
73+
continue;
74+
}
75+
76+
$blocks['extensions'][] = $ifExtensionEnabled($extension['name']);
77+
$blocks['extensions'][] = sprintf('" %s constants', $extension['name']);
78+
$blocks['extensions'][] = sprintf('syn keyword phpConstants %s contained', implode(/* $glue = */ ' ', $extension['constants']));
79+
$blocks['extensions'][] = 'endif';
80+
}
81+
82+
$blocks['extensions'][] = 'syn case ignore';
83+
84+
foreach ($extensions as $extension) {
85+
if (! count($extension['functions']) && ! count($extension['classes'])) {
86+
continue;
87+
}
88+
89+
$blocks['extensions'][] = $ifExtensionEnabled($extension['name']);
90+
91+
if (count($extension['functions'])) {
92+
$blocks['extensions'][] = sprintf('" %s functions', $extension['name']);
93+
$blocks['extensions'][] = sprintf('syn keyword phpFunctions %s contained', implode(/* $glue = */ ' ', $extension['functions']));
94+
}
95+
if (count($extension['classes'])) {
96+
$blocks['extensions'][] = sprintf('" %s classes and interfaces', $extension['name']);
97+
$blocks['extensions'][] = sprintf('syn keyword phpClasses %s contained', implode(/* $glue = */ ' ', $extension['classes']));
98+
}
99+
100+
$blocks['extensions'][] = 'endif';
101+
}
102+
103+
echo serialize($blocks);

attic/30-update.php

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
require __DIR__ . '/0-bootstrap.inc.php';
4+
5+
$blocks = unserialize(file_get_contents('php://stdin'), ['allowed_classes' => false]);
6+
7+
# Read the existing syntax file with block markers in it.
8+
#
9+
$template = file_get_contents($argv[1]);
10+
11+
# Clean up any previously defined blocks.
12+
$template = preg_replace(
13+
sprintf(
14+
'/(@block\([\'"](%s)[\'"]\)["\r\n\t ]*).*?(["\r\n\t ]*@endblock)\b/ism',
15+
implode(/* $glue = */ '|', array_keys($blocks))
16+
),
17+
"\\1___\\2___\\3",
18+
$template
19+
);
20+
21+
# Update block contents in the template.
22+
foreach ($blocks as $blockName => $lines) {
23+
$template = str_ireplace(
24+
sprintf('___%s___', $blockName),
25+
rtrim(is_array($lines) ? implode(/* $glue = */ "\n", $lines) : $lines),
26+
$template
27+
);
28+
}
29+
30+
$template = preg_replace_callback(
31+
'/
32+
(?<begin>"\s*@copy\s+(?<copy>[a-zA-Z0-9_]+)(?<processors>(\s+(strip_maximum_size))+)?)
33+
(?<script>.*?)
34+
(?<end>"\s*@end\s+([a-zA-Z0-9_]+))
35+
/sx',
36+
function (array $groups) use ($template) {
37+
$copy = preg_quote($groups['copy'], /* $delimiter = */ '/');
38+
39+
$processors = array_filter(array_map('trim', preg_split('{[;, ]+}', $groups['processors'])));
40+
41+
preg_match("/
42+
\"\\s*@begin\\s+{$copy}\b
43+
(?<script>.*?)
44+
\"\\s*@end\\s+{$copy}\b
45+
/sx", $template, $captures);
46+
47+
if (! isset($captures['script'])) {
48+
file_put_contents('php://stderr', "[ERROR] The block referenced by '{$groups['begin']}' was not found." . PHP_EOL);
49+
50+
return $groups['begin'] . $groups['script'] . $groups['end'];
51+
}
52+
53+
$script = $captures['script'];
54+
55+
foreach ($processors as $processor) {
56+
switch ($processor) {
57+
case 'strip_maximum_size':
58+
$script = preg_replace('{\\\\@\d+}', '\@', $script);
59+
break;
60+
61+
default:
62+
file_put_contents('php://stderr', "[ERROR] The processor \"{$processor}\" is not supported, found in '{$groups['begin']}'." . PHP_EOL);
63+
}
64+
}
65+
66+
return $groups['begin'] . $script . $groups['end'];
67+
},
68+
$template
69+
);
70+
71+
echo $template;

0 commit comments

Comments
 (0)