-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathis_shoppingcart.php
127 lines (109 loc) · 3.24 KB
/
is_shoppingcart.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
declare(strict_types=1);
if (!defined('_PS_VERSION_')) {
exit;
}
if (file_exists(__DIR__ . '/vendor/autoload.php')) {
require_once __DIR__ . '/vendor/autoload.php';
}
use Oksydan\IsShoppingcart\Configuration\ShoppingCartConfiguration;
use Oksydan\IsShoppingcart\Hook\HookInterface;
use PrestaShop\PrestaShop\Adapter\SymfonyContainer;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
class Is_shoppingcart extends Module
{
public function __construct()
{
$this->name = 'is_shoppingcart';
$this->tab = 'front_office_features';
$this->version = '3.0.1';
$this->author = 'Igor Stępień';
$this->need_instance = 0;
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->trans('Shopping cart', [], 'Modules.Isshoppingcart.Admin');
$this->description = $this->trans('Display a shopping cart icon on your pages and the number of items it contains.', [], 'Modules.Isshoppingcart.Admin');
$this->ps_versions_compliancy = ['min' => '8.0.0', 'max' => _PS_VERSION_];
$this->controllers = ['ajax'];
}
public function isUsingNewTranslationSystem(): bool
{
return true;
}
/**
* @return bool
*/
public function install(): bool
{
return parent::install()
&& $this->installHooks()
&& $this->installConfiguration();
}
/**
* @return bool
*/
public function installHooks(): bool
{
return $this->registerHook('displayTop')
&& $this->registerHook('displayBeforeBodyClosingTag')
&& $this->registerHook('actionFrontControllerSetMedia');
}
/**
* @return bool
*/
public function installConfiguration(): bool
{
return \Configuration::updateValue(ShoppingCartConfiguration::IS_BLOCK_CART_AJAX, 1);
}
/**
* @return bool
*/
public function uninstall(): bool
{
return parent::uninstall();
}
/** @param string $methodName */
public function __call($methodName, array $arguments)
{
if (str_starts_with($methodName, 'hook')) {
if ($hook = $this->getHookObject($methodName)) {
return $hook->execute(...$arguments);
}
} else {
return null;
}
}
/**
* @param string $methodName
*
* @return HookInterface|null
*/
private function getHookObject($methodName)
{
$serviceName = sprintf(
'oksydan.is_shoppingcart.hook.%s',
\Tools::toUnderscoreCase(str_replace('hook', '', $methodName))
);
$hook = $this->getService($serviceName);
return $hook instanceof HookInterface ? $hook : null;
}
/**
* @template T
*
* @param class-string<T>|string $serviceName
*
* @return T|object|null
*/
public function getService($serviceName)
{
try {
return $this->get($serviceName);
} catch (ServiceNotFoundException $exception) {
return null;
}
}
public function getContent(): void
{
\Tools::redirectAdmin(SymfonyContainer::getInstance()->get('router')->generate('is_shoppingcart_controller'));
}
}