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