1
|
|
|
<?php |
2
|
|
|
//[PHPCOMPRESSOR(remove,start)] |
3
|
|
|
/** |
4
|
|
|
* Created by Vitaly Iegorov <[email protected]>. |
5
|
|
|
* on 22.03.16 at 15:46 |
6
|
|
|
*/ |
7
|
|
|
namespace samsoncms\api\generator; |
8
|
|
|
|
9
|
|
|
use samsoncms\api\generator\metadata\RealMetadata; |
10
|
|
|
use samsonphp\generator\Generator; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Real entity collection class generator. |
14
|
|
|
* |
15
|
|
|
* @package samsoncms\api\generator |
16
|
|
|
*/ |
17
|
|
|
class RealCollection extends Generic |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Query constructor. |
21
|
|
|
* |
22
|
|
|
* @param Generator $generator |
23
|
|
|
* @param $metadata |
24
|
|
|
*/ |
25
|
|
|
public function __construct(Generator $generator, $metadata) |
26
|
|
|
{ |
27
|
|
|
parent::__construct($generator, $metadata); |
28
|
|
|
|
29
|
|
|
$this->parentClass = $this->className . 'Query'; |
30
|
|
|
$this->className .= 'Collection'; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Class uses generation part. |
35
|
|
|
* |
36
|
|
|
* @param RealMetadata $metadata Entity metadata |
37
|
|
|
*/ |
38
|
|
|
protected function createUses($metadata) |
39
|
|
|
{ |
40
|
|
|
$this->generator |
41
|
|
|
->newLine('use samsonframework\core\ViewInterface;') |
42
|
|
|
->newLine('use samsonframework\orm\QueryInterface;') |
43
|
|
|
->newLine('use samsonframework\orm\ArgumentInterface;') |
44
|
|
|
->newLine('use samson\activerecord\dbQuery;') |
45
|
|
|
->newLine(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Class definition generation part. |
50
|
|
|
* |
51
|
|
|
* @param RealMetadata $metadata Entity metadata |
52
|
|
|
*/ |
53
|
|
|
protected function createDefinition($metadata) |
54
|
|
|
{ |
55
|
|
|
$this->generator |
56
|
|
|
->multiComment(array( |
57
|
|
|
'Class for rendering and querying and fetching "' . $metadata->entity . '" instances from database', |
58
|
|
|
'@method ' . $metadata->entity . ' first();', |
59
|
|
|
'@method ' . $metadata->entity . '[] find();', |
60
|
|
|
)) |
61
|
|
|
->defClass($this->className, $this->parentClass) |
62
|
|
|
->newLine('use \\'.\samsoncms\api\Renderable::class.';') |
63
|
|
|
->newLine(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Class constructor generation part. |
68
|
|
|
* |
69
|
|
|
* @param RealMetadata $metadata Entity metadata |
70
|
|
|
*/ |
71
|
|
View Code Duplication |
protected function createConstructor($metadata) |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
$class = "\n\t" . '/**'; |
74
|
|
|
$class .= "\n\t" . ' * @param ViewInterface $renderer Rendering instance'; |
75
|
|
|
$class .= "\n\t" . ' * @param QueryInterface $query Database query instance'; |
76
|
|
|
$class .= "\n\t" . ' */'; |
77
|
|
|
$class .= "\n\t" . 'public function __construct(ViewInterface $renderer, QueryInterface $query = null)'; |
78
|
|
|
$class .= "\n\t" . '{'; |
79
|
|
|
$class .= "\n\t\t" . '$this->renderer = $renderer;'; |
80
|
|
|
$class .= "\n\t\t" . '$container = $GLOBALS[\'__core\']->getContainer();'; |
81
|
|
|
$class .= "\n\t\t" . 'parent::__construct($query ?? $container->get("query"));'; |
82
|
|
|
$class .= "\n\t" . '}'; |
83
|
|
|
|
84
|
|
|
$this->generator->text($class); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
//[PHPCOMPRESSOR(remove,end)] |
88
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.