diff --git a/Classes/DataCollectors/FormzCollector.php b/Classes/DataCollectors/FormzCollector.php new file mode 100644 index 0000000..5306d27 --- /dev/null +++ b/Classes/DataCollectors/FormzCollector.php @@ -0,0 +1,114 @@ +. + */ + +namespace Romm\Formz\DataCollectors; + +use Konafets\TYPO3DebugBar\DataCollectors\BaseCollector; +use Romm\Formz\Form\FormObject; +use Romm\Formz\Form\FormObjectFactory; +use TYPO3\CMS\Core\Utility\GeneralUtility; +use TYPO3\CMS\Extbase\Utility\DebuggerUtility; + +class FormzCollector extends BaseCollector +{ + /** + * @var FormObject[] + */ + private $formzInstances = []; + + /** + * Called by the DebugBar when data needs to be collected + * + * @return array Collected data + */ + public function collect() + { + $formzInstances = $this->getFormzInstances(); + + $debug = ''; + + foreach ($formzInstances as $instance) { + $debug .= DebuggerUtility::var_dump($instance, $instance->getName(), 10, false, true, true); + $forms[] = [ + 'name' => $instance->getName(), + 'params' => [ + 'Class Name' => $instance->getClassName(), + 'Fields' => $instance->getProperties(), + 'Hash' => $instance->getHash() + ] + ]; + } + + $formsCount = \count($formzInstances); + $output = [ + 'formsCount' => $formsCount, // Number of formz instance in the page + 'debug' => $debug, + 'status' => '' . $formsCount . ' Formz instance(s) in this page', + 'forms' => $forms, + 'console' => '' // div for output console javascript + ]; + + return $output; + } + + /** + * @return FormObject[] + */ + public function getFormzInstances() + { + $formObjFactory = GeneralUtility::makeInstance(FormObjectFactory::class); + + foreach ($formObjFactory->getInstances() as $instance) { + $this->formzInstances[$instance->getName()] = $instance; + } + + return $this->formzInstances; + } + + /** + * Returns a hash where keys are control names and their values + * an array of options as defined in {@see DebugBar\JavascriptRenderer::addControl()} + * + * @return array + */ + public function getWidgets() + { + $name = $this->getName(); + + return [ + (string) $name => [ + 'icon' => $name, + 'widget' => 'PhpDebugBar.Widgets.FormzWidget', + 'map' => $name, + 'default' => '[]', + ], + "$name:badge" => [ + 'map' => 'formz.formsCount', + 'default' => 0, + ], + ]; + } + + /** + * Returns the unique name of the collector + * + * @return string + */ + public function getName() + { + return 'formz'; + } +} diff --git a/Classes/Form/FormObjectFactory.php b/Classes/Form/FormObjectFactory.php index cfae228..e22f8e9 100644 --- a/Classes/Form/FormObjectFactory.php +++ b/Classes/Form/FormObjectFactory.php @@ -193,4 +193,12 @@ public function injectTypoScriptService(TypoScriptService $typoScriptService) { $this->typoScriptService = $typoScriptService; } + + /** + * @return FormObject[] + */ + public function getInstances() + { + return $this->instances; + } } diff --git a/Classes/ViewHelpers/FormViewHelper.php b/Classes/ViewHelpers/FormViewHelper.php index 74b1b02..6080cec 100644 --- a/Classes/ViewHelpers/FormViewHelper.php +++ b/Classes/ViewHelpers/FormViewHelper.php @@ -17,6 +17,7 @@ use Romm\Formz\AssetHandler\Connector\AssetHandlerConnectorManager; use Romm\Formz\AssetHandler\Html\DataAttributesAssetHandler; use Romm\Formz\Core\Core; +use Romm\Formz\DataCollectors\FormzCollector; use Romm\Formz\Exceptions\ClassNotFoundException; use Romm\Formz\Exceptions\EntryNotFoundException; use Romm\Formz\Exceptions\InvalidOptionValueException; @@ -115,6 +116,11 @@ class FormViewHelper extends \TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper */ protected $controllerService; + /** + * @var \Romm\Formz\DataCollectors\FormzCollector + */ + protected $formzCollector; + /** * @inheritdoc */ @@ -521,4 +527,12 @@ public function injectControllerService(ControllerService $controllerService) { $this->controllerService = $controllerService; } + + /** + * @param FormzCollector $formzCollector + */ + public function injectFormzCollector(FormzCollector $formzCollector) + { + $this->formzCollector = $formzCollector; + } } diff --git a/Resources/Public/JavaScript/Formz.Debug.js b/Resources/Public/JavaScript/Formz.Debug.js index 1ab50cb..00a10cd 100644 --- a/Resources/Public/JavaScript/Formz.Debug.js +++ b/Resources/Public/JavaScript/Formz.Debug.js @@ -34,18 +34,106 @@ Fz.Debug = (function () { switch (type) { case Fz.TYPE_WARNING: - color = 'color: yellow; font-weight:bold;'; + color = 'color: orange;'; + console.warn('%c[FormZ] ' + value + '%c', color, 'color: orange;'); break; case Fz.TYPE_ERROR: - color = 'color: red; font-weight:bold;'; + color = 'color: red;'; + console.error('%c[FormZ] ' + value + '%c', color, 'color: red;'); + break; + case Fz.TYPE_NOTICE: + color = 'color: blue;'; + console.info('%c[FormZ] ' + value + '%c', color, 'color: blue;'); break; default: - color = 'color: blue; font-weight:bold;'; + color = 'color: black;'; + console.log('%c[FormZ] ' + value + '%c', color, 'color: black;'); break; } - console.log('%c[FormZ - ' + type + '] %c' + value, color, 'color: black;'); + var consoleTypo3Debugbar = document.querySelector('.phpdebugbar-widgets-console'); + if (consoleTypo3Debugbar) { + consoleTypo3Debugbar.innerHTML = value; + consoleTypo3Debugbar.style = color; + } + } } }; })(); + +if (typeof(PhpDebugBar) === 'undefined') { + // namespace + var PhpDebugBar = {}; + PhpDebugBar.$ = jQuery; +} + +(function($) { + + var csscls = PhpDebugBar.utils.makecsscls('phpdebugbar-widgets-'); + + /** + * Widget for the displaying FormZ informations + * + * Options: + * - data + */ + var FormzWidget = PhpDebugBar.Widgets.FormzWidget = PhpDebugBar.Widget.extend({ + + className: csscls('formz'), + + render: function() { + + this.$status = $('
').addClass(csscls('status')).appendTo(this.$el); + this.$debug = $('
').addClass(csscls('debug')).appendTo(this.$el); + this.$list = new PhpDebugBar.Widgets.ListWidget( + { + itemRenderer: function(li, form) { + if (form.name) { + $('

').addClass(csscls('name')).text(form.name).appendTo(li); + } + + if (form.className) { + $('').addClass(csscls('className')).text(form.className).appendTo(li); + } + if (form.params && !$.isEmptyObject(form.params)) { + var table = $('
Configuration
').addClass(csscls('params')).appendTo(li); + for (var key in form.params) { + if (typeof form.params[key] !== 'function') { + table.append('' + key + '' + form.params[key] + ''); + } + } + li.css('cursor', 'pointer').click(function() { + if (table.is(':visible')) { + table.hide(); + } else { + table.show(); + } + }); + } + } + } + ); + this.$list.$el.appendTo(this.$el); + this.$console = $('
').addClass(csscls('console')).appendTo(this.$el); + + this.bindAttr('data', function(data) { + + if (data.length <= 0) { + return false; + } + + this.$list.set('data', data.forms); + this.$status.empty(); + this.$console.empty(); + + this.$status.append(data.status); + this.$debug.append(data.debug); + this.$console.append(data.console); + this.$el.append(data); + }); + } + }); +})(PhpDebugBar.$); + diff --git a/composer.json b/composer.json index 1315860..a6f2afa 100644 --- a/composer.json +++ b/composer.json @@ -26,6 +26,9 @@ "satooshi/php-coveralls": "^1.0", "nimut/testing-framework": "^1.0" }, + "suggest": { + "konafets/typo3_debugbar": "debug formz more easyly" + }, "autoload": { "psr-4": { "Romm\\Formz\\": "Classes/" diff --git a/ext_localconf.php b/ext_localconf.php index 9520786..1e84566 100644 --- a/ext_localconf.php +++ b/ext_localconf.php @@ -12,9 +12,9 @@ function ($extensionKey) { // Registering the cache. if (!is_array($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'][\Romm\Formz\Service\CacheService::CACHE_IDENTIFIER])) { $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'][\Romm\Formz\Service\CacheService::CACHE_IDENTIFIER] = [ - 'backend' => \TYPO3\CMS\Core\Cache\Backend\SimpleFileBackend::class, + 'backend' => \TYPO3\CMS\Core\Cache\Backend\SimpleFileBackend::class, 'frontend' => \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class, - 'groups' => ['all', 'system', 'pages'] + 'groups' => ['all', 'system', 'pages'] ]; } @@ -35,6 +35,11 @@ function ($extensionKey) { $container = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\Container\Container::class); $typo3Version = \TYPO3\CMS\Core\Utility\VersionNumberUtility::getCurrentTypo3Version(); + if (\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('typo3_debugbar') && version_compare($typo3Version, '8.7.0', '>=')) { + $GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][\Konafets\TYPO3DebugBar\Typo3DebugBar::class] = ['className' => \Romm\Formz\Overrides\Typo3DebugBar::class]; + $GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][\Konafets\TYPO3DebugBar\Typo3DebugBarServiceProvider::class] = ['className' => \Romm\Formz\Overrides\Typo3DebugBarServiceProvider::class]; + } + if (version_compare($typo3Version, '8.3.0', '<')) { $container->registerImplementation(\Romm\Formz\ViewHelpers\FormViewHelper::class, \Romm\Formz\Service\ViewHelper\Legacy\OldFormViewHelper::class); $GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][\Romm\Formz\ViewHelpers\FormViewHelper::class] = [