EC-CUBE /
ec-cube
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Eccube\Application; |
||
| 4 | |||
| 5 | use Eccube\Event\TemplateEvent; |
||
| 6 | use Monolog\Logger; |
||
| 7 | use Symfony\Component\Form\FormBuilder; |
||
| 8 | use Symfony\Component\HttpFoundation\Response; |
||
| 9 | use Symfony\Component\HttpFoundation\StreamedResponse; |
||
| 10 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
||
| 11 | use Symfony\Component\Security\Core\User\UserInterface; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * TODO Traitが使えるようになったら不要になる |
||
| 15 | */ |
||
| 16 | class ApplicationTrait extends \Silex\Application |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Application Shortcut Methods |
||
| 20 | */ |
||
| 21 | 16 | public function addSuccess($message, $namespace = 'front') |
|
| 22 | { |
||
| 23 | $this['session']->getFlashBag()->add('eccube.' . $namespace . '.success', $message); |
||
| 24 | 16 | } |
|
| 25 | |||
| 26 | 2 | public function addError($message, $namespace = 'front') |
|
| 27 | { |
||
| 28 | $this['session']->getFlashBag()->add('eccube.' . $namespace . '.error', $message); |
||
| 29 | 2 | } |
|
| 30 | |||
| 31 | public function addDanger($message, $namespace = 'front') |
||
| 32 | { |
||
| 33 | $this['session']->getFlashBag()->add('eccube.' . $namespace . '.danger', $message); |
||
| 34 | } |
||
| 35 | |||
| 36 | 1 | public function addWarning($message, $namespace = 'front') |
|
| 37 | { |
||
| 38 | $this['session']->getFlashBag()->add('eccube.' . $namespace . '.warning', $message); |
||
| 39 | 1 | } |
|
| 40 | |||
| 41 | public function addInfo($message, $namespace = 'front') |
||
| 42 | { |
||
| 43 | $this['session']->getFlashBag()->add('eccube.' . $namespace . '.info', $message); |
||
| 44 | } |
||
| 45 | |||
| 46 | public function addRequestError($message, $namespace = 'front') |
||
| 47 | { |
||
| 48 | $this['session']->getFlashBag()->set('eccube.' . $namespace . '.request.error', $message); |
||
| 49 | } |
||
| 50 | |||
| 51 | public function clearMessage() |
||
| 52 | { |
||
| 53 | $this['session']->getFlashBag()->clear(); |
||
| 54 | } |
||
| 55 | |||
| 56 | public function deleteMessage() |
||
| 57 | { |
||
| 58 | $this->clearMessage(); |
||
| 59 | $this->addWarning('admin.delete.warning', 'admin'); |
||
| 60 | } |
||
| 61 | |||
| 62 | public function setLoginTargetPath($targetPath) |
||
| 63 | { |
||
| 64 | $this['session']->getFlashBag()->set('eccube.login.target.path', $targetPath); |
||
| 65 | } |
||
| 66 | |||
| 67 | public function isAdminRequest() |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 68 | { |
||
| 69 | return $this['admin']; |
||
| 70 | } |
||
| 71 | |||
| 72 | public function isFrontRequest() |
||
|
0 ignored issues
–
show
|
|||
| 73 | { |
||
| 74 | return $this['front']; |
||
| 75 | } |
||
| 76 | |||
| 77 | /* |
||
| 78 | * 注意!以下コードはSilexのコードのコピーなので触らないコト |
||
| 79 | * |
||
| 80 | * 以下のコードの著作権について |
||
| 81 | * |
||
| 82 | * (c) Fabien Potencier <[email protected]> |
||
| 83 | * |
||
| 84 | * For the full copyright and license information, please view the silex |
||
| 85 | * LICENSE file that was distributed with this source code. |
||
| 86 | */ |
||
| 87 | |||
| 88 | /** FormTrait */ |
||
| 89 | /** |
||
| 90 | * Creates and returns a form builder instance |
||
| 91 | * |
||
| 92 | * @param mixed $data The initial data for the form |
||
| 93 | * @param array $options Options for the form |
||
| 94 | * |
||
| 95 | * @return FormBuilder |
||
| 96 | */ |
||
| 97 | public function form($data = null, array $options = array()) |
||
| 98 | { |
||
| 99 | return $this['form.factory']->createBuilder('form', $data, $options); |
||
| 100 | } |
||
| 101 | |||
| 102 | /** MonologTrait */ |
||
| 103 | /** |
||
| 104 | * Adds a log record. |
||
| 105 | * |
||
| 106 | * @param string $message The log message |
||
| 107 | * @param array $context The log context |
||
| 108 | * @param int $level The logging level |
||
| 109 | * |
||
| 110 | * @return bool Whether the record has been processed |
||
| 111 | */ |
||
| 112 | 8 | public function log($message, array $context = array(), $level = Logger::INFO) |
|
| 113 | { |
||
| 114 | return $this['monolog']->addRecord($level, $message, $context); |
||
| 115 | 8 | } |
|
| 116 | |||
| 117 | /** SecurityTrait */ |
||
| 118 | /** |
||
| 119 | * Gets a user from the Security context. |
||
| 120 | * |
||
| 121 | * @return mixed |
||
| 122 | * |
||
| 123 | * @see TokenInterface::getUser() |
||
| 124 | * |
||
| 125 | */ |
||
| 126 | public function user() |
||
| 127 | { |
||
| 128 | return $this['user']; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Encodes the raw password. |
||
| 133 | * |
||
| 134 | * @param UserInterface $user A UserInterface instance |
||
| 135 | * @param string $password The password to encode |
||
| 136 | * |
||
| 137 | * @return string The encoded password |
||
| 138 | * |
||
| 139 | * @throws \RuntimeException when no password encoder could be found for the user |
||
| 140 | */ |
||
| 141 | 1 | public function encodePassword(UserInterface $user, $password) |
|
| 142 | { |
||
| 143 | return $this['security.encoder_factory']->getEncoder($user)->encodePassword($password, $user->getSalt()); |
||
| 144 | 1 | } |
|
| 145 | |||
| 146 | /** |
||
| 147 | * Checks if the attributes are granted against the current authentication token and optionally supplied object. |
||
| 148 | * |
||
| 149 | * @param mixed $attributes |
||
| 150 | * @param mixed $object |
||
| 151 | * |
||
| 152 | * @return bool |
||
| 153 | * |
||
| 154 | * @throws AuthenticationCredentialsNotFoundException when the token storage has no authentication token. |
||
| 155 | */ |
||
| 156 | 16 | public function isGranted($attributes, $object = null) |
|
| 157 | { |
||
| 158 | return $this['security.authorization_checker']->isGranted($attributes, $object); |
||
| 159 | 16 | } |
|
| 160 | |||
| 161 | /** SwiftmailerTrait */ |
||
| 162 | /** |
||
| 163 | * Sends an email. |
||
| 164 | * |
||
| 165 | * @param \Swift_Message $message A \Swift_Message instance |
||
| 166 | * @param array $failedRecipients An array of failures by-reference |
||
| 167 | * |
||
| 168 | * @return int The number of sent messages |
||
| 169 | */ |
||
| 170 | 23 | public function mail(\Swift_Message $message, &$failedRecipients = null) |
|
| 171 | { |
||
| 172 | return $this['mailer']->send($message, $failedRecipients); |
||
| 173 | 23 | } |
|
| 174 | |||
| 175 | /** TranslationTrait */ |
||
| 176 | /** |
||
| 177 | * Translates the given message. |
||
| 178 | * |
||
| 179 | * @param string $id The message id |
||
|
0 ignored issues
–
show
|
|||
| 180 | * @param array $parameters An array of parameters for the message |
||
|
0 ignored issues
–
show
|
|||
| 181 | * @param string $domain The domain for the message |
||
|
0 ignored issues
–
show
|
|||
| 182 | * @param string $locale The locale |
||
|
0 ignored issues
–
show
|
|||
| 183 | * |
||
| 184 | * @return string The translated string |
||
| 185 | */ |
||
| 186 | public function trans($id, array $parameters = array(), $domain = 'messages', $locale = null) |
||
| 187 | { |
||
| 188 | return $this['translator']->trans($id, $parameters, $domain, $locale); |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Translates the given choice message by choosing a translation according to a number. |
||
| 193 | * |
||
| 194 | * @param string $id The message id |
||
|
0 ignored issues
–
show
|
|||
| 195 | * @param int $number The number to use to find the indice of the message |
||
| 196 | * @param array $parameters An array of parameters for the message |
||
|
0 ignored issues
–
show
|
|||
| 197 | * @param string $domain The domain for the message |
||
|
0 ignored issues
–
show
|
|||
| 198 | * @param string $locale The locale |
||
|
0 ignored issues
–
show
|
|||
| 199 | * |
||
| 200 | * @return string The translated string |
||
| 201 | */ |
||
| 202 | public function transChoice($id, $number, array $parameters = array(), $domain = 'messages', $locale = null) |
||
| 203 | { |
||
| 204 | return $this['translator']->transChoice($id, $number, $parameters, $domain, $locale); |
||
| 205 | } |
||
| 206 | |||
| 207 | /** TwigTrait */ |
||
| 208 | /** |
||
| 209 | * Renders a view and returns a Response. |
||
| 210 | * |
||
| 211 | * To stream a view, pass an instance of StreamedResponse as a third argument. |
||
| 212 | * |
||
| 213 | * @param string $view The view name |
||
|
0 ignored issues
–
show
|
|||
| 214 | * @param array $parameters An array of parameters to pass to the view |
||
| 215 | * @param Response $response A Response instance |
||
| 216 | * |
||
| 217 | * @return Response A Response instance |
||
| 218 | */ |
||
| 219 | 60 | public function render($view, array $parameters = array(), Response $response = null) |
|
| 220 | { |
||
| 221 | $twig = $this['twig']; |
||
| 222 | |||
| 223 | // twigファイルのソースコードを読み込み, 文字列化. |
||
| 224 | $source = $twig->getLoader()->getSource($view); |
||
| 225 | |||
| 226 | // イベントの実行. |
||
| 227 | // プラグインにはテンプレートファイル名、文字列化されたtwigファイル、パラメータを渡す |
||
| 228 | $event = new TemplateEvent($view, $source, $parameters, $response); |
||
| 229 | |||
| 230 | 60 | $eventName = $view; |
|
| 231 | if ($this->isAdminRequest()) { |
||
| 232 | // 管理画面の場合、event名に「admin.」を付ける |
||
| 233 | 57 | $eventName = 'admin.' . $view; |
|
|
0 ignored issues
–
show
|
|||
| 234 | } |
||
| 235 | $this['monolog']->debug('Template Event Name : ' . $eventName); |
||
|
0 ignored issues
–
show
|
|||
| 236 | |||
| 237 | $this['eccube.event.dispatcher']->dispatch($eventName, $event); |
||
| 238 | |||
| 239 | 60 | if ($response instanceof StreamedResponse) { |
|
| 240 | $response->setCallback(function () use ($twig, $view, $parameters) { |
||
| 241 | $twig->display($view, $parameters); |
||
| 242 | }); |
||
| 243 | } else { |
||
| 244 | 60 | if (null === $response) { |
|
| 245 | $response = new Response(); |
||
| 246 | } |
||
| 247 | |||
| 248 | // プラグインで変更された文字列から, テンプレートオブジェクトを生成 |
||
| 249 | $template = $twig->createTemplate($event->getSource()); |
||
| 250 | |||
| 251 | // レンダリング実行. |
||
| 252 | $content = $template->render($event->getParameters()); |
||
| 253 | $response->setContent($content); |
||
| 254 | } |
||
| 255 | |||
| 256 | 60 | return $response; |
|
| 257 | 60 | } |
|
| 258 | |||
| 259 | /** |
||
| 260 | * Renders a view. |
||
| 261 | * |
||
| 262 | * @param string $view The view name |
||
|
0 ignored issues
–
show
|
|||
| 263 | * @param array $parameters An array of parameters to pass to the view |
||
|
0 ignored issues
–
show
|
|||
| 264 | * |
||
| 265 | * @return string The rendered view |
||
| 266 | */ |
||
| 267 | 20 | public function renderView($view, array $parameters = array()) |
|
| 268 | { |
||
| 269 | return $this['twig']->render($view, $parameters); |
||
| 270 | 20 | } |
|
| 271 | |||
| 272 | /** UrlGeneratorTrait */ |
||
| 273 | /** |
||
| 274 | * Generates a path from the given parameters. |
||
| 275 | * |
||
| 276 | * @param string $route The name of the route |
||
|
0 ignored issues
–
show
|
|||
| 277 | * @param mixed $parameters An array of parameters |
||
|
0 ignored issues
–
show
|
|||
| 278 | * |
||
| 279 | * @return string The generated path |
||
| 280 | */ |
||
| 281 | 40 | public function path($route, $parameters = array()) |
|
| 282 | { |
||
| 283 | return $this['url_generator']->generate($route, $parameters, UrlGeneratorInterface::ABSOLUTE_PATH); |
||
| 284 | 40 | } |
|
| 285 | |||
| 286 | /** |
||
| 287 | * Generates an absolute URL from the given parameters. |
||
| 288 | * |
||
| 289 | * @param string $route The name of the route |
||
|
0 ignored issues
–
show
|
|||
| 290 | * @param mixed $parameters An array of parameters |
||
|
0 ignored issues
–
show
|
|||
| 291 | * |
||
| 292 | * @return string The generated URL |
||
| 293 | */ |
||
| 294 | 57 | public function url($route, $parameters = array()) |
|
| 295 | { |
||
| 296 | return $this['url_generator']->generate($route, $parameters, UrlGeneratorInterface::ABSOLUTE_URL); |
||
| 297 | 57 | } |
|
| 298 | } |
||
| 299 |