OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library plugin.src.plugin_impl; | 5 library plugin.src.plugin_impl; |
6 | 6 |
7 import 'dart:collection'; | 7 import 'dart:collection'; |
8 | 8 |
9 import 'package:plugin/manager.dart'; | 9 import 'package:plugin/manager.dart'; |
10 import 'package:plugin/plugin.dart'; | 10 import 'package:plugin/plugin.dart'; |
11 | 11 |
12 /** | 12 /** |
13 * A concrete implementation of an [ExtensionManager]. | 13 * A concrete implementation of an [ExtensionManager]. |
14 */ | 14 */ |
15 class ExtensionManagerImpl implements ExtensionManager { | 15 class ExtensionManagerImpl implements ExtensionManager { |
16 /** | 16 /** |
17 * A table mapping the id's of extension points to the corresponding | 17 * A table mapping the id's of extension points to the corresponding |
18 * extension points. | 18 * extension points. |
19 */ | 19 */ |
20 Map<String, ExtensionPointImpl> extensionPoints = | 20 Map<String, ExtensionPointImpl> extensionPoints = |
21 new HashMap<String, ExtensionPointImpl>(); | 21 new HashMap<String, ExtensionPointImpl>(); |
22 | 22 |
23 @override | 23 @override |
24 void processPlugins(List<Plugin> plugins) { | 24 void processPlugins(List<Plugin> plugins) { |
25 for (Plugin plugin in plugins) { | 25 for (Plugin plugin in plugins) { |
26 plugin.registerExtensionPoints((String identifier, | 26 plugin.registerExtensionPoints(registerExtensionPoint); |
27 [ValidateExtension validateExtension]) => | |
28 registerExtensionPoint(plugin, identifier, validateExtension)); | |
29 } | 27 } |
30 for (Plugin plugin in plugins) { | 28 for (Plugin plugin in plugins) { |
31 plugin.registerExtensions(registerExtension); | 29 plugin.registerExtensions(registerExtension); |
32 } | 30 } |
33 } | 31 } |
34 | 32 |
35 /** | 33 /** |
36 * Register an [extension] to the extension point with the given unique | 34 * Register an [extension] to the extension point with the given unique |
37 * [identifier]. | 35 * [identifier]. |
38 */ | 36 */ |
39 void registerExtension(String identifier, Object extension) { | 37 void registerExtension(String identifier, Object extension) { |
40 ExtensionPointImpl extensionPoint = extensionPoints[identifier]; | 38 ExtensionPointImpl extensionPoint = extensionPoints[identifier]; |
41 if (extensionPoint == null) { | 39 if (extensionPoint == null) { |
42 throw new ExtensionError( | 40 throw new ExtensionError( |
43 'There is no extension point with the id "$identifier"'); | 41 'There is no extension point with the id "$identifier"'); |
44 } | 42 } |
45 extensionPoint.add(extension); | 43 extensionPoint.add(extension); |
46 } | 44 } |
47 | 45 |
48 /** | 46 /** |
49 * Register an extension point being defined by the given [plugin] with the | 47 * Register the given [extensionPoint]. |
50 * given simple [identifier] and [validateExtension]. | |
51 */ | 48 */ |
52 ExtensionPoint registerExtensionPoint( | 49 void registerExtensionPoint(ExtensionPoint extensionPoint) { |
53 Plugin plugin, String identifier, ValidateExtension validateExtension) { | 50 String uniqueIdentifier = extensionPoint.uniqueIdentifier; |
54 String uniqueIdentifier = Plugin.buildUniqueIdentifier(plugin, identifier); | |
55 if (extensionPoints.containsKey(uniqueIdentifier)) { | 51 if (extensionPoints.containsKey(uniqueIdentifier)) { |
56 throw new ExtensionError( | 52 throw new ExtensionError( |
57 'There is already an extension point with the id "$identifier"'); | 53 'There is already an extension point with the id "$uniqueIdentifier"')
; |
58 } | 54 } |
59 ExtensionPointImpl extensionPoint = | 55 extensionPoints[uniqueIdentifier] = extensionPoint as ExtensionPointImpl; |
60 new ExtensionPointImpl(plugin, identifier, validateExtension); | |
61 extensionPoints[uniqueIdentifier] = extensionPoint; | |
62 return extensionPoint; | |
63 } | 56 } |
64 } | 57 } |
65 | 58 |
66 /** | 59 /** |
67 * A concrete representation of an extension point. | 60 * A concrete representation of an extension point. |
68 */ | 61 */ |
69 class ExtensionPointImpl implements ExtensionPoint { | 62 class ExtensionPointImpl<E> implements ExtensionPoint<E> { |
70 @override | 63 @override |
71 final Plugin plugin; | 64 final Plugin plugin; |
72 | 65 |
73 @override | 66 @override |
74 final String simpleIdentifier; | 67 final String simpleIdentifier; |
75 | 68 |
76 /** | 69 /** |
77 * The function used to validate extensions to this extension point. | 70 * The function used to validate extensions to this extension point. |
78 */ | 71 */ |
79 final ValidateExtension validateExtension; | 72 final ValidateExtension validateExtension; |
80 | 73 |
81 /** | 74 /** |
82 * The list of extensions to this extension point. | 75 * The list of extensions to this extension point. |
83 */ | 76 */ |
84 final List<Object> _extensions = <Object>[]; | 77 final List<E> _extensions = <E>[]; |
85 | 78 |
86 /** | 79 /** |
87 * Initialize a newly create extension point to belong to the given [plugin] | 80 * Initialize a newly create extension point to belong to the given [plugin] |
88 * and have the given [simpleIdentifier]. If [validateExtension] is non-`null` | 81 * and have the given [simpleIdentifier]. If [validateExtension] is non-`null` |
89 * it will be used to validate extensions associated with this extension | 82 * it will be used to validate extensions associated with this extension |
90 * point. | 83 * point. |
91 */ | 84 */ |
92 ExtensionPointImpl( | 85 ExtensionPointImpl( |
93 this.plugin, this.simpleIdentifier, this.validateExtension); | 86 this.plugin, this.simpleIdentifier, this.validateExtension); |
94 | 87 |
95 /** | 88 /** |
96 * Return a list containing all of the extensions that have been registered | 89 * Return a list containing all of the extensions that have been registered |
97 * for this extension point. | 90 * for this extension point. |
98 */ | 91 */ |
99 List<Object> get extensions => new UnmodifiableListView(_extensions); | 92 List<E> get extensions => new UnmodifiableListView<E>(_extensions); |
100 | 93 |
101 /** | 94 /** |
102 * Return the identifier used to uniquely identify this extension point. The | 95 * Return the identifier used to uniquely identify this extension point. The |
103 * unique identifier is the identifier for the plugin, followed by a period | 96 * unique identifier is the identifier for the plugin, followed by a period |
104 * (`.`), followed by the [simpleIdentifier] for the extension point. | 97 * (`.`), followed by the [simpleIdentifier] for the extension point. |
105 */ | 98 */ |
106 String get uniqueIdentifier => | 99 String get uniqueIdentifier => |
107 Plugin.buildUniqueIdentifier(plugin, simpleIdentifier); | 100 Plugin.buildUniqueIdentifier(plugin, simpleIdentifier); |
108 | 101 |
109 /** | 102 /** |
110 * Validate that the given [extension] is appropriate for this extension | 103 * Validate that the given [extension] is appropriate for this extension |
111 * point, and if it is then add it to the list of registered exceptions. | 104 * point, and if it is then add it to the list of registered exceptions. |
112 */ | 105 */ |
113 void add(Object extension) { | 106 void add(Object extension) { |
114 if (validateExtension != null) { | 107 if (validateExtension != null) { |
115 validateExtension(extension); | 108 validateExtension(extension); |
116 } | 109 } |
117 _extensions.add(extension); | 110 _extensions.add(extension as E); |
118 } | 111 } |
119 } | 112 } |
OLD | NEW |