forked from Sylius/SyliusFlowBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
171 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\FlowBundle\Symfony; | ||
|
||
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag; | ||
|
||
/** | ||
* This class provides structured storage of session attributes using | ||
* a name spacing character in the key. | ||
*/ | ||
class NamespacedAttributeBag extends AttributeBag | ||
{ | ||
/** | ||
* @param string $storageKey Session storage key | ||
* @param string $namespaceCharacter Namespace character to use in keys | ||
*/ | ||
public function __construct(string $storageKey = '_sf2_attributes', private string $namespaceCharacter = '/') | ||
{ | ||
parent::__construct($storageKey); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function has(string $name): bool | ||
{ | ||
// reference mismatch: if fixed, re-introduced in array_key_exists; keep as it is | ||
$attributes = $this->resolveAttributePath($name); | ||
$name = $this->resolveKey($name); | ||
|
||
if (null === $attributes) { | ||
return false; | ||
} | ||
|
||
return \array_key_exists($name, $attributes); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function get(string $name, mixed $default = null): mixed | ||
Check failure on line 42 in src/Symfony/NamespacedAttributeBag.php GitHub Actions / Test (8.0)ReservedWord
Check failure on line 42 in src/Symfony/NamespacedAttributeBag.php GitHub Actions / Test (8.0)ReservedWord
Check failure on line 42 in src/Symfony/NamespacedAttributeBag.php GitHub Actions / Test (8.1)ReservedWord
|
||
{ | ||
// reference mismatch: if fixed, re-introduced in array_key_exists; keep as it is | ||
$attributes = $this->resolveAttributePath($name); | ||
$name = $this->resolveKey($name); | ||
|
||
if (null === $attributes) { | ||
return $default; | ||
} | ||
|
||
return \array_key_exists($name, $attributes) ? $attributes[$name] : $default; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function set(string $name, mixed $value) | ||
Check failure on line 58 in src/Symfony/NamespacedAttributeBag.php GitHub Actions / Test (8.0)ReservedWord
|
||
{ | ||
$attributes = &$this->resolveAttributePath($name, true); | ||
$name = $this->resolveKey($name); | ||
$attributes[$name] = $value; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function remove(string $name): mixed | ||
Check failure on line 68 in src/Symfony/NamespacedAttributeBag.php GitHub Actions / Test (8.0)ReservedWord
|
||
{ | ||
$retval = null; | ||
$attributes = &$this->resolveAttributePath($name); | ||
$name = $this->resolveKey($name); | ||
if (null !== $attributes && \array_key_exists($name, $attributes)) { | ||
$retval = $attributes[$name]; | ||
unset($attributes[$name]); | ||
} | ||
|
||
return $retval; | ||
} | ||
|
||
/** | ||
* Resolves a path in attributes property and returns it as a reference. | ||
* | ||
* This method allows structured namespacing of session attributes. | ||
* | ||
* @param string $name Key name | ||
* @param bool $writeContext Write context, default false | ||
* | ||
* @return array|null | ||
*/ | ||
protected function &resolveAttributePath(string $name, bool $writeContext = false) | ||
{ | ||
$array = &$this->attributes; | ||
$name = (0 === strpos($name, $this->namespaceCharacter)) ? substr($name, 1) : $name; | ||
|
||
// Check if there is anything to do, else return | ||
if (!$name) { | ||
return $array; | ||
} | ||
|
||
$parts = explode($this->namespaceCharacter, $name); | ||
if (\count($parts) < 2) { | ||
if (!$writeContext) { | ||
return $array; | ||
} | ||
|
||
$array[$parts[0]] = []; | ||
|
||
return $array; | ||
} | ||
|
||
unset($parts[\count($parts) - 1]); | ||
|
||
foreach ($parts as $part) { | ||
if (null !== $array && !\array_key_exists($part, $array)) { | ||
if (!$writeContext) { | ||
$null = null; | ||
|
||
return $null; | ||
} | ||
|
||
$array[$part] = []; | ||
} | ||
|
||
$array = &$array[$part]; | ||
} | ||
|
||
return $array; | ||
} | ||
|
||
/** | ||
* Resolves the key from the name. | ||
* | ||
* This is the last part in a dot separated string. | ||
*/ | ||
protected function resolveKey(string $name): string | ||
{ | ||
if (false !== $pos = strrpos($name, $this->namespaceCharacter)) { | ||
$name = substr($name, $pos + 1); | ||
} | ||
|
||
return $name; | ||
} | ||
} |