Skip to content
Snippets Groups Projects
Commit e74c51bf authored by Sabina Halkic's avatar Sabina Halkic Committed by Shibin Das
Browse files

Issue #3230487 by sabina.h: Refactor to use dependency injection

parent 84a44e4b
Branches
Tags
1 merge request!1Issue #3230487: Refactor to use dependency injection
services:
revision_graph.storage:
class: Drupal\revision_graph\RevisionGraphStorage
arguments: ['@database', '@path.current']
......@@ -2,16 +2,58 @@
namespace Drupal\revision_graph\Controller;
use Drupal\Core\Database\Connection;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Url;
use Drupal\node\Controller\NodeController;
use Drupal\node\NodeInterface;
use Drupal\revision_graph\RevisionGraphStorage;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Returns responses for Node routes.
*/
class RevisionGraphNodeController extends NodeController {
/**
* The default database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $connection;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* {@inheritdoc}
*/
public function __construct(DateFormatterInterface $date_formatter, RendererInterface $renderer, EntityRepositoryInterface $entity_repository = NULL, Connection $connection, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($date_formatter, $renderer, $entity_repository);
$this->connection = $connection;
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('date.formatter'),
$container->get('renderer'),
$container->get('entity.repository'),
$container->get('database'),
$container->get('entity_type.manager')
);
}
/**
* {@inheritdoc}
*/
......@@ -43,8 +85,7 @@ class RevisionGraphNodeController extends NodeController {
* List of revisions.
*/
private function loadGraphData(NodeInterface $node) {
$connection = \Drupal::service('database');
$result = $connection->query('SELECT * FROM {revision_graph} WHERE primary_id = :entity_id', [':entity_id' => $node->id()])->fetchAll();
$result = $this->connection->query('SELECT * FROM {revision_graph} WHERE primary_id = :entity_id', [':entity_id' => $node->id()])->fetchAll();
$graph = [
'map' => [],
'branches' => [],
......@@ -81,7 +122,7 @@ class RevisionGraphNodeController extends NodeController {
$commits = [];
$langcode = $node->language()->getId();
$node_storage = \Drupal::EntityTypeManager()->getStorage('node');
$node_storage = $this->entityTypeManager->getStorage('node');
$default_revision = $node->getRevisionId();
foreach ($this->revisionIds($node) as $vid) {
......@@ -149,7 +190,7 @@ class RevisionGraphNodeController extends NodeController {
*/
private function revisionIds(NodeInterface $node, $langcode = NULL) {
$vids = [];
$connection = \Drupal::service('database');
$connection = $this->connection;
if (isset($langcode)) {
$result = $connection->query(
'SELECT vid FROM {node_revision} WHERE nid=:nid AND langcode=:langcode ORDER BY vid DESC',
......
......@@ -2,72 +2,88 @@
namespace Drupal\revision_graph;
use Drupal\Core\Database\Database;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\node\NodeInterface;
use Drupal\Core\Path\CurrentPathStack;
class RevisionGraphStorage
{
class RevisionGraphStorage {
const UNTRACKED_BANCH_NAME = 'untracked';
const UNTRACKED_BANCH_NAME = 'untracked';
public function initGraph( ContentEntityInterface $entity)
{
$conn = Database::getConnection();
$conn->insert('revision_graph')->fields(
array(
'primary_id' => $entity->id(),
'version_id' => $entity->getRevisionId(),
'parent_version_id' => $entity->getRevisionId(),
'type' => $entity->getEntityTypeId(),
'branch' => $this->formatBranchName($entity->getRevisionId(), $entity->language()->getId()),
)
)->execute();
}
/**
* The default database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $connection;
public function updateGraph( ContentEntityInterface $entity)
{
$conn = Database::getConnection();
// @TODO hunt actual parent version id.
$parent_version_id = $entity->original->getRevisionId();
$branch = $this->getParentBranchName($parent_version_id);
/**
* The current path.
*
* @var \Drupal\Core\Path\CurrentPathStack
*/
protected $currentPath;
// For some reason we have default revision id in original.
// Hackish way to sniff the actual parent.
// @TODO Check if this is a bug in core due to some optimisation.
$current_path = \Drupal::service('path.current')->getPath();
$path_args = explode('/', $current_path);
if ($path_args[1] == 'node' && $path_args[3] == 'revisions' && $path_args[5] == 'revert') {
$parent_version_id = (int) $path_args[4];
$branch = $parent_version_id;
}
/**
* Creates a RevisionGraphStorage object.
*
* @param \Drupal\Core\Database\Connection $connection
* The default database connection.
* @param \Drupal\Core\Path\CurrentPathStack $current_path
* The current path.
*/
public function __construct(Connection $connection, CurrentPathStack $current_path) {
$this->connection = $connection;
$this->currentPath = $current_path;
}
public function initGraph(ContentEntityInterface $entity) {
$this->connection->insert('revision_graph')->fields([
'primary_id' => $entity->id(),
'version_id' => $entity->getRevisionId(),
'parent_version_id' => $entity->getRevisionId(),
'type' => $entity->getEntityTypeId(),
'branch' => $this->formatBranchName($entity->getRevisionId(), $entity->language()->getId()),
])->execute();
}
$conn->insert('revision_graph')->fields(
array(
'primary_id' => $entity->id(),
'version_id' => $entity->getRevisionId(),
'parent_version_id' => $parent_version_id,
'type' => $entity->getEntityTypeId(),
'branch' => $this->formatBranchName($branch, $entity->language()->getId()),
)
)->execute();
}
private function formatBranchName($parent_branch, $language_id)
{
list($branch, $code) = explode(':', $parent_branch);
return implode(':', [$branch, $language_id]);
public function updateGraph(ContentEntityInterface $entity) {
// @TODO hunt actual parent version id.
$parent_version_id = $entity->original->getRevisionId();
$branch = $this->getParentBranchName($parent_version_id);
// For some reason we have default revision id in original.
// Hackish way to sniff the actual parent.
// @TODO Check if this is a bug in core due to some optimisation.
$current_path = $this->currentPath->getPath();
$path_args = explode('/', $current_path);
if ($path_args[1] == 'node' && $path_args[3] == 'revisions' && $path_args[5] == 'revert') {
$parent_version_id = (int) $path_args[4];
$branch = $parent_version_id;
}
private function getParentBranchName($version_id)
{
$connection = \Drupal::service('database');
$result = $connection->query("SELECT * FROM {revision_graph} WHERE version_id = :vid", [':vid' => $version_id])->fetchAll();
$branch_name = self::UNTRACKED_BANCH_NAME;
foreach ($result as $r) {
$branch_name = $r->branch;
break;
}
return $branch_name;
$this->connection->insert('revision_graph')->fields([
'primary_id' => $entity->id(),
'version_id' => $entity->getRevisionId(),
'parent_version_id' => $parent_version_id,
'type' => $entity->getEntityTypeId(),
'branch' => $this->formatBranchName($branch, $entity->language()->getId()),
])->execute();
}
private function formatBranchName($parent_branch, $language_id) {
list($branch, $code) = explode(':', $parent_branch);
return implode(':', [$branch, $language_id]);
}
private function getParentBranchName($version_id) {
$result = $this->connection->query("SELECT * FROM {revision_graph} WHERE version_id = :vid", [':vid' => $version_id])->fetchAll();
$branch_name = self::UNTRACKED_BANCH_NAME;
foreach ($result as $r) {
$branch_name = $r->branch;
break;
}
return $branch_name;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment