|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
/** |
|
4
|
|
|
* Contains EveApiCreatorTrait trait. |
|
5
|
|
|
* |
|
6
|
|
|
* PHP version 7.0+ |
|
7
|
|
|
* |
|
8
|
|
|
* LICENSE: |
|
9
|
|
|
* This file is part of Yet Another Php Eve Api Library also know as Yapeal |
|
10
|
|
|
* which can be used to access the Eve Online API data and place it into a |
|
11
|
|
|
* database. |
|
12
|
|
|
* Copyright (C) 2015-2016 Michael Cummings |
|
13
|
|
|
* |
|
14
|
|
|
* This program is free software: you can redistribute it and/or modify it |
|
15
|
|
|
* under the terms of the GNU Lesser General Public License as published by the |
|
16
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your |
|
17
|
|
|
* option) any later version. |
|
18
|
|
|
* |
|
19
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT |
|
20
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
21
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
|
22
|
|
|
* for more details. |
|
23
|
|
|
* |
|
24
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
|
25
|
|
|
* along with this program. If not, see |
|
26
|
|
|
* <http://spdx.org/licenses/LGPL-3.0.html>. |
|
27
|
|
|
* |
|
28
|
|
|
* You should be able to find a copy of this license in the COPYING-LESSER.md |
|
29
|
|
|
* file. A copy of the GNU GPL should also be available in the COPYING.md file. |
|
30
|
|
|
* |
|
31
|
|
|
* @copyright 2015-2016 Michael Cummings |
|
32
|
|
|
* @license http://www.gnu.org/copyleft/lesser.html GNU LGPL |
|
33
|
|
|
* @author Michael Cummings <[email protected]> |
|
34
|
|
|
*/ |
|
35
|
|
|
namespace Yapeal\Console\Command; |
|
36
|
|
|
|
|
37
|
|
|
use Twig_Environment; |
|
38
|
|
|
use Yapeal\Event\EveApiEventEmitterTrait; |
|
39
|
|
|
use Yapeal\Exception\YapealFileSystemException; |
|
40
|
|
|
use Yapeal\FileSystem\CommonFileHandlingTrait; |
|
41
|
|
|
use Yapeal\Log\Logger; |
|
42
|
|
|
use Yapeal\Xml\EveApiReadWriteInterface; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Trait EveApiCreatorTrait |
|
46
|
|
|
*/ |
|
47
|
|
|
trait EveApiCreatorTrait |
|
48
|
|
|
{ |
|
49
|
|
|
use CommonFileHandlingTrait, EveApiEventEmitterTrait; |
|
50
|
|
|
/** |
|
51
|
|
|
* Getter for $overwrite. |
|
52
|
|
|
* |
|
53
|
|
|
* @return boolean |
|
54
|
|
|
*/ |
|
55
|
|
|
public function isOverwrite(): bool |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->overwrite; |
|
58
|
|
|
} |
|
59
|
|
|
/** |
|
60
|
|
|
* Fluent interface setter for $overwrite. |
|
61
|
|
|
* |
|
62
|
|
|
* @param bool $value |
|
63
|
|
|
* |
|
64
|
|
|
* @return self Fluent interface. |
|
65
|
|
|
*/ |
|
66
|
|
|
public function setOverwrite(bool $value = true) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->overwrite = (bool)$value; |
|
69
|
|
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
/** |
|
72
|
|
|
* @param Twig_Environment $twig |
|
73
|
|
|
* |
|
74
|
|
|
* @return self Fluent interface. |
|
75
|
|
|
*/ |
|
76
|
|
|
public function setTwig(Twig_Environment $twig) |
|
77
|
|
|
{ |
|
78
|
|
|
$this->twig = $twig; |
|
79
|
|
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
/** |
|
82
|
|
|
* @param string $eventName |
|
83
|
|
|
* @param \Yapeal\Xml\EveApiReadWriteInterface $data |
|
84
|
|
|
* @param array $context |
|
85
|
|
|
* |
|
86
|
|
|
* @return bool|string |
|
87
|
|
|
* @throws \DomainException |
|
88
|
|
|
* @throws \InvalidArgumentException |
|
89
|
|
|
* @throws \LogicException |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function getContentsFromTwig(string $eventName, EveApiReadWriteInterface $data, array $context) |
|
92
|
|
|
{ |
|
93
|
|
|
$yem = $this->getYem(); |
|
94
|
|
|
try { |
|
95
|
|
|
$templateName = $this->findRelativeFileWithPath(ucfirst($data->getEveApiSectionName()), |
|
|
|
|
|
|
96
|
|
|
$data->getEveApiName(), |
|
97
|
|
|
$this->getTwigExtension()); |
|
98
|
|
|
} catch (YapealFileSystemException $exc) { |
|
99
|
|
|
$mess = 'Failed to find accessible twig template file during'; |
|
100
|
|
|
$yem->triggerLogEvent('Yapeal.Log.log', |
|
101
|
|
|
Logger::WARNING, |
|
102
|
|
|
$this->createEventMessage($mess, $data, $eventName), |
|
103
|
|
|
['exception' => $exc]); |
|
104
|
|
|
return false; |
|
105
|
|
|
} |
|
106
|
|
|
$mess = sprintf('Using %1$s template file in twig to create file of', $templateName); |
|
107
|
|
|
$yem->triggerLogEvent('Yapeal.Log.log', Logger::DEBUG, $this->createEveApiMessage($mess, $data)); |
|
108
|
|
|
try { |
|
109
|
|
|
$contents = $this->getTwig() |
|
110
|
|
|
->render($templateName, $context); |
|
111
|
|
|
} catch (\Twig_Error $exc) { |
|
112
|
|
|
$mess = 'Creation of file failed because of twig exception during'; |
|
113
|
|
|
$yem->triggerLogEvent('Yapeal.Log.log', |
|
114
|
|
|
Logger::WARNING, |
|
115
|
|
|
$this->createEventMessage($mess, $data, $eventName), |
|
116
|
|
|
['exception' => $exc]); |
|
117
|
|
|
return false; |
|
118
|
|
|
} |
|
119
|
|
|
return $contents; |
|
120
|
|
|
} |
|
121
|
|
|
/** |
|
122
|
|
|
* @return Twig_Environment |
|
123
|
|
|
* @throws \LogicException |
|
124
|
|
|
*/ |
|
125
|
|
|
protected function getTwig(): Twig_Environment |
|
126
|
|
|
{ |
|
127
|
|
|
if (null === $this->twig) { |
|
128
|
|
|
$mess = 'Tried to use Twig before it was set'; |
|
129
|
|
|
throw new \LogicException($mess); |
|
130
|
|
|
} |
|
131
|
|
|
return $this->twig; |
|
132
|
|
|
} |
|
133
|
|
|
/** |
|
134
|
|
|
* @return string |
|
135
|
|
|
* @throws \LogicException |
|
136
|
|
|
*/ |
|
137
|
|
|
protected function getTwigExtension(): string |
|
138
|
|
|
{ |
|
139
|
|
|
if (null === $this->twigExtension) { |
|
|
|
|
|
|
140
|
|
|
$mess = 'Tried to use twig file extension before it was set'; |
|
141
|
|
|
throw new \LogicException($mess); |
|
142
|
|
|
} |
|
143
|
|
|
return $this->twigExtension; |
|
144
|
|
|
} |
|
145
|
|
|
/** |
|
146
|
|
|
* Used to decide if existing file should be overwritten. |
|
147
|
|
|
* |
|
148
|
|
|
* @var bool $overwrite |
|
149
|
|
|
*/ |
|
150
|
|
|
private $overwrite = false; |
|
151
|
|
|
/** |
|
152
|
|
|
* @var Twig_Environment $twig |
|
153
|
|
|
*/ |
|
154
|
|
|
private $twig; |
|
155
|
|
|
} |
|
156
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.