Skip to content

Commit

Permalink
Merge pull request #297 from palantirnet/feature/drupal_theme_rebuild
Browse files Browse the repository at this point in the history
New rector (10.1): drupal_theme_rebuild is deprecated
  • Loading branch information
agentrickard authored Apr 26, 2024
2 parents b7fe789 + 213cdde commit f727f31
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 51 deletions.
8 changes: 4 additions & 4 deletions config/drupal-8/drupal-8.0-deprecations.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@

$rectorConfig->ruleWithConfiguration(FunctionToServiceRector::class, [
// https://www.drupal.org/node/2418133
new FunctionToServiceConfiguration('drupal_realpath', 'file_system', 'realpath'),
new FunctionToServiceConfiguration('8.0.0', 'drupal_realpath', 'file_system', 'realpath'),
// https://www.drupal.org/node/2912696
new FunctionToServiceConfiguration('drupal_render', 'renderer', 'render'),
new FunctionToServiceConfiguration('8.0.0', 'drupal_render', 'renderer', 'render'),
// https://www.drupal.org/node/2912696
new FunctionToServiceConfiguration('drupal_render_root', 'renderer', 'renderRoot'),
new FunctionToServiceConfiguration('8.0.0', 'drupal_render_root', 'renderer', 'renderRoot'),
// https://www.drupal.org/node/1876852
new FunctionToServiceConfiguration('format_date', 'date.formatter', 'format'),
new FunctionToServiceConfiguration('8.0.0', 'format_date', 'date.formatter', 'format'),
]);

$rectorConfig->rule(EntityInterfaceLinkRector::class);
Expand Down
4 changes: 2 additions & 2 deletions config/drupal-8/drupal-8.7-deprecations.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
});
$rectorConfig->ruleWithConfiguration(FunctionToServiceRector::class, [
// https://www.drupal.org/node/3006851
new FunctionToServiceConfiguration('file_prepare_directory', 'file_system', 'prepareDirectory'),
new FunctionToServiceConfiguration('8.7.0', 'file_prepare_directory', 'file_system', 'prepareDirectory'),
// https://www.drupal.org/node/3006851
new FunctionToServiceConfiguration('file_unmanaged_save_data', 'file_system', 'saveData'),
new FunctionToServiceConfiguration('8.7.0', 'file_unmanaged_save_data', 'file_system', 'saveData'),
]);

/**
Expand Down
10 changes: 5 additions & 5 deletions config/drupal-8/drupal-8.8-deprecations.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@
$rectorConfig->ruleWithConfiguration(FunctionToServiceRector::class,
[
// https://www.drupal.org/node/2835616
new FunctionToServiceConfiguration('entity_get_display', 'entity_display.repository', 'getViewDisplay'),
new FunctionToServiceConfiguration('8.8.0', 'entity_get_display', 'entity_display.repository', 'getViewDisplay'),
// https://www.drupal.org/node/2835616
new FunctionToServiceConfiguration('entity_get_form_display', 'entity_display.repository', 'getFormDisplay'),
new FunctionToServiceConfiguration('8.8.0', 'entity_get_form_display', 'entity_display.repository', 'getFormDisplay'),
// https://www.drupal.org/node/3039255
new FunctionToServiceConfiguration('file_directory_temp', 'file_system', 'getTempDirectory'),
new FunctionToServiceConfiguration('8.8.0', 'file_directory_temp', 'file_system', 'getTempDirectory'),
// https://www.drupal.org/node/3038437
new FunctionToServiceConfiguration('file_scan_directory', 'file_system', 'scanDirectory'),
new FunctionToServiceConfiguration('8.8.0', 'file_scan_directory', 'file_system', 'scanDirectory'),
// https://www.drupal.org/node/3035273
new FunctionToServiceConfiguration('file_uri_target', 'stream_wrapper_manager', 'getTarget'),
new FunctionToServiceConfiguration('8.8.0', 'file_uri_target', 'stream_wrapper_manager', 'getTarget'),
]);

$rectorConfig->ruleWithConfiguration(MethodToMethodWithCheckRector::class, [
Expand Down
8 changes: 4 additions & 4 deletions config/drupal-9/drupal-9.3-deprecations.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@

// Change record: https://www.drupal.org/node/3223520
$rectorConfig->ruleWithConfiguration(FunctionToServiceRector::class, [
new FunctionToServiceConfiguration('file_copy', 'file.repository', 'copy'),
new FunctionToServiceConfiguration('file_move', 'file.repository', 'move'),
new FunctionToServiceConfiguration('file_save_data', 'file.repository', 'writeData'),
new FunctionToServiceConfiguration('9.3.0', 'file_copy', 'file.repository', 'copy'),
new FunctionToServiceConfiguration('9.3.0', 'file_move', 'file.repository', 'move'),
new FunctionToServiceConfiguration('9.3.0', 'file_save_data', 'file.repository', 'writeData'),
// Change record: https://www.drupal.org/node/2939099
new FunctionToServiceConfiguration('render', 'renderer', 'render'),
new FunctionToServiceConfiguration('9.3.0', 'render', 'renderer', 'render'),
]);

// Change record: https://www.drupal.org/node/3223091.
Expand Down
60 changes: 30 additions & 30 deletions src/Rector/Deprecation/FunctionToServiceRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace DrupalRector\Rector\Deprecation;

use DrupalRector\Contract\VersionedConfigurationInterface;
use DrupalRector\Rector\AbstractDrupalCoreRector;
use DrupalRector\Rector\ValueObject\FunctionToServiceConfiguration;
use PhpParser\Node;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand All @@ -20,12 +20,12 @@
* Improvement opportunities
* - Dependency injection
*/
class FunctionToServiceRector extends AbstractRector implements ConfigurableRectorInterface
class FunctionToServiceRector extends AbstractDrupalCoreRector
{
/**
* @var FunctionToServiceConfiguration[]
*/
private array $functionToServiceConfigs;
protected array $configuration;

public function configure(array $configuration): void
{
Expand All @@ -35,7 +35,7 @@ public function configure(array $configuration): void
}
}

$this->functionToServiceConfigs = $configuration;
$this->configuration = $configuration;
}

/**
Expand All @@ -51,18 +51,18 @@ public function getNodeTypes(): array
/**
* {@inheritdoc}
*/
public function refactor(Node $node): ?Node
public function refactorWithConfiguration(Node $node, VersionedConfigurationInterface $configuration): ?Node
{
foreach ($this->functionToServiceConfigs as $configuration) {
/** @var Node\Expr\FuncCall $node */
if ($this->getName($node->name) === $configuration->getDeprecatedFunctionName()) {
// This creates a service call like `\Drupal::service('file_system').
$service = new Node\Expr\StaticCall(new Node\Name\FullyQualified('Drupal'), 'service', [new Node\Arg(new Node\Scalar\String_($configuration->getServiceName()))]);
assert($configuration instanceof FunctionToServiceConfiguration);
assert($node instanceof Node\Expr\FuncCall);

$method_name = new Node\Identifier($configuration->getServiceMethodName());
if ($this->getName($node->name) === $configuration->getDeprecatedFunctionName()) {
// This creates a service call like `\Drupal::service('file_system').
$service = new Node\Expr\StaticCall(new Node\Name\FullyQualified('Drupal'), 'service', [new Node\Arg(new Node\Scalar\String_($configuration->getServiceName()))]);

return new Node\Expr\MethodCall($service, $method_name, $node->args);
}
$method_name = new Node\Identifier($configuration->getServiceMethodName());

return new Node\Expr\MethodCall($service, $method_name, $node->args);
}

return null;
Expand All @@ -82,7 +82,7 @@ public function getRuleDefinition(): RuleDefinition
CODE_AFTER
,
[
new FunctionToServiceConfiguration('drupal_realpath', 'file_system', 'realpath'),
new FunctionToServiceConfiguration('8.0.0', 'drupal_realpath', 'file_system', 'realpath'),
]
),
new ConfiguredCodeSample(
Expand All @@ -95,7 +95,7 @@ public function getRuleDefinition(): RuleDefinition
CODE_AFTER
,
[
new FunctionToServiceConfiguration('drupal_render', 'renderer', 'render'),
new FunctionToServiceConfiguration('8.0.0', 'drupal_render', 'renderer', 'render'),
]
),
new ConfiguredCodeSample(
Expand All @@ -108,7 +108,7 @@ public function getRuleDefinition(): RuleDefinition
CODE_AFTER
,
[
new FunctionToServiceConfiguration('drupal_render_root', 'renderer', 'renderRoot'),
new FunctionToServiceConfiguration('8.0.0', 'drupal_render_root', 'renderer', 'renderRoot'),
]
),
new ConfiguredCodeSample(
Expand All @@ -122,7 +122,7 @@ public function getRuleDefinition(): RuleDefinition
CODE_AFTER
,
[
new FunctionToServiceConfiguration('entity_get_display', 'entity_display.repository', 'getViewDisplay'),
new FunctionToServiceConfiguration('8.8.0', 'entity_get_display', 'entity_display.repository', 'getViewDisplay'),
]
),
new ConfiguredCodeSample(
Expand All @@ -136,7 +136,7 @@ public function getRuleDefinition(): RuleDefinition
CODE_AFTER
,
[
new FunctionToServiceConfiguration('entity_get_form_display', 'entity_display.repository', 'getFormDisplay'),
new FunctionToServiceConfiguration('8.8.0', 'entity_get_form_display', 'entity_display.repository', 'getFormDisplay'),
]
),
new ConfiguredCodeSample(
Expand All @@ -149,7 +149,7 @@ public function getRuleDefinition(): RuleDefinition
CODE_AFTER
,
[
new FunctionToServiceConfiguration('file_copy', 'file.repository', 'copy'),
new FunctionToServiceConfiguration('9.3.0', 'file_copy', 'file.repository', 'copy'),
]
),
new ConfiguredCodeSample(
Expand All @@ -162,7 +162,7 @@ public function getRuleDefinition(): RuleDefinition
CODE_AFTER
,
[
new FunctionToServiceConfiguration('file_directory_temp', 'file_system', 'getTempDirectory'),
new FunctionToServiceConfiguration('8.0.0', 'file_directory_temp', 'file_system', 'getTempDirectory'),
]
),
new ConfiguredCodeSample(
Expand All @@ -175,7 +175,7 @@ public function getRuleDefinition(): RuleDefinition
CODE_AFTER
,
[
new FunctionToServiceConfiguration('file_move', 'file.repository', 'move'),
new FunctionToServiceConfiguration('9.3.0', 'file_move', 'file.repository', 'move'),
]
),
new ConfiguredCodeSample(
Expand All @@ -188,7 +188,7 @@ public function getRuleDefinition(): RuleDefinition
CODE_AFTER
,
[
new FunctionToServiceConfiguration('file_prepare_directory', 'file_system', 'prepareDirectory'),
new FunctionToServiceConfiguration('8.7.0', 'file_prepare_directory', 'file_system', 'prepareDirectory'),
]
),
new ConfiguredCodeSample(
Expand All @@ -201,7 +201,7 @@ public function getRuleDefinition(): RuleDefinition
CODE_AFTER
,
[
new FunctionToServiceConfiguration('file_save_data', 'file.repository', 'writeData'),
new FunctionToServiceConfiguration('8.7.0', 'file_save_data', 'file.repository', 'writeData'),
]
),
new ConfiguredCodeSample(
Expand All @@ -214,7 +214,7 @@ public function getRuleDefinition(): RuleDefinition
CODE_AFTER
,
[
new FunctionToServiceConfiguration('file_scan_directory', 'file_system', 'scanDirectory'),
new FunctionToServiceConfiguration('8.8.0', 'file_scan_directory', 'file_system', 'scanDirectory'),
]
),
new ConfiguredCodeSample(
Expand All @@ -227,7 +227,7 @@ public function getRuleDefinition(): RuleDefinition
CODE_AFTER
,
[
new FunctionToServiceConfiguration('file_unmanaged_save_data', 'file_system', 'saveData'),
new FunctionToServiceConfiguration('9.3.0', 'file_unmanaged_save_data', 'file_system', 'saveData'),
]
),
new ConfiguredCodeSample(
Expand All @@ -240,7 +240,7 @@ public function getRuleDefinition(): RuleDefinition
CODE_AFTER
,
[
new FunctionToServiceConfiguration('file_uri_target', 'stream_wrapper_manager', 'getTarget'),
new FunctionToServiceConfiguration('8.8.0', 'file_uri_target', 'stream_wrapper_manager', 'getTarget'),
]
),
new ConfiguredCodeSample(
Expand All @@ -253,7 +253,7 @@ public function getRuleDefinition(): RuleDefinition
CODE_AFTER
,
[
new FunctionToServiceConfiguration('format_date', 'date.formatter', 'format'),
new FunctionToServiceConfiguration('8.0.0', 'format_date', 'date.formatter', 'format'),
]
),
new ConfiguredCodeSample(
Expand All @@ -266,7 +266,7 @@ public function getRuleDefinition(): RuleDefinition
CODE_AFTER
,
[
new FunctionToServiceConfiguration('format_date', 'date.formatter', 'format'),
new FunctionToServiceConfiguration('8.0.0', 'format_date', 'date.formatter', 'format'),
]
),
new ConfiguredCodeSample(
Expand All @@ -279,7 +279,7 @@ public function getRuleDefinition(): RuleDefinition
CODE_AFTER
,
[
new FunctionToServiceConfiguration('render', 'renderer', 'render'),
new FunctionToServiceConfiguration('9.3.0', 'render', 'renderer', 'render'),
]
),
]);
Expand Down
14 changes: 12 additions & 2 deletions src/Rector/ValueObject/FunctionToServiceConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace DrupalRector\Rector\ValueObject;

class FunctionToServiceConfiguration
use DrupalRector\Contract\VersionedConfigurationInterface;

class FunctionToServiceConfiguration implements VersionedConfigurationInterface
{
/**
* The deprecated function name.
Expand All @@ -21,11 +23,14 @@ class FunctionToServiceConfiguration
*/
protected string $serviceMethodName;

public function __construct(string $deprecatedFunctionName, string $serviceName, string $serviceMethodName)
protected string $introducedVersion;

public function __construct(string $introducedVersion, string $deprecatedFunctionName, string $serviceName, string $serviceMethodName)
{
$this->deprecatedFunctionName = $deprecatedFunctionName;
$this->serviceName = $serviceName;
$this->serviceMethodName = $serviceMethodName;
$this->introducedVersion = $introducedVersion;
}

public function getDeprecatedFunctionName(): string
Expand All @@ -42,4 +47,9 @@ public function getServiceMethodName(): string
{
return $this->serviceMethodName;
}

public function getIntroducedVersion(): string
{
return $this->introducedVersion;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@

return static function (RectorConfig $rectorConfig): void {
DeprecationBase::addClass(FunctionToServiceRector::class, $rectorConfig, false, [
new FunctionToServiceConfiguration('render', 'renderer', 'render'),
new FunctionToServiceConfiguration('file_copy', 'file.repository', 'copy'),
new FunctionToServiceConfiguration('file_move', 'file.repository', 'move'),
new FunctionToServiceConfiguration('file_save_data', 'file.repository', 'writeData'),
new FunctionToServiceConfiguration('9.3.0', 'render', 'renderer', 'render'),
new FunctionToServiceConfiguration('8.0.0', 'file_copy', 'file.repository', 'copy'),
new FunctionToServiceConfiguration('9.3.0', 'file_move', 'file.repository', 'move'),
new FunctionToServiceConfiguration('9.3.0', 'file_save_data', 'file.repository', 'writeData'),
new FunctionToServiceConfiguration('10.1.0', 'drupal_theme_rebuild', 'theme.registry', 'reset'),
]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* Implements hook_install().
*/
function append_file_info_install() {
require_once DRUPAL_ROOT . '/includes/theme.inc';
drupal_theme_rebuild();
}

-----
<?php

/**
* Implements hook_install().
*/
function append_file_info_install() {
require_once DRUPAL_ROOT . '/includes/theme.inc';
\Drupal\Component\Utility\DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '10.1.0', fn() => \Drupal::service('theme.registry')->reset(), fn() => drupal_theme_rebuild());
}

0 comments on commit f727f31

Please sign in to comment.