|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace hipanel\grid; |
|
4
|
|
|
|
|
5
|
|
|
use hiqdev\higrid\representations\RepresentationCollection; |
|
6
|
|
|
use hiqdev\higrid\representations\RepresentationCollectionInterface; |
|
7
|
|
|
use Yii; |
|
8
|
|
|
use yii\base\InvalidConfigException; |
|
9
|
|
|
use yii\helpers\Inflector; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class RepresentationCollectionFinder helps to find a representation collection class |
|
13
|
|
|
* depending on [[module]] and [[controller]] name. |
|
14
|
|
|
* |
|
15
|
|
|
* @author Dmytro Naumenko <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
class RepresentationCollectionFinder |
|
18
|
|
|
{ |
|
19
|
|
|
private $module; |
|
20
|
|
|
private $controller; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct($module, $controller) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->module = $module; |
|
25
|
|
|
$this->controller = $controller; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
protected function buildClassName() |
|
29
|
|
|
{ |
|
30
|
|
|
return sprintf('\hipanel\modules\%s\grid\%sRepresentations', $this->module, $this->controller); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @return RepresentationCollectionInterface|RepresentationCollection |
|
35
|
|
|
*/ |
|
36
|
|
|
public function find() |
|
37
|
|
|
{ |
|
38
|
|
|
$representationsClass = $this->buildClassName(); |
|
39
|
|
|
|
|
40
|
|
|
if (!class_exists($representationsClass)) { |
|
41
|
|
|
return null; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return Yii::createObject(['class' => $representationsClass]); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return RepresentationCollectionInterface|RepresentationCollection |
|
49
|
|
|
*/ |
|
50
|
|
|
public function findOrFallback() |
|
51
|
|
|
{ |
|
52
|
|
|
$collection = $this->find(); |
|
53
|
|
|
|
|
54
|
|
|
if ($collection === null) { |
|
55
|
|
|
$collection = new RepresentationCollection(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $collection; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return RepresentationCollection|RepresentationCollectionInterface |
|
63
|
|
|
* @throws InvalidConfigException When collection does not exist for the route |
|
64
|
|
|
*/ |
|
65
|
|
|
public function findOrFail() |
|
66
|
|
|
{ |
|
67
|
|
|
$collection = $this->find(); |
|
68
|
|
|
if ($collection === null) { |
|
69
|
|
|
throw new InvalidConfigException('Representation class "' . $this->buildClassName() . '" does not exist'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $collection; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
static function forCurrentRoute() |
|
|
|
|
|
|
76
|
|
|
{ |
|
77
|
|
|
$controller = Yii::$app->controller; |
|
78
|
|
|
|
|
79
|
|
|
$module = $controller->module->id; |
|
80
|
|
|
$controller = Inflector::id2camel($controller->id); |
|
81
|
|
|
|
|
82
|
|
|
return new static($module, $controller); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.