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\Cli\EveApi; |
36
|
|
|
|
37
|
|
|
use Twig_Environment; |
38
|
|
|
use Yapeal\Event\EveApiEventEmitterTrait; |
39
|
|
|
use Yapeal\Exception\YapealFileSystemException; |
40
|
|
|
use Yapeal\FileSystem\CommonFileHandlingTrait; |
41
|
|
|
use Yapeal\FileSystem\RelativeFileSearchTrait; |
42
|
|
|
use Yapeal\Log\Logger; |
43
|
|
|
use Yapeal\Xml\EveApiReadWriteInterface; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Trait EveApiCreatorTrait |
47
|
|
|
*/ |
48
|
|
|
trait EveApiCreatorTrait |
49
|
|
|
{ |
50
|
|
|
use CommonFileHandlingTrait, EveApiEventEmitterTrait, RelativeFileSearchTrait; |
51
|
|
|
/** |
52
|
|
|
* Getter for $overwrite. |
53
|
|
|
* |
54
|
|
|
* @return boolean |
55
|
|
|
*/ |
56
|
|
|
public function isOverwrite(): bool |
57
|
|
|
{ |
58
|
|
|
return $this->overwrite; |
59
|
|
|
} |
60
|
|
|
/** |
61
|
|
|
* Fluent interface setter for $overwrite. |
62
|
|
|
* |
63
|
|
|
* @param bool $value |
64
|
|
|
* |
65
|
|
|
* @return self Fluent interface. |
66
|
|
|
*/ |
67
|
|
|
public function setOverwrite(bool $value = true) |
68
|
|
|
{ |
69
|
|
|
$this->overwrite = (bool)$value; |
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
/** |
73
|
|
|
* @param Twig_Environment $twig |
74
|
|
|
* |
75
|
|
|
* @return self Fluent interface. |
76
|
|
|
*/ |
77
|
|
|
public function setTwig(Twig_Environment $twig) |
78
|
|
|
{ |
79
|
|
|
$this->twig = $twig; |
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
/** |
83
|
|
|
* @param string $eventName |
84
|
|
|
* @param \Yapeal\Xml\EveApiReadWriteInterface $data |
85
|
|
|
* @param array $context |
86
|
|
|
* |
87
|
|
|
* @return bool|string |
88
|
|
|
* @throws \DomainException |
89
|
|
|
* @throws \InvalidArgumentException |
90
|
|
|
* @throws \LogicException |
91
|
|
|
*/ |
92
|
|
|
protected function getContentsFromTwig(string $eventName, EveApiReadWriteInterface $data, array $context) |
93
|
|
|
{ |
94
|
|
|
$yem = $this->getYem(); |
95
|
|
|
try { |
96
|
|
|
$templateName = $this->findRelativeFileWithPath(ucfirst($data->getEveApiSectionName()), |
97
|
|
|
$data->getEveApiName(), |
98
|
|
|
$this->getTwigExtension()); |
99
|
|
|
} catch (YapealFileSystemException $exc) { |
100
|
|
|
$mess = 'Failed to find accessible twig template file during'; |
101
|
|
|
$yem->triggerLogEvent('Yapeal.Log.log', |
102
|
|
|
Logger::WARNING, |
103
|
|
|
$this->createEventMessage($mess, $data, $eventName), |
104
|
|
|
['exception' => $exc]); |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
$mess = sprintf('Using %1$s template file in twig to create file of', $templateName); |
108
|
|
|
$yem->triggerLogEvent('Yapeal.Log.log', Logger::DEBUG, $this->createEveApiMessage($mess, $data)); |
109
|
|
|
try { |
110
|
|
|
$contents = $this->getTwig() |
111
|
|
|
->render($templateName, $context); |
112
|
|
|
} catch (\Twig_Error $exc) { |
113
|
|
|
$mess = 'Creation of file failed because of twig exception during'; |
114
|
|
|
$yem->triggerLogEvent('Yapeal.Log.log', |
115
|
|
|
Logger::WARNING, |
116
|
|
|
$this->createEventMessage($mess, $data, $eventName), |
117
|
|
|
['exception' => $exc]); |
118
|
|
|
return false; |
119
|
|
|
} |
120
|
|
|
return $contents; |
121
|
|
|
} |
122
|
|
|
/** |
123
|
|
|
* @return Twig_Environment |
124
|
|
|
* @throws \LogicException |
125
|
|
|
*/ |
126
|
|
|
protected function getTwig(): Twig_Environment |
127
|
|
|
{ |
128
|
|
|
if (null === $this->twig) { |
129
|
|
|
$mess = 'Tried to use Twig before it was set'; |
130
|
|
|
throw new \LogicException($mess); |
131
|
|
|
} |
132
|
|
|
return $this->twig; |
133
|
|
|
} |
134
|
|
|
/** |
135
|
|
|
* @return string |
136
|
|
|
* @throws \LogicException |
137
|
|
|
*/ |
138
|
|
|
protected function getTwigExtension(): string |
139
|
|
|
{ |
140
|
|
|
if (null === $this->twigExtension) { |
141
|
|
|
$mess = 'Tried to use twig file extension before it was set'; |
142
|
|
|
throw new \LogicException($mess); |
143
|
|
|
} |
144
|
|
|
return $this->twigExtension; |
145
|
|
|
} |
146
|
|
|
/** |
147
|
|
|
* Used to decide if existing file should be overwritten. |
148
|
|
|
* |
149
|
|
|
* @var bool $overwrite |
150
|
|
|
*/ |
151
|
|
|
private $overwrite = false; |
152
|
|
|
/** |
153
|
|
|
* @var Twig_Environment $twig |
154
|
|
|
*/ |
155
|
|
|
private $twig; |
156
|
|
|
/** |
157
|
|
|
* @var string twigExtension |
158
|
|
|
*/ |
159
|
|
|
private $twigExtension; |
160
|
|
|
} |
161
|
|
|
|