From 40ab8e7c0ab422903f50056da8660e097af75a42 Mon Sep 17 00:00:00 2001 From: William Hearn Date: Wed, 23 Aug 2017 12:33:34 -0400 Subject: [PATCH] Issue #2901069 by sylus: Slight re-architecture without library dependency --- README.txt | 11 ++- ckeditor_codemirror.info.yml | 2 +- ckeditor_codemirror.install | 36 ++++++++- ckeditor_codemirror.module | 130 +++++++++++++++++++----------- src/Plugin/CKEditorPlugin/CodeMirror.php | 6 +- src/Tests/CkeditorCodeMirrorBasicTest.php | 10 ++- 6 files changed, 132 insertions(+), 63 deletions(-) diff --git a/README.txt b/README.txt index c5735eb..f6ab057 100644 --- a/README.txt +++ b/README.txt @@ -11,17 +11,16 @@ DRUPAL 8 DEPENDENCIES ------------ -- Libraries API (8.x-3.x) module https://www.drupal.org/project/libraries - CKEditor-CodeMirror-Plugin library https://github.com/w8tcha/CKEditor-CodeMir ror-Plugin INSTALLATION ------------ -1. Download and install the Libraries API module. -2. Download and install CKEditor CodeMirror module. -3. Download CKEditor-CodeMirror-Plugin library to the following directory - "libraries/ckeditor.codemirror". Path to "plugin.js" file should be - "libraries/ckeditor.codemirror/plugin.js". +1. Download and install CKEditor CodeMirror module. +2. Download and uncompress the latest CKEditor-CodeMirror-Plugin release and + rename the folder to "ckeditor_codemirror" such that the resulting path to + the "plugin.js" file should be: + "libraries/ckeditor-codemirror/codemirror/plugin.js". CONFIGURATION ------------- diff --git a/ckeditor_codemirror.info.yml b/ckeditor_codemirror.info.yml index 3331d05..a4ea6e3 100644 --- a/ckeditor_codemirror.info.yml +++ b/ckeditor_codemirror.info.yml @@ -5,7 +5,7 @@ package: CKEditor # core: 8.x dependencies: - ckeditor - - libraries + # Information added by Drupal.org packaging script on 2016-07-24 version: '8.x-1.0' diff --git a/ckeditor_codemirror.install b/ckeditor_codemirror.install index c3b91e5..a51ae27 100644 --- a/ckeditor_codemirror.install +++ b/ckeditor_codemirror.install @@ -15,14 +15,14 @@ function ckeditor_codemirror_requirements($phase) { $return = array(); // If the library is installed. - if (($library = libraries_detect('ckeditor.codemirror')) && !empty($library['installed'])) { + if (file_exists(DRUPAL_ROOT . _ckeditor_codemirror_get_path('ckeditor-codemirror', TRUE) . '/codemirror/plugin.js')) { $return['ckeditor_codemirror_library'] = array( 'title' => t('CKEditor CodeMirror'), 'severity' => REQUIREMENT_OK, 'value' => t('CKEditor CodeMirror plugin %version installed at %path.', array( - '%path' => $library['library path'], - '%version' => $library['version'], + '%path' => _ckeditor_codemirror_get_path('ckeditor-codemirror', TRUE), + '%version' => _ckeditor_codemirror_get_version(), )), ); } @@ -39,3 +39,33 @@ function ckeditor_codemirror_requirements($phase) { return $return; } + +/** + * Gets the version information for the ckeditor_codemirror library. + * + * @return + * A string containing the version of the library. + */ +function _ckeditor_codemirror_get_version() { + // Provide defaults. + $options = [ + 'file' => 'plugin.js', + 'pattern' => '@version:\s+([0-9a-zA-Z\.-]+)@', + 'lines' => 20, + 'cols' => 200, + ]; + + $file = DRUPAL_ROOT . _ckeditor_codemirror_get_path('ckeditor-codemirror', TRUE) . '/codemirror' . '/' . $options['file']; + if (empty($options['file']) || !file_exists($file)) { + return; + } + $file = fopen($file, 'r'); + while ($options['lines'] && $line = fgets($file, $options['cols'])) { + if (preg_match($options['pattern'], $line, $version)) { + fclose($file); + return $version[1]; + } + $options['lines']--; + } + fclose($file); +} diff --git a/ckeditor_codemirror.module b/ckeditor_codemirror.module index 65aa834..fe5b5d2 100644 --- a/ckeditor_codemirror.module +++ b/ckeditor_codemirror.module @@ -5,51 +5,91 @@ * Main code for CKEditor CodeMirror module. */ + +/** + * Gets the path of a library. + * + * @param $name + * The machine name of a library to return the path for. + * @param $base_path + * Whether to prefix the resulting path with base_path(). + * + * @return + * The path to the specified library or FALSE if the library wasn't found. + */ +function _ckeditor_codemirror_get_path($name, $base_path = FALSE) { + $libraries = &drupal_static(__FUNCTION__); + + if (!isset($libraries)) { + $libraries = _ckeditor_codemirror_get_libraries(); + } + + $path = ($base_path ? base_path() : ''); + if (!isset($libraries[$name])) { + return FALSE; + } + else { + $path .= $libraries[$name]; + } + + return $path; +} + /** - * Implements hook_libraries_info(). + * Returns an array of library directories. + * + * Returns an array of library directories from the all-sites directory + * (i.e. sites/all/libraries/), the profiles directory, and site-specific + * directory (i.e. sites/somesite/libraries/). The returned array will be keyed + * by the library name. Site-specific libraries are prioritized over libraries + * in the default directories. That is, if a library with the same name appears + * in both the site-wide directory and site-specific directory, only the + * site-specific version will be listed. + * + * @return + * A list of library directories. */ -function ckeditor_codemirror_libraries_info() { - $libraries['ckeditor.codemirror'] = array( - 'name' => 'CKEditor CodeMirror plugin', - 'vendor url' => 'https://w8tcha.github.io/CKEditor-CodeMirror-Plugin', - 'download url' => 'https://github.com/w8tcha/CKEditor-CodeMirror-Plugin/releases', - 'version arguments' => array( - 'file' => 'plugin.js', - 'pattern' => '@version:\s+([0-9a-zA-Z\.-]+)@', - ), - 'files' => array( - 'js' => array( - 'plugin.js', - 'js/codemirror.min.js', - ), - 'css' => array( - 'css/codemirror.min.css', - ), - ), - 'variants' => array( - 'minified' => array( - 'files' => array( - 'js' => array( - 'js/codemirror.min.js', - ), - 'css' => array( - 'css/codemirror.min.css', - ), - ), - ), - 'source' => array( - 'files' => array( - 'js' => array( - 'js/codemirror.js', - ), - 'css' => array( - 'css/codemirror.css', - 'css/codemirror.ckeditor.css', - ), - ), - ), - ), - ); - - return $libraries; +function _ckeditor_codemirror_get_libraries() { + $searchdir = []; + // Similar to 'modules' and 'themes' directories inside an installation + // profile, installation profiles may want to place libraries into a + // 'libraries' directory. + $profile = drupal_get_profile(); + $profile = drupal_get_path('profile', $profile); + + $conf_path = \Drupal::service('site.path'); + + // Similar to 'modules' and 'themes' directories in the root directory, + // certain distributions may want to place libraries into a 'libraries' + // directory in Drupal's root directory. + $searchdir[] = 'libraries'; + + // Similar to 'modules' and 'themes' directories inside an installation + // profile, installation profiles may want to place libraries into a + // 'libraries' directory. + $searchdir[] = "$profile/libraries"; + + // Always search sites/all/libraries. + $searchdir[] = 'sites/all/libraries'; + + // Also search sites//*. + $searchdir[] = "$conf_path/libraries"; + + // Retrieve list of directories. + $directories = []; + $nomask = ['CVS']; + foreach ($searchdir as $dir) { + if (is_dir($dir) && $handle = opendir($dir)) { + while (FALSE !== ($file = readdir($handle))) { + if (!in_array($file, $nomask) && $file[0] != '.') { + if (is_dir("$dir/$file")) { + $directories[$file] = "$dir/$file"; + } + } + } + closedir($handle); + } + } + + return $directories; } diff --git a/src/Plugin/CKEditorPlugin/CodeMirror.php b/src/Plugin/CKEditorPlugin/CodeMirror.php index 72ca4c4..f24f9a2 100644 --- a/src/Plugin/CKEditorPlugin/CodeMirror.php +++ b/src/Plugin/CKEditorPlugin/CodeMirror.php @@ -23,9 +23,7 @@ class CodeMirror extends CKEditorPluginBase implements CKEditorPluginConfigurabl * {@inheritdoc} */ public function getFile() { - if ($library_path = libraries_get_path('ckeditor.codemirror')) { - return $library_path . '/plugin.js'; - } + return _ckeditor_codemirror_get_path('ckeditor-codemirror') . '/codemirror/plugin.js'; } /** @@ -124,7 +122,7 @@ class CodeMirror extends CKEditorPluginBase implements CKEditorPluginConfigurabl ); $theme_options = array('default' => 'default'); - $themes_directory = libraries_get_path('ckeditor.codemirror') . '/theme'; + $themes_directory = _ckeditor_codemirror_get_path('ckeditor-codemirror') . '/codemirror/theme'; if (is_dir($themes_directory)) { $theme_css_files = file_scan_directory($themes_directory, '/\.css/i'); foreach ($theme_css_files as $file) { diff --git a/src/Tests/CkeditorCodeMirrorBasicTest.php b/src/Tests/CkeditorCodeMirrorBasicTest.php index c482cc3..0e75c99 100644 --- a/src/Tests/CkeditorCodeMirrorBasicTest.php +++ b/src/Tests/CkeditorCodeMirrorBasicTest.php @@ -73,14 +73,16 @@ class CkeditorCodeMirrorBasicTest extends WebTestBase { * Check the library status on "Status report" page. */ public function testCheckStatusReportPage() { + module_load_include('install', 'ckeditor_codemirror'); + $this->drupalLogin($this->privilegedUser); $this->drupalGet('admin/reports/status'); - if (($library = libraries_detect('ckeditor.codemirror')) && !empty($library['library path'])) { + if (file_exists(DRUPAL_ROOT . '/libraries/ckeditor-codemirror/codemirror/plugin.js')) { $this->assertRaw(t('CKEditor CodeMirror plugin %version installed at %path.', array( - '%path' => $library['library path'], - '%version' => $library['version'], + '%path' => '/libraries/ckeditor-codemirror/', + '%version' => _ckeditor_codemirror_get_version(), )) ); } @@ -109,7 +111,7 @@ class CkeditorCodeMirrorBasicTest extends WebTestBase { $this->drupalGet('node/add/article'); $editor_settings = $this->getDrupalSettings()['editor']['formats']['full_html']['editorSettings']; - if (($library = libraries_detect('ckeditor.codemirror')) && !empty($library['library path'])) { + if (file_exists(DRUPAL_ROOT . '/libraries/ckeditor-codemirror/codemirror/plugin.js')) { $ckeditor_enabled = $editor_settings['codemirror']['enable']; $this->assertTrue($ckeditor_enabled); } -- 2.5.4 (Apple Git-61)