Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Derek Reese committed May 19, 2020
1 parent 3e5575d commit 74ff5e0
Show file tree
Hide file tree
Showing 15 changed files with 666 additions and 101 deletions.
1 change: 1 addition & 0 deletions config/install/patternkit.settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ patternkit_json_editor_js: ""
patternkit_json_editor_theme: bootstrap4
patternkit_libraries: []
patternkit_render_cache: true
patternkit_pl_host: https://demo.patternlab.io/api
4 changes: 4 additions & 0 deletions patternkit.links.task.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ patternkit.settings.json_settings:
title: 'JSON Pattern Library Settings'
route_name: patternkit.settings.json_settings
base_route: patternkit.settings
patternkit.settings.rest_settings:
title: 'REST Pattern Library Settings'
route_name: patternkit.settings.rest_settings
base_route: patternkit.settings
10 changes: 10 additions & 0 deletions patternkit.routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,13 @@ patternkit.settings.json_settings:
_admin_route: TRUE
requirements:
_permission: 'access administration pages'
patternkit.settings.rest_settings:
path: '/admin/config/user-interface/patternkit/rest'
defaults:
_form: '\Drupal\patternkit\Form\PatternLibraryRESTForm'
_title: 'Patternkit REST Library settings'
_description: 'Configure Patternkit REST Pattern Library Support.'
options:
_admin_route: TRUE
requirements:
_permission: 'access administration pages'
3 changes: 3 additions & 0 deletions patternkit.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ services:
patternkit.library.discovery.parser.json:
class: Drupal\patternkit\PatternLibraryParser\JSONPatternLibraryParser
arguments: ['@serialization.json', '@app.root', '@module_handler', '@theme.manager']
patternkit.library.discovery.parser.rest:
class: Drupal\patternkit\PatternLibraryParser\RESTPatternLibraryParser
arguments: ['@cache.default', '@http_client', '@config.factory', '@file_system', '@logger.channel.patternkit', '@serialization.json', '@stream_wrapper_manager']
patternkit.library.discovery.parser.twig:
class: Drupal\patternkit\PatternLibraryParser\TwigPatternLibraryParser
arguments: ['@serialization.json', '@app.root', '@module_handler', '@theme.manager']
Expand Down
13 changes: 13 additions & 0 deletions src/Form/PatternLibraryJSONForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,17 @@ public function getFormId() :string {
return 'patternkit_json_editor_config';
}

/**
* {@inheritDoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state): void {
$this->config(static::SETTINGS)
->set('patternkit_json_editor_theme', $form_state->getValue('patternkit_json_editor_theme'))
->set('patternkit_json_editor_icons', $form_state->getValue('patternkit_json_editor_icons'))
->set('patternkit_json_editor_css', $form_state->getValue('patternkit_json_editor_css'))
->set('patternkit_json_editor_js', $form_state->getValue('patternkit_json_editor_js'))
->save();
parent::submitForm($form, $form_state);
}

}
62 changes: 62 additions & 0 deletions src/Form/PatternLibraryRESTForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Drupal\patternkit\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

/**
* Settings form for configuring the REST Library Plugin.
*/
class PatternLibraryRESTForm extends ConfigFormBase {

/**
* Settings identifier.
*
* @var string
*/
public const SETTINGS = 'patternkit.settings';

/**
* Implements buildForm().
*
* {@inheritDoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) :array {
$config = $this->config(static::SETTINGS);

$form['patternkit_pl_host'] = array(
'#type' => 'textfield',
'#title' => t('PatternLab Host Web Address'),
'#description' => t('Enter the website address of the PatternLab host REST endpoint.'),
'#default_value' => $config->get('patternkit_pl_host'),
);

return parent::buildForm($form, $form_state);
}

/**
* {@inheritDoc}
*/
protected function getEditableConfigNames() :array {
return [static::SETTINGS];
}

/**
* {@inheritDoc}
*/
public function getFormId() :string {
return 'patternkit_rest_editor_config';
}

/**
* {@inheritDoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state): void {
$this->config(static::SETTINGS)
->set('patternkit_pl_host', $form_state->getValue('patternkit_pl_host'))
->save();
parent::submitForm($form, $form_state);
}

}
4 changes: 2 additions & 2 deletions src/Form/PatternkitSettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Drupal\patternkit\Form;

use Drupal\Core\Config\Config;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
Expand Down Expand Up @@ -130,8 +131,7 @@ public function getFormId() :string {
* {@inheritDoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state): void {
$config = $this->config(static::SETTINGS);
$config
$config = $this->config(static::SETTINGS)
->set('patternkit_libraries', $form_state->getValue('patternkit_libraries'))
->set('patternkit_cache_enabled', $form_state->getValue('patternkit_cache_enabled'))
->set('patternkit_render_cache', $form_state->getValue('patternkit_render_cache'))
Expand Down
12 changes: 9 additions & 3 deletions src/Loader/PatternLibraryLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

namespace Drupal\patternkit\Loader;

use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\patternkit\PatternLibraryCollector;
use Twig\Error\LoaderError;
use Twig_LoaderInterface;

/**
* Functionality for parsing Twig pattern libraries.
*/
class PatternLibraryLoader extends \Twig_Loader_Filesystem {
class PatternLibraryLoader extends \Twig_Loader_Filesystem implements Twig_LoaderInterface {

/**
* Overrides to add paths from pattern libraries.
Expand All @@ -21,6 +24,7 @@ class PatternLibraryLoader extends \Twig_Loader_Filesystem {
* Provides library names and paths.
*
* @throws \Twig\Error\LoaderError
* Thrown when encountering a cascaded loader error.
*/
public function __construct($paths,
LoggerChannelInterface $logger,
Expand All @@ -30,8 +34,10 @@ public function __construct($paths,
try {
$libraries = $pattern_collector->getLibraryDefinitions();
}
catch (\Exception $exception) {
$logger->error('Error loading pattern libraries: @message', ['@message' => $exception->getMessage()]);
catch (PluginException $exception) {
$message = $exception->getMessage();
$logger->error('Error loading pattern libraries: @message', ['@message' => $message]);
throw new LoaderError($message);
}
foreach ($libraries as $namespace => $library) {
$path = $library['data'];
Expand Down
5 changes: 3 additions & 2 deletions src/PatternLibraryCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Cache\CacheCollector;
use Drupal\Core\Lock\LockBackendInterface;
use function is_array;

/**
* The PatternLibraryCollector caches library information and performs retrieval
Expand Down Expand Up @@ -129,7 +130,7 @@ public static function create(ContainerInterface $container): self {
/** @var \Drupal\Core\Lock\LockBackendInterface $lock */
$lock = $container->get('lock');
/** @var \Drupal\Core\Logger\LoggerChannelInterface $logger */
$logger = $container->get('@logger.channel.default');
$logger = $container->get('logger.channel.patternkit');
/** @var \Drupal\Core\Extension\ModuleHandlerInterface $module_handler */
$module_handler = $container->get('module_handler');
/** @var string $root */
Expand Down Expand Up @@ -210,7 +211,7 @@ public function buildByExtension($extension_type, $extension): array {
$libraries = $this->applyLibrariesOverride($libraries, $extension);

foreach ($libraries as $id => &$library) {
if (!isset($library['patterns'])) {
if (!isset($library['patterns']) || !is_array($library['patterns'])) {
unset($libraries[$id]);
continue;
}
Expand Down
45 changes: 45 additions & 0 deletions src/PatternLibraryParser/FilePatternLibraryParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Drupal\patternkit\PatternEditorConfig;
use Drupal\patternkit\PatternLibraryJSONParserTrait;
use Drupal\patternkit\PatternLibraryParserBase;
use Symfony\Component\Finder\Iterator\RecursiveDirectoryIterator;

/**
* Parses a File pattern library collection into usable metadata.
Expand Down Expand Up @@ -39,6 +40,50 @@ public function __construct(
parent::__construct($root, $module_handler, $theme_manager);
}

/**
* Returns an array of file components grouped by file basename and extension.
*
* @param string $path
* The fully-qualified path to discover component files.
* @param array $filter
* An optional filter of file extensions to search for.
*
* @return array
* Array of file components.
* [basename][extension] = filename.
*/
public static function discoverComponents($path, array $filter = []): array {
$components = [];
$rdit = new RecursiveDirectoryIterator($path, \FilesystemIterator::KEY_AS_PATHNAME | \FilesystemIterator::CURRENT_AS_FILEINFO);
/** @var \SplFileInfo $file */
foreach (new \RecursiveIteratorIterator($rdit) as $file) {
// Skip directories and non-files.
if (!$file->isFile()) {
continue;
}
$file_path = $file->getPath();

// Skip tests folders.
if (strpos($file_path, '/tests') !== FALSE) {
continue;
}

// Get the file extension for the file.
$file_ext = $file->getExtension();
if (!in_array(strtolower($file_ext), $filter, TRUE)) {
continue;
}

// We use file_basename as a unique key, it is required that the
// JSON and twig file share this basename.
$file_basename = $file->getBasename('.' . $file_ext);

// Build an array of all the filenames of interest, keyed by name.
$components[$file_basename][$file_ext] = "$file_path/$file_basename.$file_ext";
}
return $components;
}

/**
* Fetches all assets for a pattern.
*
Expand Down
Loading

0 comments on commit 74ff5e0

Please sign in to comment.