added Bootstrap + Module + model class map helper + message translation config
This commit is contained in:
@ -8,24 +8,31 @@
|
|||||||
"email": "email@example.com"
|
"email": "email@example.com"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
"require": {
|
||||||
|
"php": ">=5.5"
|
||||||
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"squizlabs/php_codesniffer": "^2.7",
|
"squizlabs/php_codesniffer": "^2.7",
|
||||||
"fabpot/php-cs-fixer": "^1.12",
|
"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": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"Da\\" : ["./lib", "./tests/lib"]
|
"Da\\": [
|
||||||
|
"./lib",
|
||||||
|
"./tests/lib"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"autoload-dev": {
|
"autoload-dev": {
|
||||||
"psr-4": {
|
"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"
|
"minimum-stability": "dev"
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,49 +1,217 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Da\User;
|
namespace Da\User;
|
||||||
|
|
||||||
use Da\User\Helper\AuthHelper;
|
use Da\User\Helper\ClassMapHelper;
|
||||||
use Da\User\Query\AccountQuery;
|
use Yii;
|
||||||
use Da\User\Query\ProfileQuery;
|
use yii\authclient\Collection;
|
||||||
use Da\User\Query\TokenQuery;
|
|
||||||
use Da\User\Query\UserQuery;
|
|
||||||
use yii\base\Application;
|
use yii\base\Application;
|
||||||
use yii\base\BootstrapInterface;
|
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
|
class Bootstrap implements BootstrapInterface
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @inheritdoc
|
||||||
|
*/
|
||||||
public function bootstrap($app)
|
public function bootstrap($app)
|
||||||
{
|
{
|
||||||
if($app->hasModule('user') && $app->getModule('user') instanceof Module) {
|
if ($app->hasModule('user') && $app->getModule('user') instanceof Module) {
|
||||||
|
$classMap = $this->buildClassMap();
|
||||||
|
$this->initContainer($classMap);
|
||||||
|
$this->initTranslations($app);
|
||||||
|
|
||||||
// configure yii's container
|
if ($app instanceof WebApplication) {
|
||||||
$this->setContainer();
|
$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;
|
$di = Yii::$container;
|
||||||
// helpers
|
|
||||||
$di->set('Da\User\Helper\AuthHelper');
|
|
||||||
|
|
||||||
// email change strategy
|
// email change strategy
|
||||||
$di->set('Da\User\Strategy\DefaultEmailChangeStrategy');
|
$di->set('Da\User\Strategy\DefaultEmailChangeStrategy');
|
||||||
$di->set('Da\User\Strategy\InsecureEmailChangeStrategy');
|
$di->set('Da\User\Strategy\InsecureEmailChangeStrategy');
|
||||||
$di->set('Da\User\Strategy\SecureEmailChangeStrategy');
|
$di->set('Da\User\Strategy\SecureEmailChangeStrategy');
|
||||||
|
|
||||||
// active query classes
|
// models + active query classes
|
||||||
Yii::$container->set('Da\User\Query\AccountQuery');
|
$modelClassMap = [];
|
||||||
Yii::$container->set('Da\User\Query\ProfileQuery');
|
foreach ($map as $class => $definition) {
|
||||||
Yii::$container->set('Da\User\Query\TokenQuery');
|
|
||||||
Yii::$container->set('Da\User\Query\UserQuery');
|
$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
|
<?php
|
||||||
|
|
||||||
namespace Da\User;
|
namespace Da\User;
|
||||||
|
|
||||||
use Da\User\Strategy\DefaultEmailChangeStrategy;
|
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
|
class Module extends \yii\base\Module
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
@ -64,12 +57,12 @@ class Module extends \yii\base\Module
|
|||||||
* @var array the url rules (routes)
|
* @var array the url rules (routes)
|
||||||
*/
|
*/
|
||||||
public $routes = [
|
public $routes = [
|
||||||
'<id:\d+>' => 'profile/show',
|
'<id:\d+>' => 'profile/show',
|
||||||
'<action:(login|logout)>' => 'security/<action>',
|
'<action:(login|logout)>' => 'auth/<action>',
|
||||||
'<action:(register|resend)>' => 'registration/<action>',
|
'<action:(register|resend)>' => 'registration/<action>',
|
||||||
'confirm/<id:\d+>/<code:[A-Za-z0-9_-]+>' => 'registration/confirm',
|
'confirm/<id:\d+>/<code:[A-Za-z0-9_-]+>' => 'registration/confirm',
|
||||||
'forgot' => 'recovery/request',
|
'forgot' => 'recovery/request',
|
||||||
'recover/<id:\d+>/<code:[A-Za-z0-9_-]+>' => 'recovery/reset',
|
'recover/<id:\d+>/<code:[A-Za-z0-9_-]+>' => 'recovery/reset',
|
||||||
'settings/<action:\w+>' => 'settings/<action>'
|
'settings/<action:\w+>' => 'settings/<action>'
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
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