|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* HiPanel core package |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://hipanel.com/ |
|
6
|
|
|
* @package hipanel-core |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @copyright Copyright (c) 2014-2019, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hipanel\grid; |
|
12
|
|
|
|
|
13
|
|
|
use hiqdev\higrid\representations\RepresentationCollection; |
|
14
|
|
|
use hiqdev\higrid\representations\RepresentationCollectionInterface; |
|
15
|
|
|
use Yii; |
|
16
|
|
|
use yii\base\InvalidConfigException; |
|
17
|
|
|
use yii\helpers\Inflector; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class RepresentationCollectionFinder helps to find a representation collection class |
|
21
|
|
|
* depending on [[module]] and [[controller]] name. |
|
22
|
|
|
* |
|
23
|
|
|
* @author Dmytro Naumenko <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class RepresentationCollectionFinder implements RepresentationCollectionFinderInterface |
|
26
|
|
|
{ |
|
27
|
|
|
private $module; |
|
28
|
|
|
private $controller; |
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
* // TODO: declare format. example: '\hipanel\modules\%s\grid\%sRepresentations' |
|
32
|
|
|
*/ |
|
33
|
|
|
private $representationsLocation; |
|
34
|
|
|
|
|
35
|
|
|
public function __construct($module, $controller, string $representationsLocation) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->module = $module; |
|
38
|
|
|
$this->controller = $controller; |
|
39
|
|
|
$this->representationsLocation = $representationsLocation; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
protected function buildClassName() |
|
43
|
|
|
{ |
|
44
|
|
|
return sprintf($this->representationsLocation, $this->module, $this->controller); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return RepresentationCollectionInterface|RepresentationCollection |
|
49
|
|
|
*/ |
|
50
|
|
|
public function find() |
|
51
|
|
|
{ |
|
52
|
|
|
$representationsClass = $this->buildClassName(); |
|
53
|
|
|
|
|
54
|
|
|
if (!class_exists($representationsClass)) { |
|
55
|
|
|
return null; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return Yii::createObject(['class' => $representationsClass]); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return RepresentationCollectionInterface|RepresentationCollection |
|
63
|
|
|
*/ |
|
64
|
|
|
public function findOrFallback() |
|
65
|
|
|
{ |
|
66
|
|
|
$collection = $this->find(); |
|
67
|
|
|
|
|
68
|
|
|
if ($collection === null) { |
|
69
|
|
|
$collection = new RepresentationCollection(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $collection; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @throws InvalidConfigException When collection does not exist for the route |
|
77
|
|
|
* @return RepresentationCollection|RepresentationCollectionInterface |
|
78
|
|
|
*/ |
|
79
|
|
|
public function findOrFail() |
|
80
|
|
|
{ |
|
81
|
|
|
$collection = $this->find(); |
|
82
|
|
|
if ($collection === null) { |
|
83
|
|
|
throw new InvalidConfigException('Representation class "' . $this->buildClassName() . '" does not exist'); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $collection; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public static function forCurrentRoute(string $representationsLocation): RepresentationCollectionFinderInterface |
|
90
|
|
|
{ |
|
91
|
|
|
$controller = Yii::$app->controller; |
|
92
|
|
|
|
|
93
|
|
|
if ($controller->module instanceof RepresentationCollectionFinderProviderInterface) { |
|
94
|
|
|
return $controller->module->getRepresentationCollectionFinder(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$module = $controller->module->id; |
|
98
|
|
|
$controller = Inflector::id2camel($controller->id); |
|
99
|
|
|
|
|
100
|
|
|
return new static($module, $controller, $representationsLocation); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|