Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with settings factory #74

Merged
merged 3 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export COMPOSE_PROJECT_NAME=settings
export MIGRATIONS_NAMESPACE=MonsieurBiz\\SyliusSettingsPlugin\\Migrations
export USER_UID=$(shell id -u)
PLUGIN_NAME=sylius-${COMPOSE_PROJECT_NAME}-plugin
COMPOSE=docker-compose
COMPOSE=docker compose
YARN=yarn

###
Expand Down
6 changes: 3 additions & 3 deletions src/Command/SetSettingsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$settings = $this->settingsRegistry->getByAlias($alias);

if (null === $settings) {
throw new SettingsException(sprintf('The alias "%s" is not valid.', $alias));
throw new SettingsException(\sprintf('The alias "%s" is not valid.', $alias));
}

['vendor' => $vendor, 'plugin' => $plugin] = $settings->getAliasAsArray();
Expand All @@ -114,12 +114,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->settingManager->persist($setting);
$this->settingManager->flush();
} catch (Exception $e) {
$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
$output->writeln(\sprintf('<error>%s</error>', $e->getMessage()));

return Command::FAILURE;
}

$output->writeln(sprintf('<info>%s</info>', 'The setting has been saved'));
$output->writeln(\sprintf('<info>%s</info>', 'The setting has been saved'));

return Command::SUCCESS;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function formAction(Request $request, RegistryInterface $registry, string
[
'settings' => $settings,
'form_event' => 'monsieurbiz.settings.form',
'form_event_dedicated' => sprintf('monsieurbiz.settings.form.%s', $settings->getAlias()),
'form_event_dedicated' => \sprintf('monsieurbiz.settings.form.%s', $settings->getAlias()),
'form' => $form->createView(),
]
);
Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjection/InstantiateSettingsPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private function validateSettingsResource(string $class): void
{
$classImplements = (array) (class_implements($class) ?: []);
if (!\in_array(SettingsInterface::class, $classImplements, true)) {
throw new InvalidArgumentException(sprintf('Class "%s" must implement "%s" to be registered as a Settings resource.', $class, SettingsInterface::class));
throw new InvalidArgumentException(\sprintf('Class "%s" must implement "%s" to be registered as a Settings resource.', $class, SettingsInterface::class));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Entity/Setting/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ private function getTypeFromValue($value): string

$type = \gettype($value);
if (!isset($types[$type])) {
throw new LogicException(sprintf('Impossible to match the type of the value. (%s)', $type));
throw new LogicException(\sprintf('Impossible to match the type of the value. (%s)', $type));
}

return $types[$type]($value); /** @phpstan-ignore-line */
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/SettingsAlreadyExistsException.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ final class SettingsAlreadyExistsException extends SettingsException
public function __construct(SettingsInterface $settings)
{
parent::__construct(
sprintf("Settings instance aliased '%s' already exists.", $settings->getAlias())
\sprintf("Settings instance aliased '%s' already exists.", $settings->getAlias())
);
}
}
10 changes: 5 additions & 5 deletions src/Processor/SettingsProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,16 @@ private function getChannelIdAndLocaleCodeFromSettingKey(string $settingKey): ar
{
switch (true) {
// Default website + Default locale
case sprintf('%1$s-%1$s', Settings::DEFAULT_KEY) === $settingKey:
case \sprintf('%1$s-%1$s', Settings::DEFAULT_KEY) === $settingKey:
return [null, null];
// Default website + locale
case 1 === preg_match(sprintf('`^%1$s-(?!%1$s)(?P<localeCode>.+)$`', Settings::DEFAULT_KEY), $settingKey, $matches):
case 1 === preg_match(\sprintf('`^%1$s-(?!%1$s)(?P<localeCode>.+)$`', Settings::DEFAULT_KEY), $settingKey, $matches):
return [null, $matches['localeCode']];
// Website + default locale
case 1 === preg_match(sprintf('`^channel-(?P<channelId>[0-9]+)-%1$s$`', Settings::DEFAULT_KEY), $settingKey, $matches):
case 1 === preg_match(\sprintf('`^channel-(?P<channelId>[0-9]+)-%1$s$`', Settings::DEFAULT_KEY), $settingKey, $matches):
return [(int) $matches['channelId'], null];
// Website + locale
case 1 === preg_match(sprintf('`^channel-(?P<channelId>[0-9]+)-(?!%1$s)(?P<localeCode>.+)$`', Settings::DEFAULT_KEY), $settingKey, $matches):
case 1 === preg_match(\sprintf('`^channel-(?P<channelId>[0-9]+)-(?!%1$s)(?P<localeCode>.+)$`', Settings::DEFAULT_KEY), $settingKey, $matches):
return [(int) $matches['channelId'], $matches['localeCode']];
default:
throw new LogicException("Format of the setting's key is incorrect.");
Expand Down Expand Up @@ -100,7 +100,7 @@ private function removeUnusedSettings(array &$data, array $settings): void
// Manage defaults, and remove actual settings with "use default value" checked
foreach ($data as $key => $value) {
// Is the setting a "use default value"?
if (1 === preg_match(sprintf('`^(?P<key>.*)(?:___%1$s)$`', Settings::DEFAULT_KEY), $key, $matches)) {
if (1 === preg_match(\sprintf('`^(?P<key>.*)(?:___%1$s)$`', Settings::DEFAULT_KEY), $key, $matches)) {
if (true === $value) {
if (isset($settings[$matches['key']])) {
$this->entityManager->remove($settings[$matches['key']]);
Expand Down
2 changes: 1 addition & 1 deletion src/Provider/SettingProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function validateType(?string $type): void
{
$types = Setting::getAllStorageTypes();
if (!\in_array($type, $types, true)) {
throw new Exception(sprintf('The type "%s" is not valid. Valid types are: %s', $type, implode(', ', $types)));
throw new Exception(\sprintf('The type "%s" is not valid. Valid types are: %s', $type, implode(', ', $types)));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Provider/SettingsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ public function getSettingValueByChannelAndLocale(
return $settingsInstance->getCurrentValue($channel, $locale, $path);
}

throw new SettingsException(sprintf('Cannot fetch setting %s - %s', $alias, $path));
throw new SettingsException(\sprintf('Cannot fetch setting %s - %s', $alias, $path));
}
}
4 changes: 4 additions & 0 deletions src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ services:
class: MonsieurBiz\SyliusSettingsPlugin\Provider\SettingsProvider

MonsieurBiz\SyliusSettingsPlugin\Provider\SettingsProviderInterface: '@monsieurbiz.settings.provider'

MonsieurBiz\SyliusSettingsPlugin\Factory\SettingFactory:
arguments:
$className: 'MonsieurBiz\SyliusSettingsPlugin\Entity\Setting\Setting'
8 changes: 4 additions & 4 deletions src/Settings/Metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function getName(bool $aliased = false): string
public function getParameter(string $name): mixed
{
if (!$this->hasParameter($name)) {
throw new InvalidArgumentException(sprintf('Parameter "%s" is not configured for resource "%s".', $name, $this->getAlias()));
throw new InvalidArgumentException(\sprintf('Parameter "%s" is not configured for resource "%s".', $name, $this->getAlias()));
}

return $this->parameters[$name];
Expand All @@ -95,7 +95,7 @@ public function getDefaultValues(): array
public function getClass(string $name): string
{
if (!$this->hasClass($name)) {
throw new InvalidArgumentException(sprintf('Class "%s" is not configured for resource "%s".', $name, $this->getAlias()));
throw new InvalidArgumentException(\sprintf('Class "%s" is not configured for resource "%s".', $name, $this->getAlias()));
}

return $this->parameters['classes'][$name];
Expand All @@ -108,13 +108,13 @@ public function hasClass(string $name): bool

public function getServiceId(string $serviceName): string
{
return sprintf('%s.%s.%s', $this->applicationName, $serviceName, $this->alias($this->name));
return \sprintf('%s.%s.%s', $this->applicationName, $serviceName, $this->alias($this->name));
}

private static function parseAlias(string $alias): array
{
if (!str_contains($alias, '.')) {
throw new InvalidArgumentException(sprintf('Invalid alias "%s" supplied, it should conform to the following format "<applicationName>.<name>".', $alias));
throw new InvalidArgumentException(\sprintf('Invalid alias "%s" supplied, it should conform to the following format "<applicationName>.<name>".', $alias));
}

return explode('.', $alias);
Expand Down
2 changes: 1 addition & 1 deletion src/Settings/Metadata/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final class Registry implements RegistryInterface
public function get(string $alias): MetadataInterface
{
if (!\array_key_exists($alias, $this->metadata)) {
throw new InvalidArgumentException(sprintf('Resource "%s" does not exist.', $alias));
throw new InvalidArgumentException(\sprintf('Resource "%s" does not exist.', $alias));
}

return $this->metadata[$alias];
Expand Down
10 changes: 5 additions & 5 deletions src/Settings/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function getFormClass(): string
$className = $this->metadata->getClass('form');
$parentClassNames = (array) (class_parents($className) ?: []);
if (!\in_array(AbstractSettingsType::class, $parentClassNames, true)) {
throw new SettingsException(sprintf('Class %s should extend %s', $className, AbstractSettingsType::class));
throw new SettingsException(\sprintf('Class %s should extend %s', $className, AbstractSettingsType::class));
}

return $className;
Expand Down Expand Up @@ -220,10 +220,10 @@ private function getCacheTags(?ChannelInterface $channel, ?string $localeCode, a
{
return array_merge([
$this->getAlias(),
sprintf('vendor.%s', $this->getAliasAsArray()['vendor']),
sprintf('plugin.%s', $this->getAliasAsArray()['plugin']),
sprintf('channel.%s', $channel?->getCode() ?? self::DEFAULT_KEY),
sprintf('locale.%s', $localeCode ?? self::DEFAULT_KEY),
\sprintf('vendor.%s', $this->getAliasAsArray()['vendor']),
\sprintf('plugin.%s', $this->getAliasAsArray()['plugin']),
\sprintf('channel.%s', $channel?->getCode() ?? self::DEFAULT_KEY),
\sprintf('locale.%s', $localeCode ?? self::DEFAULT_KEY),
], $extra);
}
}
Loading