-
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
11 changed files
with
283 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.idea | ||
composer.lock | ||
vendor |
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 |
---|---|---|
@@ -1,2 +1,5 @@ | ||
# utils | ||
Collection of utilites for easier development and app running | ||
# Dev Portal / Utils | ||
Collection of utilities for easier development and app running | ||
|
||
## Documentation | ||
Documentation can be found [here](https://github.com/dev-portal/utils/blob/master/docs/en/index.md) |
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,24 @@ | ||
{ | ||
"name": "dev-portal/utils", | ||
"description": "Collection of utilities for easier development and app running", | ||
"type": "library", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Tomáš Blatný", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"nette/di": "^2.3", | ||
"nette/security": "^2.3" | ||
}, | ||
"require-dev": { | ||
"nette/tester": "^1.6" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"DevPortal\\Utils\\": "src/Utils/" | ||
} | ||
} | ||
} |
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,39 @@ | ||
# Dev Portal / Utils | ||
|
||
## Installation | ||
|
||
Install using composer | ||
|
||
```sh | ||
$ composer require dev-portal/utils | ||
``` | ||
|
||
## Modules | ||
|
||
### Namespace for UserStorage | ||
|
||
This module provides easy configuration of namespace for IUserStorage for Nette Framework. | ||
|
||
First enable it by registering extension into your config neon: | ||
|
||
```yml | ||
extensions: | ||
userNamespace: DevPortal\Utils\DI\UserNamespaceExtension | ||
``` | ||
|
||
You can then configure it: | ||
|
||
```yml | ||
userNamespace: | ||
type: auto | ||
namespace: foo.bar | ||
``` | ||
|
||
`type` attribute can be: | ||
|
||
- `config` or `value` - uses directly namespace from `namespace` attribute | ||
- `dir` or `directory` - uses namespace dependent on absolute path of your project (`%wwwDir%`) | ||
- `auto` - if `namespace` is set, uses it, otherwise uses absolute path of your project | ||
|
||
You can also write your own `IUserNamespaceResolver` and register it under `resolverClass` attribute. | ||
This overrides predefined resolvers. |
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,98 @@ | ||
<?php | ||
/** | ||
* @author Tomáš Blatný | ||
*/ | ||
|
||
namespace DevPortal\Utils\DI; | ||
|
||
use DevPortal\Utils\Security\IUserNamespaceResolver; | ||
use DevPortal\Utils\Security\NamespaceResolvers\ConfigResolver; | ||
use DevPortal\Utils\Security\NamespaceResolvers\DirectoryResolver; | ||
use Nette\DI\CompilerExtension; | ||
use Nette\DI\ServiceDefinition; | ||
use Nette\InvalidArgumentException; | ||
use Nette\Security\IUserStorage; | ||
|
||
|
||
class UserNamespaceExtension extends CompilerExtension | ||
{ | ||
|
||
private $defaults = [ | ||
'type' => 'auto', | ||
'namespace' => NULL, | ||
'resolverClass' => NULL, | ||
]; | ||
|
||
|
||
public function loadConfiguration() | ||
{ | ||
$config = $this->validateConfig($this->defaults); | ||
|
||
if (isset($config['resolverClass']) && $config['resolverClass']) { | ||
$this->getContainerBuilder() | ||
->addDefinition($this->prefix('resolver')) | ||
->setClass($config['resolverClass']); | ||
return; | ||
} | ||
|
||
switch ($config['type']) { | ||
case 'auto': | ||
if (isset($config['namespace']) && $config['namespace']) { | ||
$this->registerConfigResolver($config['namespace']); | ||
} else { | ||
$this->registerDirectoryResolver(); | ||
} | ||
break; | ||
case 'dir': | ||
case 'directory': | ||
$this->registerDirectoryResolver(); | ||
break; | ||
case 'config': | ||
case 'value': | ||
if (!$config['namespace']) { | ||
throw new InvalidArgumentException('Set configuration value ' . $this->name . '.namespace to use ConfigResolver.'); | ||
} | ||
$this->registerConfigResolver($config['namespace']); | ||
break; | ||
default: | ||
throw new InvalidArgumentException('Invalid configuration value ' . $this->name . '.type: ' . $config['type']); | ||
} | ||
} | ||
|
||
|
||
public function beforeCompile() | ||
{ | ||
$builder = $this->getContainerBuilder(); | ||
|
||
if ($name = $builder->getByType(IUserStorage::class)) { | ||
$builder->getDefinition($name) | ||
->addSetup('setNamespace', [$builder->getByType(IUserNamespaceResolver::class), 'resolve']); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* @param string $namespace | ||
* @return ServiceDefinition | ||
*/ | ||
private function registerConfigResolver($namespace) | ||
{ | ||
return $this->getContainerBuilder() | ||
->addDefinition($this->prefix('resolver')) | ||
->setClass(ConfigResolver::class) | ||
->setArguments([$namespace]); | ||
} | ||
|
||
|
||
/** | ||
* @return ServiceDefinition | ||
*/ | ||
private function registerDirectoryResolver() | ||
{ | ||
return $this->getContainerBuilder() | ||
->addDefinition($this->prefix('resolver')) | ||
->setClass(DirectoryResolver::class) | ||
->setArguments(['%wwwDir%']); | ||
} | ||
|
||
} |
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,14 @@ | ||
<?php | ||
/** | ||
* @author Tomáš Blatný | ||
*/ | ||
|
||
namespace DevPortal\Utils\Security; | ||
|
||
interface IUserNamespaceResolver | ||
{ | ||
|
||
/** @return string */ | ||
function resolve(); | ||
|
||
} |
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,31 @@ | ||
<?php | ||
/** | ||
* @author Tomáš Blatný | ||
*/ | ||
|
||
namespace DevPortal\Utils\Security\NamespaceResolvers; | ||
|
||
use DevPortal\Utils\Security\IUserNamespaceResolver; | ||
|
||
|
||
class ConfigResolver implements IUserNamespaceResolver | ||
{ | ||
|
||
|
||
/** @var string */ | ||
private $namespace; | ||
|
||
|
||
public function __construct($namespace) | ||
{ | ||
$this->namespace = (string) $namespace; | ||
} | ||
|
||
|
||
/** @return string */ | ||
public function resolve() | ||
{ | ||
return $this->namespace; | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
src/Utils/Security/NamespaceResolvers/DirectoryResolver.php
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,30 @@ | ||
<?php | ||
/** | ||
* @author Tomáš Blatný | ||
*/ | ||
|
||
namespace DevPortal\Utils\Security\NamespaceResolvers; | ||
|
||
use DevPortal\Utils\Security\IUserNamespaceResolver; | ||
|
||
|
||
class DirectoryResolver implements IUserNamespaceResolver | ||
{ | ||
|
||
/** @var string */ | ||
private $wwwDir; | ||
|
||
|
||
public function __construct($wwwDir) | ||
{ | ||
$this->wwwDir = $wwwDir; | ||
} | ||
|
||
|
||
/** @return string */ | ||
public function resolve() | ||
{ | ||
return str_replace(DIRECTORY_SEPARATOR, '.', $this->wwwDir); | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
tests/Utils/Security/NamespaceResolvers/ConfigResolver.phpt
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,15 @@ | ||
<?php | ||
/** | ||
* Test: ConfigResolver | ||
* | ||
* @author Tomáš Blatný | ||
*/ | ||
|
||
use DevPortal\Utils\Security\NamespaceResolvers\ConfigResolver; | ||
use Tester\Assert; | ||
|
||
require __DIR__ . "/../../../bootstrap.php"; | ||
|
||
$resolver = new ConfigResolver('abc'); | ||
|
||
Assert::equal('abc', $resolver->resolve()); |
16 changes: 16 additions & 0 deletions
16
tests/Utils/Security/NamespaceResolvers/DirectoryResolver.phpt
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,16 @@ | ||
<?php | ||
/** | ||
* Test: DirectoryResolver | ||
* | ||
* @author Tomáš Blatný | ||
*/ | ||
|
||
use DevPortal\Utils\Security\NamespaceResolvers\DirectoryResolver; | ||
use Tester\Assert; | ||
|
||
|
||
require __DIR__ . "/../../../bootstrap.php"; | ||
|
||
$resolver = new DirectoryResolver(__DIR__); | ||
|
||
Assert::equal(str_replace(DIRECTORY_SEPARATOR, '.', __DIR__), $resolver->resolve()); |
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,8 @@ | ||
<?php | ||
|
||
use Tester\Environment; | ||
|
||
|
||
require_once __DIR__ . '/../vendor/autoload.php'; | ||
|
||
Environment::setup(); |