Skip to content

EspoCRM – Open Source CRM Application

Before upgrade check methods override

Yurii2de423ea03b1
diff --git a/upgrades/10.0/scripts/BeforeUpgrade.php b/upgrades/10.0/scripts/BeforeUpgrade.php
index cad6fbedba..25012ae18f 100644
--- a/upgrades/10.0/scripts/BeforeUpgrade.php
+++ b/upgrades/10.0/scripts/BeforeUpgrade.php
@@ -28,6 +28,7 @@
************************************************************************/
use Espo\Core\Container;
+use Espo\Core\Record\Service;
use Espo\Entities\Extension;
use Espo\ORM\EntityManager;
@@ -44,6 +45,7 @@ class BeforeUpgrade
$this->container = $container;
$this->processCheckExtensions();
+ $this->processCheckCustomizations();
}
/**
@@ -94,4 +96,166 @@ class BeforeUpgrade
$errorMessageList[] = $message;
}
+
+ private function processCheckCustomizations(): void
+ {
+ /** @var array{0: string, 1: string[]}[] $data */
+ $data = [
+ [
+ Service::class,
+ [
+ 'create',
+ 'read',
+ 'update',
+ 'delete',
+ 'link',
+ 'unlink',
+ 'massLink',
+ 'beforeCreateEntity',
+ 'afterCreateEntity',
+ 'beforeUpdateEntity',
+ 'afterUpdateEntity',
+ 'beforeDeleteEntity',
+ 'afterDeleteEntity',
+ ]
+ ],
+ ];
+
+ $output = [];
+
+ foreach (self::getClasses("custom/Espo/") as $class) {
+ foreach ($data as $it) {
+ $base = $it[0];
+ $methods = $it[1];
+
+ try {
+ $isSubClass = class_exists($class) && is_subclass_of($class, $base);
+ } catch (Throwable) {
+ continue;
+ }
+
+ if (
+ $isSubClass &&
+ (
+ str_starts_with($class, "Espo\\Modules\\") ||
+ str_starts_with($class, "Espo\\Custom\\")
+ ) &&
+ !str_starts_with($class, "Espo\\Modules\\Crm\\")
+ ) {
+ foreach ($methods as $method) {
+ if (self::isMethodOverridden($class, $base, $method)) {
+ $output[] = [$class, $method];
+ }
+ }
+ }
+ }
+ }
+
+ echo "!";
+
+ if ($output === []) {
+ return;
+ }
+
+ $message = "Cannot upgrade because of incompatible method overrides. ".
+ "Fix the customizations and/or uninstall the incompatible extensions. " .
+ "You can fix it by removing the methods for the classes.\n\n";
+
+ $message .= "Problem classes and methods:\n";
+
+ foreach ($output as $it) {
+ $message .= ' ' . $it[0] . ': ' . $it[1] . "\n";
+ }
+
+ throw new Error($message);
+ }
+
+ /**
+ * @noinspection PhpSameParameterValueInspection
+ */
+ private static function isMethodOverridden(string $child, string $base, string $method): bool
+ {
+ if (!class_exists($child) || !method_exists($child, $method)) {
+ return false;
+ }
+
+ return (new ReflectionMethod($child, $method))->getDeclaringClass()->getName() !== $base;
+ }
+
+ /**
+ * @return string[]
+ * @noinspection PhpSameParameterValueInspection
+ */
+ private static function getClasses(string $directory): array
+ {
+ $iterator = new RecursiveIteratorIterator(
+ new RecursiveDirectoryIterator($directory)
+ );
+
+ $classes = [];
+
+ foreach ($iterator as $file) {
+ if (!$file->isFile()) {
+ continue;
+ }
+
+ if ($file->getExtension() !== 'php') {
+ continue;
+ }
+
+ $content = file_get_contents($file->getPathname());
+
+ $namespace = null;
+ $class = null;
+
+ $tokens = token_get_all($content);
+
+ for ($i = 0; $i < count($tokens); $i++) {
+ $token = $tokens[$i];
+
+ if (!is_array($token)) {
+ continue;
+ }
+
+ if ($token[0] === T_NAMESPACE) {
+ $namespace = '';
+
+ for ($j = $i + 1; $j < count($tokens); $j++) {
+ if (
+ is_array($tokens[$j]) &&
+ in_array($tokens[$j][0], [T_STRING, T_NAME_QUALIFIED, T_NS_SEPARATOR])
+ ) {
+ $namespace .= $tokens[$j][1];
+ } elseif ($tokens[$j] === ';') {
+ break;
+ }
+ }
+ }
+
+ if ($token[0] === T_CLASS) {
+ $prev = $tokens[$i - 1] ?? null;
+
+ if (is_array($prev) && $prev[0] === T_NEW) {
+ continue;
+ }
+
+ for ($j = $i + 1; $j < count($tokens); $j++) {
+ if (is_array($tokens[$j]) && $tokens[$j][0] === T_STRING) {
+ $class = $tokens[$j][1];
+
+ break;
+ }
+ }
+
+ if ($class) {
+ $classes[] = $namespace
+ ? $namespace . '\\' . $class
+ : $class;
+ }
+ }
+ }
+ }
+
+ return $classes;
+ }
}