Skip to content
Snippets Groups Projects
Commit 5d34cd54 authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Merge pull request #46 from larowlan/graph-not-gliph

Use core Graph over Gliph
parents c31f933f 7e70fa3f
No related branches found
No related tags found
No related merge requests found
...@@ -7,7 +7,7 @@ cache: ...@@ -7,7 +7,7 @@ cache:
apt: true apt: true
php: php:
- 5.4 - 5.6
- 5.5 - 5.5
env: env:
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
namespace Drupal\default_content; namespace Drupal\default_content;
use Drupal\Component\Graph\Graph;
use Drupal\Component\Utility\SafeMarkup; use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\Config\Entity\ConfigEntityInterface; use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\ContentEntityInterface;
...@@ -14,8 +15,6 @@ use Drupal\Core\Entity\EntityManager; ...@@ -14,8 +15,6 @@ use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountInterface;
use Drupal\rest\LinkManager\LinkManagerInterface; use Drupal\rest\LinkManager\LinkManagerInterface;
use Drupal\rest\Plugin\Type\ResourcePluginManager; use Drupal\rest\Plugin\Type\ResourcePluginManager;
use Gliph\Graph\DirectedAdjacencyList;
use Gliph\Traversal\DepthFirst;
use Symfony\Component\Serializer\Serializer; use Symfony\Component\Serializer\Serializer;
/** /**
...@@ -62,18 +61,18 @@ class DefaultContentManager implements DefaultContentManagerInterface { ...@@ -62,18 +61,18 @@ class DefaultContentManager implements DefaultContentManagerInterface {
protected $scanner; protected $scanner;
/** /**
* The tree resolver. * A list of vertex objects keyed by their link.
* *
* @var \Gliph\Graph\DirectedAdjacencyList * @var array
*/ */
protected $tree = FALSE; protected $vertexes = array();
/** /**
* A list of vertex objects keyed by their link. * The graph entries.
* *
* @var array * @var array
*/ */
protected $vertexes = array(); protected $graph = [];
/** /**
* The link manager service. * The link manager service.
...@@ -151,7 +150,7 @@ class DefaultContentManager implements DefaultContentManagerInterface { ...@@ -151,7 +150,7 @@ class DefaultContentManager implements DefaultContentManagerInterface {
$file_map[$self] = $file; $file_map[$self] = $file;
// Create a vertex for the graph. // Create a vertex for the graph.
$vertex = $this->getVertex($self); $vertex = $this->getVertex($self);
$this->tree()->addVertex($vertex); $this->graph[$vertex->link]['edges'] = [];
if (empty($decoded['_embedded'])) { if (empty($decoded['_embedded'])) {
// No dependencies to resolve. // No dependencies to resolve.
continue; continue;
...@@ -159,17 +158,18 @@ class DefaultContentManager implements DefaultContentManagerInterface { ...@@ -159,17 +158,18 @@ class DefaultContentManager implements DefaultContentManagerInterface {
// Here we need to resolve our dependencies; // Here we need to resolve our dependencies;
foreach ($decoded['_embedded'] as $embedded) { foreach ($decoded['_embedded'] as $embedded) {
foreach ($embedded as $item) { foreach ($embedded as $item) {
$this->tree()->addDirectedEdge($vertex, $this->getVertex($item['_links']['self']['href'])); $edge = $this->getVertex($item['_links']['self']['href']);
$this->graph[$vertex->link]['edges'][$edge->link] = TRUE;
} }
} }
} }
} }
// @todo what if no dependencies? // @todo what if no dependencies?
$sorted = $this->sortTree(); $sorted = $this->sortTree($this->graph);
foreach ($sorted as $vertex) { foreach ($sorted as $link => $details) {
if (!empty($file_map[$vertex->link])) { if (!empty($file_map[$link])) {
$file = $file_map[$vertex->link]; $file = $file_map[$link];
$entity_type_id = $file->entity_type_id; $entity_type_id = $file->entity_type_id;
$resource = $this->resourcePluginManager->getInstance(array('id' => 'entity:' . $entity_type_id)); $resource = $this->resourcePluginManager->getInstance(array('id' => 'entity:' . $entity_type_id));
$definition = $resource->getPluginDefinition(); $definition = $resource->getPluginDefinition();
...@@ -292,20 +292,16 @@ class DefaultContentManager implements DefaultContentManagerInterface { ...@@ -292,20 +292,16 @@ class DefaultContentManager implements DefaultContentManagerInterface {
return file_get_contents($file->uri); return file_get_contents($file->uri);
} }
protected function tree() {
if (empty($this->tree)) {
$this->tree = new DirectedAdjacencyList();
}
return $this->tree;
}
protected function resetTree() { protected function resetTree() {
$this->tree = FALSE; $this->graph = [];
$this->vertexes = array(); $this->vertexes = array();
} }
protected function sortTree() { protected function sortTree(array $graph) {
return DepthFirst::toposort($this->tree()); $graph_object = new Graph($graph);
$sorted = $graph_object->searchAndSort();
uasort($sorted, 'Drupal\Component\Utility\SortArray::sortByWeightElement');
return array_reverse($sorted);
} }
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment