New Services
As part of the clean-up work leading into the Patternkit 9.1.0-Beta4 release, the existing \Drupal\patternkit\Asset\Library (patternkit.asset.library) service has been deprecated and replaced with three new services:
LibraryNamespaceResolver (patternkit.library.namespace_resolver)
This service addresses the responsibility of mapping library namespaces to the providing extensions and their library definitions. To achieve this, it builds on top of the already assembled and altered library discovery information provided by Core via the @library.discovery service. This offloads much of the work for considering how other assets are handled in libraries that our module doesn't care about and lets our service focus on processing and capturing the pattern collection data at the library definition level before it is processed and individual patterns are discovered.
Once the library data is prepared with the inclusion of pattern-specific collection data, this service is responsible for caching this information.
PatternDiscovery (patternkit.pattern.discovery)
This service serves as the entry point for most developer needs to work with discovered pattern definitions. Through this service, the developer has access to retrieve pattern definitions that have been discovered, whether that be a specific pattern, all patterns within a given namespace, or all patterns that have been discovered across all namespaces. Behind the scenes, the PatternDiscoveryCollector service is used to discover and cache these pattern definitions.
PatternDiscoveryCollector (patternkit.pattern.discovery.collector)
This service is the driver for leveraging the pattern library data assembled in the LibraryNamespaceResolver and feeding it to the existing PatternLibraryParser services in order to discover available patterns. These discovered patterns are then cached for access via the PatternDiscovery service.
Deprecation and Replacement
With the introduction of these new services, the existing Library service has been rewritten as a pass-through to the new services to maintain backward-compatibility with existing usages of the service both remaining in the module and in existing sites using the module. Each of the public methods exposed on this service, however, will now log a deprecation warning and the service itself is intended to be removed leading up to the stable 1.0 release.
With the introduction of these new services, the method for performing several tasks has changed.
Get all discovered pattern definitions
The old way
$pattern_definitions = \Drupal::service('patternkit.asset.library')->getAssets();
$example_pattern = $pattern_definitions['@patternkit/atoms/example/src/example'];
Using this method, the $pattern_definitions variable would contain an array of all discovered patterns keyed by their namespaced ID.
The new way
$pattern_definitions = \Drupal::service('patternkit.pattern.discovery')->getPatternDefinitions();
$example_pattern = $pattern_definitions['@patternkit']['@patternkit/atoms/example/src/example'];
Using this method, the $pattern_definitions variable now contains a nested associative array of all discovered patterns, first keyed by namespace, then by the pattern's namespaced ID.
Get a single pattern definition
The old way
$pattern = \Drupal::service('patternkit.asset.library')->getLibraryAsset('@patternkit/atoms/example/src/example');
The new way
$pattern = \Drupal::service('patternkit.pattern.discovery')->getPatternDefinition('@patternkit/atoms/example/src/example');
Get library definitions
The old way
$libraries = \Drupal::service('patternkit.asset.library')->getLibraries();
Using this method, the $libraries variable is now an array of \Drupal\patternkit\PatternLibrary instances populated with the specific library's metadata keyed by the library's namespace.
The new way
$libraries = \Drupal::service('patternkit.library.namespace_resolver')->getLibraryDefinitions();
Using this method, the $libraries variable is now an associative array of each library's metadata keyed by the library's namespace. The library data is no longer contained with in instances of the \Drupal\patternkit\PatternLibrary class. Each library definition now contains various data in addition to the information discovered by Drupal core including the following keys:
id: The name of the library.extension: The name of the extension providing the library.extensionType: The type of extension providing the library.extensionPath: The path to the extension providing the library.namespace: The namespace for the library.
Get a single library definition
The old way
Previously, in order to fetch a single library definition, you had to know the extension providing it in addition to the library name. The return value of this method is a populated \Drupal\patternkit\PatternLibrary class instance.
$library = \Drupal::service('patternkit.asset.library')->getLibraryByName('patternkit_example', 'patternkit');
The new way
Using the new service, knowing the providing module is no longer necessary. The library data is discovered and aggregated before the lookup in order to allow any alterations between modules to be reconciled first. Again, the return value is also different in that the returned library data is contained in an associative array instead of a \Drupal\patternkit\PatternLibrary class instance.
Also note that the library name argument is the library's namespace instead of the library ID defined in the *.libraries.yml file. In most cases, this will be the same just prefixed with an "@".
$library = \Drupal::service('patternkit.library.namespace_resolver')->getLibraryByName('@patternkit');