-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathHookUpdateN.php
More file actions
86 lines (72 loc) · 2.95 KB
/
HookUpdateN.php
File metadata and controls
86 lines (72 loc) · 2.95 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
<?php
namespace DrupalCodeBuilder\Generator;
use DrupalCodeBuilder\Definition\PropertyDefinition;
use MutableTypedData\Definition\PropertyListInterface;
use DrupalCodeBuilder\File\DrupalExtension;
/**
* Generator for hook_update_N() implementation.
*/
class HookUpdateN extends HookImplementationProcedural {
/**
* {@inheritdoc}
*/
public static function addToGeneratorDefinition(PropertyListInterface $definition) {
parent::addToGeneratorDefinition($definition);
// This hook is plain, not tokenised, so we don't need to add the property
// on both variants.
foreach ($definition->getVariants() as $variant) {
// The next schema number to use for the hook implementation.
$variant->addProperty(PropertyDefinition::create('string')
->setName('schema_number')
->setInternal(TRUE)
->setCallableDefault(function ($component_data) {
// Get overwritten by detectExistence() if existing implementations are
// found.
return (\DrupalCodeBuilder\Factory::getEnvironment()->getCoreMajorVersion() * 1000) + 1;
})
);
}
}
/**
* {@inheritdoc}
*/
public function detectExistence(DrupalExtension $extension) {
// This does not strictly speaking detect existence, but detects existing
// implementations of this hook, so that our new implementation gets the
// correct number.
$install_filename = (
$this->component_data->component_base_path->value ?
$this->component_data->component_base_path->value . '/' :
''
)
. '%module.install';
$install_filename = str_replace('%module', $this->component_data->root_component_name->value, $install_filename);
if (!$extension->hasFile($install_filename)) {
return;
}
$ast = $extension->getFileAST($install_filename);
$install_function_nodes = $extension->getASTFunctions($ast);
// Detect any existing implementations.
$hook_update_schema_numbers = [];
foreach ($install_function_nodes as $function_node) {
$existing_function_name = (string) $function_node->name;
$matches = [];
if (preg_match('@' . $this->component_data['root_component_name'] . '_update_(\d+)$@', $existing_function_name, $matches)) {
$hook_update_schema_numbers[] = $matches[1];
}
}
if ($hook_update_schema_numbers) {
$this->component_data->schema_number->value = max($hook_update_schema_numbers) + 1;
}
}
/**
* {@inheritdoc}
*/
public function getContents(): array {
// Replace the '_N' part of the function declaration.
$this->component_data->declaration->value = preg_replace('/(?<=_update_)N/', $this->component_data->schema_number->value, $this->component_data->declaration->value);
// Also do the function name.
$this->component_data->function_name->value = preg_replace('/(?<=_update_)N/', $this->component_data->schema_number->value, $this->component_data->function_name->value);
return parent::getContents();
}
}