Browse Source

Add font icon task

mscherer 5 years ago
parent
commit
c4f7a4b12a
1 changed files with 64 additions and 0 deletions
  1. 64 0
      src/IdeHelper/Generator/Task/FormatIconTask.php

+ 64 - 0
src/IdeHelper/Generator/Task/FormatIconTask.php

@@ -0,0 +1,64 @@
+<?php
+
+namespace Tools\IdeHelper\Generator\Task;
+
+use Cake\Core\Configure;
+use Cake\View\View;
+use IdeHelper\Generator\Directive\ExpectedArguments;
+use IdeHelper\Generator\Task\TaskInterface;
+use Tools\View\Helper\FormatHelper;
+
+class FormatIconTask implements TaskInterface {
+
+	const CLASS_FORMAT_HELPER = FormatHelper::class;
+
+	/**
+	 * @return \IdeHelper\Generator\Directive\BaseDirective[]
+	 */
+	public function collect() {
+		$result = [];
+
+		$icons = $this->collectIcons();
+		$list = [];
+		foreach ($icons as $icon) {
+			$list[$icon] = '\'' . $icon . '\'';
+		}
+
+		ksort($list);
+
+		$method = '\\' . static::CLASS_FORMAT_HELPER . '::icon()';
+		$directive = new ExpectedArguments($method, 0, $list);
+		$result[$directive->key()] = $directive;
+
+		return $result;
+	}
+
+	/**
+	 * Fontawesome v4
+	 *
+	 * @return string[]
+	 */
+	protected function collectIcons() {
+		$helper = new FormatHelper(new View());
+		$configured = $helper->getConfig('fontIcons');
+
+		$fontFile = Configure::readOrFail('Format.fontPath');
+		$icons = [];
+		if ($fontFile && file_exists($fontFile)) {
+			$content = file_get_contents($fontFile);
+			preg_match_all('/glyph-name="([a-z][^"]+)"/', $content, $matches);
+			$icons = $matches[1];
+			foreach ($icons as $key => $icon) {
+				if (strpos($icon, 'uni') === 0 || preg_match('#[a-z]\d[a-z]\d#i', $icon)) {
+					unset($icons[$key]);
+				}
+			}
+		}
+
+		$icons = array_merge($configured, $icons);
+		sort($icons);
+
+		return $icons;
+	}
+
+}