Skip to content

Commit e0b29ee

Browse files
committed
Merge pull request #66 from drupol/Submodule-Service-Container-Doctrine
Issue #2504025 by Pol, Fabianx: Support Drupal 8 plugins via annotation parsing
2 parents cc913f5 + 4d41cfb commit e0b29ee

File tree

53 files changed

+736
-40
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+736
-40
lines changed

HACK.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
## List of service and class that we've altered ##
1+
## List of classes that we've altered ##
22
- Database:
33
- Drupal\Core\Database\Connection
44
- Drupal\Core\Database\Database
55
- Drupal\Core\Database\Query\Merge
6+
- FileCache:
7+
- Drupal\Component\FileCacheFactory (PHP 5.3 friendly)
8+
- Drupal\Component\NullFileCache (PHP 5.3 friendly)
69
- KeyValueStore:
710
- Drupal\Core\KeyValueStore\DatabaseStorage
811
- StringTranslation:

lib/Drupal/Component/FileCache/FileCacheFactory.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ class FileCacheFactory {
3838
* @return \Drupal\Component\FileCache\FileCacheInterface
3939
* The initialized FileCache object.
4040
*/
41-
public static function get($collection, $default_configuration = []) {
42-
$default_configuration += [
41+
public static function get($collection, $default_configuration = array()) {
42+
$default_configuration += array(
4343
'class' => '\Drupal\Component\FileCache\FileCache',
4444
'collection' => $collection,
4545
'cache_backend_class' => NULL,
46-
'cache_backend_configuration' => [],
47-
];
46+
'cache_backend_configuration' => array(),
47+
);
4848

49-
$configuration = [];
49+
$configuration = array();
5050
if (isset(static::$configuration[$collection])) {
5151
$configuration = static::$configuration[$collection];
5252
}

lib/Drupal/Component/FileCache/NullFileCache.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class NullFileCache implements FileCacheInterface {
2626
* @param array $cache_backend_configuration
2727
* (optional) The configuration for the backend class.
2828
*/
29-
public function __construct($prefix, $collection, $cache_backend_class = NULL, array $cache_backend_configuration = []) {
29+
public function __construct($prefix, $collection, $cache_backend_class = NULL, array $cache_backend_configuration = array()) {
3030
}
3131

3232
/**
@@ -40,7 +40,7 @@ public function get($filepath) {
4040
* {@inheritdoc}
4141
*/
4242
public function getMultiple(array $filepaths) {
43-
return [];
43+
return array();
4444
}
4545

4646
/**
+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Definition of Drupal\Core\Annotation\Translation.
6+
*/
7+
8+
namespace Drupal\Core\Annotation;
9+
10+
use Drupal\Component\Annotation\AnnotationBase;
11+
use Drupal\Core\StringTranslation\TranslationWrapper;
12+
13+
/**
14+
* @defgroup plugin_translatable Annotation for translatable text
15+
* @{
16+
* Describes how to put translatable UI text into annotations.
17+
*
18+
* When providing plugin annotation, properties whose values are displayed in
19+
* the user interface should be made translatable. Much the same as how user
20+
* interface text elsewhere is wrapped in t() to make it translatable, in plugin
21+
* annotation, wrap translatable strings in the @ Translation() annotation.
22+
* For example:
23+
* @code
24+
* title = @ Translation("Title of the plugin"),
25+
* @endcode
26+
* Remove spaces after @ in your actual plugin - these are put into this sample
27+
* code so that it is not recognized as annotation.
28+
*
29+
* To provide replacement values for placeholders, use the "arguments" array:
30+
* @code
31+
* title = @ Translation("Bundle !title", arguments = {"!title" = "Foo"}),
32+
* @endcode
33+
*
34+
* It is also possible to provide a context with the text, similar to t():
35+
* @code
36+
* title = @ Translation("Bundle", context = "Validation"),
37+
* @endcode
38+
* Other t() arguments like language code are not valid to pass in. Only
39+
* context is supported.
40+
*
41+
* @see i18n
42+
* @see annotation
43+
* @}
44+
*/
45+
46+
/**
47+
* Defines a translatable annotation object.
48+
*
49+
* Some metadata within an annotation needs to be translatable. This class
50+
* supports that need by allowing both the translatable string and, if
51+
* specified, a context for that string. The string (with optional context)
52+
* is passed into t().
53+
*
54+
* @ingroup plugin_translatable
55+
*
56+
* @Annotation
57+
*/
58+
class Translation extends AnnotationBase {
59+
60+
/**
61+
* The string translation object.
62+
*
63+
* @var \Drupal\Core\StringTranslation\TranslationWrapper
64+
*/
65+
protected $translation;
66+
67+
/**
68+
* Constructs a new class instance.
69+
*
70+
* Parses values passed into this class through the t() function in Drupal and
71+
* handles an optional context for the string.
72+
*
73+
* @param array $values
74+
* Possible array keys:
75+
* - value (required): the string that is to be translated.
76+
* - arguments (optional): an array with placeholder replacements, keyed by
77+
* placeholder.
78+
* - context (optional): a string that describes the context of "value";
79+
*/
80+
public function __construct(array $values) {
81+
$string = $values['value'];
82+
$arguments = isset($values['arguments']) ? $values['arguments'] : array();
83+
$options = array();
84+
if (!empty($values['context'])) {
85+
$options = array(
86+
'context' => $values['context'],
87+
);
88+
}
89+
$this->translation = new TranslationWrapper($string, $arguments, $options);
90+
}
91+
92+
/**
93+
* {@inheritdoc}
94+
*/
95+
public function get() {
96+
return $this->translation;
97+
}
98+
99+
}
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Contains \Drupal\Core\Block\Annotation\Block.
6+
*/
7+
8+
namespace Drupal\Core\Block\Annotation;
9+
10+
use Drupal\Component\Annotation\Plugin;
11+
12+
/**
13+
* Defines a Block annotation object.
14+
*
15+
* @ingroup block_api
16+
*
17+
* @Annotation
18+
*/
19+
class Block extends Plugin {
20+
21+
/**
22+
* The plugin ID.
23+
*
24+
* @var string
25+
*/
26+
public $id;
27+
28+
/**
29+
* The administrative label of the block.
30+
*
31+
* @var \Drupal\Core\Annotation\Translation
32+
*
33+
* @ingroup plugin_translatable
34+
*/
35+
public $admin_label = '';
36+
37+
/**
38+
* The category in the admin UI where the block will be listed.
39+
*
40+
* @var \Drupal\Core\Annotation\Translation
41+
*
42+
* @ingroup plugin_translatable
43+
*/
44+
public $category = '';
45+
46+
/**
47+
* Class used to retrieve derivative definitions of the block.
48+
*
49+
* @var string
50+
*/
51+
public $derivative = '';
52+
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Contains \Drupal\service_container_annotation_discovery\Tests\ServiceContainerBlockIntegrationTest.
6+
*/
7+
8+
namespace Drupal\service_container_annotation_discovery\Tests;
9+
10+
use Drupal\Component\Plugin\PluginBase;
11+
use Drupal\service_container\Tests\ServiceContainerIntegrationTestBase;
12+
use Mockery\CountValidator\Exception;
13+
use Symfony\Component\Yaml\Exception\RuntimeException;
14+
15+
class ServiceContainerBlockIntegrationTest extends ServiceContainerIntegrationTestBase {
16+
17+
/**
18+
* {@inheritdoc}
19+
*/
20+
protected function setUp() {
21+
parent::setUp('service_container_annotation_discovery_test');
22+
23+
$this->container = \Drupal::getContainer();
24+
}
25+
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
public static function getInfo() {
30+
return array(
31+
'name' => 'ServiceContainerAnnotationDirectoryIntegrationTest',
32+
'description' => 'Tests the \ServiceContainer Annotation Discovery class',
33+
'group' => 'service_container',
34+
);
35+
}
36+
37+
/**
38+
* Tests if plugins with annotations are available as services.
39+
*/
40+
public function testDoctrinePlugin() {
41+
$plugins = array(
42+
array(
43+
'owner' => 'sc_doctrine_test',
44+
'type' => 'Plugin1',
45+
'name' => 'Plugin1A',
46+
),
47+
array(
48+
'owner' => 'sc_doctrine_test',
49+
'type' => 'Plugin2',
50+
'name' => 'Plugin2B',
51+
),
52+
);
53+
foreach($plugins as $plugin) {
54+
$plugin_manager = $this->container->get($plugin['owner'] . '.' . $plugin['type']);
55+
$this->assertTrue($plugin_manager->hasDefinition($plugin['name']));
56+
$object = $plugin_manager->createInstance($plugin['name']);
57+
$this->assertTrue($object instanceof PluginBase);
58+
}
59+
60+
$plugin = array(
61+
'owner' => 'sc_doctrine_test',
62+
'type' => 'Plugin3',
63+
'name' => 'Plugin1C',
64+
);
65+
try {
66+
$this->container->get($plugin['owner'] . '.' . $plugin['type']);
67+
} catch (\RuntimeException $e) {
68+
$this->pass('Bad input correctly threw an exception');
69+
}
70+
71+
$plugin = array(
72+
'owner' => 'sc_doctrine_test',
73+
'type' => 'Plugin1',
74+
'name' => 'Plugin3A',
75+
);
76+
$plugin_manager = $this->container->get($plugin['owner'] . '.' . $plugin['type']);
77+
$this->assertFalse($plugin_manager->hasDefinition($plugin['name']));
78+
}
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name = Services Container Annotation Discovery
2+
description = Services Container Annotation discovery integration.
3+
package = Utility
4+
core = 7.x
5+
php = 5.3.10
6+
7+
; Dependencies
8+
dependencies[] = service_container_symfony
9+
10+
registry_autoload[] = PSR-0
11+
registry_autoload[] = PSR-4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* @file
4+
* Main module file for the service_container_annotation_discovery module.
5+
*/
6+
7+
// -----------------------------------------------------------------------
8+
// Contrib Hooks
9+
10+
/**
11+
* Implements hook_ctools_plugin_directory().
12+
*/
13+
function service_container_annotation_discovery_ctools_plugin_directory($owner, $plugin_type) {
14+
if ($owner == 'service_container') {
15+
return 'src/ServiceContainer/' . $plugin_type;
16+
}
17+
18+
return NULL;
19+
}
20+
21+
// -----------------------------------------------------------------------
22+
// Public API

0 commit comments

Comments
 (0)