Issue #1881654 by dawehner: Convert the Analyzer class to a service.

8.0.x
catch 2013-02-18 16:42:49 +00:00
parent 0edd4c0b86
commit d4be054870
3 changed files with 22 additions and 26 deletions

View File

@ -2,11 +2,12 @@
/** /**
* @file * @file
* Definition of Drupal\views\Analyzer. * Contains \Drupal\views\Analyzer.
*/ */
namespace Drupal\views; namespace Drupal\views;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\views\ViewExecutable; use Drupal\views\ViewExecutable;
/** /**
@ -20,44 +21,36 @@ use Drupal\views\ViewExecutable;
class Analyzer { class Analyzer {
/** /**
* The view to analyze. * A module handler that invokes the 'views_analyze' hook.
* *
* @var Drupal\views\ViewExecutable. * @var \Drupal\Core\Extension\ModuleHandlerInterface
*/ */
protected $view; protected $moduleHandler;
/** /**
* Constructs the analyzer object. * Constructs an Analyzer object.
* *
* @param Drupal\views\ViewExecutable $view * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* (optional) The view to analyze. * The module handler that invokes the 'views_analyze' hook.
*/ */
function __construct(ViewExecutable $view = NULL) { public function __construct(ModuleHandlerInterface $module_handler) {
if (isset($view)) { $this->moduleHandler = $module_handler;
$this->view = $view;
}
} }
/**
* Sets the view which is analyzed by this analyzer.
*
* @param Drupal\views\ViewExecutable
* The view to analyze.
*/
public function setView(ViewExecutable $view = NULL) {
$this->view = $view;
}
/** /**
* Analyzes a review and return the results. * Analyzes a review and return the results.
* *
* @param \Drupal\views\ViewExecutable $view
* The view to analyze.
*
* @return array * @return array
* An array of analyze results organized into arrays keyed by 'ok', * An array of analyze results organized into arrays keyed by 'ok',
* 'warning' and 'error'. * 'warning' and 'error'.
*/ */
public function getMessages() { public function getMessages(ViewExecutable $view) {
$this->view->initDisplay(); $view->initDisplay();
$messages = module_invoke_all('views_analyze', $this->view); $messages = $this->moduleHandler->invokeAll('views_analyze', array($view));
return $messages; return $messages;
} }
@ -70,7 +63,7 @@ class Analyzer {
*/ */
public function formatMessages(array $messages) { public function formatMessages(array $messages) {
if (empty($messages)) { if (empty($messages)) {
$messages = array($this->formatMessage(t('View analysis can find nothing to report.'), 'ok')); $messages = array(static::formatMessage(t('View analysis can find nothing to report.'), 'ok'));
} }
$types = array('ok' => array(), 'warning' => array(), 'error' => array()); $types = array('ok' => array(), 'warning' => array(), 'error' => array());

View File

@ -37,6 +37,9 @@ class ViewsBundle extends Bundle {
->addArgument(new Reference('config.factory')); ->addArgument(new Reference('config.factory'));
$container->register('views.executable', 'Drupal\views\ViewExecutableFactory'); $container->register('views.executable', 'Drupal\views\ViewExecutableFactory');
$container->register('views.analyzer', 'Drupal\views\Analyzer')
->addArgument(new Reference('module_handler'));
} }
} }

View File

@ -615,8 +615,8 @@ function views_ui_analyze_view_form($form, &$form_state) {
$form['#title'] = t('View analysis'); $form['#title'] = t('View analysis');
$form['#section'] = 'analyze'; $form['#section'] = 'analyze';
$analyzer = new Analyzer($view->get('executable')); $analyzer = drupal_container()->get('views.analyzer');
$messages = $analyzer->getMessages(); $messages = $analyzer->getMessages($view->get('executable'));
$form['analysis'] = array( $form['analysis'] = array(
'#prefix' => '<div class="form-item">', '#prefix' => '<div class="form-item">',