aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/develop/namespacify.php
blob: 447e87905e241f6bd4188d3ab534c45ba05e7463 (plain)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/

//
// Security message:
//
// This script is potentially dangerous.
// Remove or comment the next line (die(".... ) to enable this script.
// Do NOT FORGET to either remove this script or disable it after you have used it.
//
die("Please read the first lines of this script for instructions on how to enable it");

$namespace_dir = realpath(__DIR__ . '/../phpbb/');
$code_dir = realpath(__DIR__ . '/../');
$test_dir = realpath(__DIR__ . '/../../tests/');
$config_dir = realpath(__DIR__ . '/../config/');

function map_class_name($old_class_name, $code_dir)
{
	$parts = explode('_', $old_class_name);
	$cur_dir = array();
	$cur_name = array();
	$in_name = false;
	foreach ($parts as $i => $part)
	{
		if (empty($part))
		{
			return null;
		}

		if (!$in_name)
		{
			$new_dir = array_merge($cur_dir, array($part));
			$path = $code_dir . '/' . implode('/', $new_dir);

			if (file_exists($path) && is_dir($path))
			{
				$cur_dir = $new_dir;
			}
			else
			{
				$in_name = true;
				$cur_name[] = $part;
			}
		}
		else
		{
			$cur_name[] = $part;
		}
	}

	if (empty($cur_name) && !empty($cur_dir))
	{
		$cur_name[] = $cur_dir[count($cur_dir) - 1];
	}

	if (file_exists($code_dir . '/' . implode('/', $cur_dir) . '/' . implode('_', $cur_name) . '.php'))
	{
		return implode('\\', $cur_dir) . '\\' . implode('_', $cur_name);
	}

	return null;
}

$iterator = new \AppendIterator();
$iterator->append(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($code_dir)));
$iterator->append(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($test_dir)));
$iterator->append(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($config_dir)));

foreach ($iterator as $file)
{
	if (substr($file->getPath(), 0, 6) === 'vendor')
	{
		continue;
	}

	if ($file->getExtension() == 'php')
	{
		$code = file_get_contents($file->getPathname());
		$namespaced_file = false;

		if (preg_match('#^' . preg_quote($namespace_dir, '#') . '#', $file->getPath()))
		{
			if (preg_match('#^(?:interface|(?:abstract )?class) (phpbb_[a-z0-9_]+)#m', $code, $matches))
			{
				$old_class_name = $matches[1];
				$dirs = explode(DIRECTORY_SEPARATOR, preg_replace('#^' . preg_quote(dirname($namespace_dir) . DIRECTORY_SEPARATOR, '#') . '#', '', $file->getPath()));

				$namespace = implode('\\', $dirs);

				if ($dirs[count($dirs) - 1] == substr($file->getFilename(), 0, -4))
				{
					$class_name = preg_replace('#^' . preg_quote(implode('_', $dirs), '#') . '#', $dirs[count($dirs) - 1], $old_class_name);
				}
				else
				{
					$class_name = preg_replace('#^' . preg_quote(implode('_', $dirs), '#') . '_#', '', $old_class_name);
				}

				$code = preg_replace("#^\*/$#m", "*/\n\nnamespace $namespace;", $code, 1, $count);
				if ($count != 1)
				{
					die("Incorrect replacement count for namespace of $old_class_name");
				}
				$code = preg_replace("#^(interface|(?:abstract )?class) $old_class_name#m", "\\1 $class_name", $code, -1, $count);
				if ($count != 1)
				{
					die("Incorrect replacement count for $old_class_name");
				}

				$namespaced_file = true;
			}
		}

		if (preg_match_all('#[^a-z0-9_$](phpbb_[a-z0-9_]+)#', $code, $matches))
		{
			foreach ($matches[1] as $old_class_name)
			{
				$class_name = map_class_name($old_class_name, $code_dir);
				if ($class_name)
				{
					$code = preg_replace("#([^a-z0-9_\$>])$old_class_name([^a-z0-9_])#", '\\1\\\\' . $class_name . '\\2', $code);
				}
			}
		}

		if ($namespaced_file)
		{
			$code = preg_replace('#new ([a-zA-Z0-9_][a-zA-Z0-9_\\\\]+)#', 'new \\\\\\1', $code);
			$code = preg_replace('#([^a-zA-Z0-9_\\\\$])([a-zA-Z0-9_][a-zA-Z0-9_\\\\]+)::#', '\\1\\\\\\2::', $code);
			$code = preg_replace('#catch \(([a-zA-Z0-9_][a-zA-Z0-9_\\\\]+)#', 'catch (\\\\\\1', $code);
			$code = preg_replace('#(\(|, )([a-zA-Z0-9_][a-zA-Z0-9_\\\\]+)(\s\$)#', '\\1\\\\\\2\\3', $code);
			$code = preg_replace('#(implements |extends )([a-zA-Z0-9_][a-zA-Z0-9_\\\\]+)(?=\s*(?:,|\n))#', '\\1\\\\\\2', $code);
			$abs_classes = array(
				'Countable',
				'IteratorAggregate',
				'ArrayAccess',
			);
			$code = preg_replace('#(\s+)(' . implode('|', $abs_classes) . ')#', '\\1\\\\\\2', $code);
			$rel_classes = array(
				'ContainerBuilder',
				'YamlFileLoader',
				'FileLocator',
				'Extension',
				'CompilerPassInterface',
				'EventSubscriberInterface',
				'EventDispatcherInterface',
				'ContainerAwareEventDispatcher',
				'ContainerInterface',
				'KernelEvents',
				'RouteCollection',
				'ControllerResolverInterface',
				'Request',
				'include',
				'array',
				'parent',
				'self',
			);
			$code = preg_replace('#([^a-zA-Z0-9_])\\\\((?:' . implode('|', $rel_classes) . ')(?:\s|\(|::|;))#', '\\1\\2', $code);
		}

		file_put_contents($file->getPathname(), $code);
	}

	if ($file->getExtension() == 'yml')
	{
		$code = file_get_contents($file->getPathname());

		if (preg_match_all('#\s*class:\s*(phpbb_[a-z0-9_]+)\s+#', $code, $matches))
		{
			foreach ($matches[1] as $old_class_name)
			{
				$class_name = map_class_name($old_class_name, $code_dir);
				if ($class_name)
				{
					$code = preg_replace("#(\s*class:\s*)$old_class_name(\s+)#", "\\1$class_name\\2", $code);
				}
			}
		}

		file_put_contents($file->getPathname(), $code);
	}
}