1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved. |
5
|
|
|
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace SprykerShop\Yves\ShopApplication\Twig; |
9
|
|
|
|
10
|
|
|
use LogicException; |
11
|
|
|
use Silex\Application; |
12
|
|
|
use SprykerShop\Yves\ShopApplication\Dependency\Service\ShopApplicationToUtilTextServiceInterface; |
13
|
|
|
|
14
|
|
|
class RoutingHelper implements RoutingHelperInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var \Silex\Application |
18
|
|
|
*/ |
19
|
|
|
protected $app; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var \Spryker\Shared\Kernel\Store |
23
|
|
|
*/ |
24
|
|
|
protected $store; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var \SprykerShop\Yves\ShopApplication\Dependency\Service\ShopApplicationToUtilTextServiceInterface |
28
|
|
|
*/ |
29
|
|
|
protected $utilTextService; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param \Silex\Application $app |
33
|
|
|
* @param \SprykerShop\Yves\ShopApplication\Dependency\Service\ShopApplicationToUtilTextServiceInterface $utilTextService |
34
|
|
|
*/ |
35
|
|
|
public function __construct(Application $app, ShopApplicationToUtilTextServiceInterface $utilTextService) |
36
|
|
|
{ |
37
|
|
|
$this->app = $app; |
38
|
|
|
$this->utilTextService = $utilTextService; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $destination |
43
|
|
|
* |
44
|
|
|
* @throws \LogicException |
45
|
|
|
* |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
public function getRouteFromDestination($destination) |
49
|
|
|
{ |
50
|
|
|
if (strpos($destination, '::') !== false) { |
51
|
|
|
[$controllerNamespaceName, $actionName] = explode('::', $destination); |
52
|
|
|
} elseif (strpos($destination, ':') !== false) { |
53
|
|
|
[$serviceName, $actionName] = explode(':', $destination); |
54
|
|
|
$controllerNamespaceName = get_class($this->app[$serviceName]); |
55
|
|
|
} else { |
56
|
|
|
throw new LogicException('Cannot parse destination'); |
57
|
|
|
} |
58
|
|
|
[$namespace, $application, $module, $layer, $controllerName] = explode('\\', $controllerNamespaceName); |
59
|
|
|
|
60
|
|
|
$module = $this->resolveModuleName($module); |
61
|
|
|
|
62
|
|
|
$controller = $this->utilTextService->camelCaseToSeparator(str_replace('Controller', '', $controllerName)); |
63
|
|
|
$action = $this->utilTextService->camelCaseToSeparator((str_replace('Action', '', $actionName))); |
64
|
|
|
|
65
|
|
|
return $module . '/' . $controller . '/' . $action; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $moduleName |
70
|
|
|
* |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
protected function resolveModuleName(string $moduleName): string |
74
|
|
|
{ |
75
|
|
|
$codeBucketIdentifierLength = mb_strlen(APPLICATION_CODE_BUCKET); |
|
|
|
|
76
|
|
|
$codeBucketSuffix = mb_substr($moduleName, -$codeBucketIdentifierLength); |
77
|
|
|
|
78
|
|
|
if ($codeBucketSuffix === APPLICATION_CODE_BUCKET) { |
79
|
|
|
$moduleName = mb_substr($moduleName, 0, -$codeBucketIdentifierLength); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $moduleName; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|