added Bootstrap + Module + model class map helper + message translation config
This commit is contained in:
@ -8,24 +8,31 @@
|
||||
"email": "email@example.com"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=5.5"
|
||||
},
|
||||
"require-dev": {
|
||||
"squizlabs/php_codesniffer": "^2.7",
|
||||
"fabpot/php-cs-fixer": "^1.12",
|
||||
"phpmd/phpmd": "^2.4"
|
||||
"phpmd/phpmd": "^2.4",
|
||||
"dektrium/yii2-user": "^0.9.9",
|
||||
"dektrium/yii2-rbac": "^0.3.0"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Da\\" : ["./lib", "./tests/lib"]
|
||||
"Da\\": [
|
||||
"./lib",
|
||||
"./tests/lib"
|
||||
]
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"Da\\": ["./lib", "./tests/lib"]
|
||||
"Da\\": [
|
||||
"./lib",
|
||||
"./tests/lib"
|
||||
]
|
||||
}
|
||||
},
|
||||
"require": {
|
||||
"dektrium/yii2-user": "^0.9.9",
|
||||
"dektrium/yii2-rbac": "^0.3.0"
|
||||
},
|
||||
"minimum-stability": "dev"
|
||||
}
|
||||
|
||||
@ -1,49 +1,217 @@
|
||||
<?php
|
||||
|
||||
namespace Da\User;
|
||||
|
||||
use Da\User\Helper\AuthHelper;
|
||||
use Da\User\Query\AccountQuery;
|
||||
use Da\User\Query\ProfileQuery;
|
||||
use Da\User\Query\TokenQuery;
|
||||
use Da\User\Query\UserQuery;
|
||||
use Da\User\Helper\ClassMapHelper;
|
||||
use Yii;
|
||||
use yii\authclient\Collection;
|
||||
use yii\base\Application;
|
||||
use yii\base\BootstrapInterface;
|
||||
use Yii;
|
||||
use yii\base\Exception;
|
||||
use yii\console\Application as ConsoleApplication;
|
||||
use yii\i18n\PhpMessageSource;
|
||||
use yii\web\Application as WebApplication;
|
||||
|
||||
/**
|
||||
*
|
||||
* Bootstrap.php
|
||||
*
|
||||
* Date: 3/12/16
|
||||
* Time: 15:13
|
||||
* @author Antonio Ramirez <hola@2amigos.us>
|
||||
*/
|
||||
class Bootstrap implements BootstrapInterface
|
||||
{
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function bootstrap($app)
|
||||
{
|
||||
if ($app->hasModule('user') && $app->getModule('user') instanceof Module) {
|
||||
$classMap = $this->buildClassMap();
|
||||
$this->initContainer($classMap);
|
||||
$this->initTranslations($app);
|
||||
|
||||
// configure yii's container
|
||||
$this->setContainer();
|
||||
if ($app instanceof WebApplication) {
|
||||
$this->initUrlRoutes($app);
|
||||
$this->initAuthCollection($app);
|
||||
} else {
|
||||
/** @var $app ConsoleApplication */
|
||||
$this->initConsoleCommands($app);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function setContainer()
|
||||
/**
|
||||
* Initialize container with module classes
|
||||
*
|
||||
* @param array $map the previously built class map list
|
||||
*/
|
||||
protected function initContainer($map)
|
||||
{
|
||||
$di = Yii::$container;
|
||||
// helpers
|
||||
$di->set('Da\User\Helper\AuthHelper');
|
||||
|
||||
// email change strategy
|
||||
$di->set('Da\User\Strategy\DefaultEmailChangeStrategy');
|
||||
$di->set('Da\User\Strategy\InsecureEmailChangeStrategy');
|
||||
$di->set('Da\User\Strategy\SecureEmailChangeStrategy');
|
||||
|
||||
// active query classes
|
||||
Yii::$container->set('Da\User\Query\AccountQuery');
|
||||
Yii::$container->set('Da\User\Query\ProfileQuery');
|
||||
Yii::$container->set('Da\User\Query\TokenQuery');
|
||||
Yii::$container->set('Da\User\Query\UserQuery');
|
||||
// models + active query classes
|
||||
$modelClassMap = [];
|
||||
foreach ($map as $class => $definition) {
|
||||
|
||||
$di->set($class, $definition);
|
||||
$model = is_array($definition) ? $definition['class'] : $definition;
|
||||
$name = (substr($class, strrpos($class, '\\') + 1));
|
||||
$modelClassMap[$name] = $model;
|
||||
|
||||
if (in_array($name, ['User', 'Profile', 'Token', 'Account'])) {
|
||||
$di->set(
|
||||
$name . 'Query',
|
||||
function () use ($model) {
|
||||
return $model->find();
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// helpers
|
||||
$di->set('Da\User\Helper\AuthHelper');
|
||||
$di->setSingleton(ClassMapHelper::class, 'Da\User\Helper\ModelClassMapHelper', [$modelClassMap]);
|
||||
|
||||
if (php_sapi_name() !== 'cli') {
|
||||
// override Yii
|
||||
$di->set(
|
||||
'yii\web\User',
|
||||
[
|
||||
'enableAutoLogin' => true,
|
||||
'loginUrl' => ['/user/auth/login'],
|
||||
'identityClass' => $di->get(ClassMapHelper::class)->get('User')
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers module translation messages
|
||||
*
|
||||
* @param Application $app
|
||||
*/
|
||||
protected function initTranslations(Application $app)
|
||||
{
|
||||
if (!isset($app->get('i18n')->translations['user*'])) {
|
||||
$app->get('i18n')->translations['user*'] = [
|
||||
'class' => PhpMessageSource::class,
|
||||
'basePath' => __DIR__ . '/../i18n',
|
||||
'sourceLanguage' => 'en-US'
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes web url routes (rules in Yii2)
|
||||
*
|
||||
* @param WebApplication $app
|
||||
*/
|
||||
protected function initUrlRoutes(WebApplication $app)
|
||||
{
|
||||
/** @var $module Module */
|
||||
$module = $app->getModule('user');
|
||||
$config = [
|
||||
'class' => 'yii\web\GroupUrlRule',
|
||||
'prefix' => $module->prefix,
|
||||
'rules' => $module->routes,
|
||||
];
|
||||
|
||||
if ($module->prefix !== 'user') {
|
||||
$config['routePrefix'] = 'user';
|
||||
}
|
||||
|
||||
$rule = Yii::createObject($config);
|
||||
$app->getUrlManager()->addRules([$rule], false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures the authCollection component is configured.
|
||||
*
|
||||
* @param WebApplication $app
|
||||
*/
|
||||
protected function initAuthCollection(WebApplication $app)
|
||||
{
|
||||
if (!$app->has('authClientCollection')) {
|
||||
$app->set('authClientCollection', Collection::class);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers console commands to main app.
|
||||
*
|
||||
* @param ConsoleApplication $app
|
||||
*/
|
||||
protected function initConsoleCommands(ConsoleApplication $app)
|
||||
{
|
||||
$app->getModule('user')->controllerNamespace = 'Da\User\Command';
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds class map according to use configuration
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function buildClassMap()
|
||||
{
|
||||
$map = [];
|
||||
|
||||
$defaults = [
|
||||
'User' => 'Da\User\Model\User',
|
||||
'Account' => 'Da\User\Model\Account',
|
||||
'Profile' => 'Da\User\Model\Profile',
|
||||
'Token' => 'Da\User\Model\Token',
|
||||
// ---
|
||||
'UserSearch' => 'Da\User\Search\UserSearch',
|
||||
// ---
|
||||
'RegistrationForm' => 'Da\User\Form\RegistrationForm',
|
||||
'ResendForm' => 'Da\User\Form\ResendForm',
|
||||
'LoginForm' => 'Da\User\Form\LoginForm',
|
||||
'SettingsForm' => 'Da\User\Form\SettingsForm',
|
||||
'RecoveryForm' => 'Da\User\Form\RecoveryForm',
|
||||
];
|
||||
|
||||
$routes = [
|
||||
'Da\User\Model' => [
|
||||
'User',
|
||||
'Account',
|
||||
'Profile',
|
||||
'Token'
|
||||
],
|
||||
'Da\User\Search' => [
|
||||
'UserSearch'
|
||||
],
|
||||
'Da\UserForm' => [
|
||||
'RegistrationForm',
|
||||
'ResendForm',
|
||||
'LoginForm',
|
||||
'SettingsForm',
|
||||
'RecoveryForm',
|
||||
]
|
||||
];
|
||||
|
||||
foreach ($defaults as $name => $definition) {
|
||||
$map[$this->getRoute($routes, $name) . "\\$name"] = $definition;
|
||||
}
|
||||
|
||||
return $map;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the parent class name route of a short class name
|
||||
*
|
||||
* @param array $routes class name routes
|
||||
* @param string $name
|
||||
*
|
||||
* @return int|string
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function getRoute(array $routes, $name)
|
||||
{
|
||||
foreach ($routes as $route => $names) {
|
||||
if (in_array($name, $names)) {
|
||||
return $route;
|
||||
}
|
||||
}
|
||||
throw new Exception('Unknown configuration class name');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
49
lib/User/Helper/ClassMapHelper.php
Normal file
49
lib/User/Helper/ClassMapHelper.php
Normal file
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace Da\User\Helper;
|
||||
|
||||
/**
|
||||
*
|
||||
* ModelMapHelper.php
|
||||
*
|
||||
* Date: 3/12/16
|
||||
* Time: 18:10
|
||||
* @author Antonio Ramirez <hola@2amigos.us>
|
||||
*/
|
||||
class ClassMapHelper
|
||||
{
|
||||
protected $map = [];
|
||||
|
||||
/**
|
||||
* ModelClassMapHelper constructor.
|
||||
*
|
||||
* @param array $map
|
||||
*/
|
||||
public function __construct($map = [])
|
||||
{
|
||||
$this->map = $map;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param $class
|
||||
*/
|
||||
public function set($key, $class)
|
||||
{
|
||||
$this->map[$key] = $class;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
*
|
||||
* @return mixed
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function get($key)
|
||||
{
|
||||
if (array_key_exists($key, $this->map)) {
|
||||
return $this->map[$key];
|
||||
};
|
||||
throw new \Exception('Unknown model map key: ' . $key);
|
||||
}
|
||||
}
|
||||
@ -1,16 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace Da\User;
|
||||
|
||||
use Da\User\Strategy\DefaultEmailChangeStrategy;
|
||||
|
||||
/**
|
||||
*
|
||||
* Module.php
|
||||
*
|
||||
* Date: 3/12/16
|
||||
* Time: 15:15
|
||||
* @author Antonio Ramirez <hola@2amigos.us>
|
||||
*/
|
||||
class Module extends \yii\base\Module
|
||||
{
|
||||
/**
|
||||
@ -65,7 +58,7 @@ class Module extends \yii\base\Module
|
||||
*/
|
||||
public $routes = [
|
||||
'<id:\d+>' => 'profile/show',
|
||||
'<action:(login|logout)>' => 'security/<action>',
|
||||
'<action:(login|logout)>' => 'auth/<action>',
|
||||
'<action:(register|resend)>' => 'registration/<action>',
|
||||
'confirm/<id:\d+>/<code:[A-Za-z0-9_-]+>' => 'registration/confirm',
|
||||
'forgot' => 'recovery/request',
|
||||
|
||||
46
lib/i18n/message.php
Normal file
46
lib/i18n/message.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
'sourcePath' => __DIR__ . '/../User',
|
||||
'messagePath' => __DIR__,
|
||||
'languages' => [
|
||||
'ca',
|
||||
'da',
|
||||
'de',
|
||||
'de-DU',
|
||||
'es',
|
||||
'fa-IR',
|
||||
'fi',
|
||||
'fr',
|
||||
'hr',
|
||||
'hu',
|
||||
'it',
|
||||
'kz',
|
||||
'lt',
|
||||
'nl',
|
||||
'pl',
|
||||
'pt-BR',
|
||||
'pt-PT',
|
||||
'ro',
|
||||
'ru',
|
||||
'th',
|
||||
'tr_TR',
|
||||
'uk',
|
||||
'vi',
|
||||
'zh-CN',
|
||||
],
|
||||
'translator' => 'Yii::t',
|
||||
'sort' => false,
|
||||
'overwrite' => true,
|
||||
'removeUnused' => false,
|
||||
'only' => ['*.php'],
|
||||
'except' => [
|
||||
'.svn',
|
||||
'.git',
|
||||
'.gitignore',
|
||||
'.gitkeep',
|
||||
'.hgignore',
|
||||
'.hgkeep'
|
||||
],
|
||||
'format' => 'php',
|
||||
];
|
||||
Reference in New Issue
Block a user