/
DriverManager.php
74 lines (62 loc) · 1.75 KB
/
DriverManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
namespace Concrete\Core\Summary\Data\Extractor\Driver;
use Concrete\Core\Application\Application;
class DriverManager
{
const PRIORITY_DEFAULT = 1;
/**
* @var Application
*/
protected $app;
/**
* @var RegisteredDriver[]
*/
protected $drivers = [];
public function __construct(Application $app)
{
$this->app = $app;
}
/**
* @param string $class
* @param int $priority
*/
public function register(string $class, $priority = self::PRIORITY_DEFAULT)
{
$this->drivers[] = new RegisteredDriver($class, $priority);
}
/**
* @return RegisteredDriver[]
*/
public function getDrivers()
{
// Sort the drivers so that the highest score come last, so that the highest scored
// drivers run and have a chance to override fields set by lower scored drivers.
usort($this->drivers, function($a, $b) {
if ($a->getPriority() > $b->getPriority()) {
return 1;
} else if ($a->getPriority() < $b->getPriority()) {
return -1;
} else {
return 0;
}
});
return $this->drivers;
}
/**
* @param string $category
* @param $object
* @return DriverCollection
*/
public function getDriverCollection($object) : DriverCollection
{
$drivers = $this->getDrivers();
$collection = new DriverCollection();
foreach($drivers as $driver) {
$inflatedDriver = $driver->inflateClass($this->app);
if ($inflatedDriver->isValidForObject($object)) {
$collection->addDriver($inflatedDriver);
}
}
return $collection;
}
}