Skip to content

Commit

Permalink
init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
fgh151 committed Mar 23, 2016
0 parents commit d2a96bd
Show file tree
Hide file tree
Showing 10 changed files with 380 additions and 0 deletions.
50 changes: 50 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Created by .ignore support plugin (hsz.mobi)
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion

*.iml

## Directory-based project format:
.idea/
# if you remove the above rule, at least ignore the following:

# User-specific stuff:
# .idea/workspace.xml
# .idea/tasks.xml
# .idea/dictionaries

# Sensitive or high-churn files:
# .idea/dataSources.ids
# .idea/dataSources.xml
# .idea/sqlDataSources.xml
# .idea/dynamic.xml
# .idea/uiDesigner.xml

# Gradle:
# .idea/gradle.xml
# .idea/libraries

# Mongo Explorer plugin:
# .idea/mongoSettings.xml

## File-based project format:
*.ipr
*.iws

## Plugin-specific files:

# IntelliJ
/out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties


49 changes: 49 additions & 0 deletions Module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace fgh151\modules\params;

use Yii;

/**
* Class Module
* @package fgh151\modules\params
*/
class Module extends \yii\base\Module
{
public $defaultRoute = 'main';

public $controllerNamespace = 'common\modules\params\controllers';

public $paramsFilePath = [];

public function init()
{
parent::init();
$this->registerTranslations();
}

protected function registerTranslations()
{
Yii::$app->i18n->translations['params/*'] = [
'class' => 'yii\i18n\PhpMessageSource',
'sourceLanguage' => 'en-US',
'basePath' => '@eapanel/params/messages',
'fileMap' => [
'params/system' => 'system.php',
'params/app' => 'app.php'
],
];
}

/**
* @param $category
* @param $message
* @param array $params
* @param null $language
* @return string
*/
public static function t($category, $message, $params = [], $language = null)
{
return Yii::t('params/' . $category, $message, $params, $language);
}
}
32 changes: 32 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "fgh151/yii2-params",
"version": "0.0.1",
"description": "Yii2 params files managment",
"keywords": ["yii2", "params"],
"homepage": "http://openitstudio.ru",
"type": "yii2-extension",
"license": "GPL",
"authors": [
{
"name": "Fedor B Gorsky",
"email": "[email protected]"
},
{
"name": "Evgenii Dudal",
"email": "[email protected]"
}
],
"minimum-stability": "dev",
"require": {
"yiisoft/yii2": "^2.0.2",
"yiisoft/yii2-bootstrap": "~2.0"
},
"require-dev": {

},
"autoload": {
"psr-4": {
"fgh151\\modules\\params": ""
}
}
}
36 changes: 36 additions & 0 deletions controllers/MainController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace fgh151\modules\params\controllers;

use fgh151\modules\params\ParamsModule;
use yii\web\Controller;
use fgh151\modules\params\models\ParamsModel;

/**
* Class MainController
* @package fgh151\modules\params\controllers
*/
class MainController extends Controller
{
/**
* @return string
*/
public function actionIndex()
{
return $this->render('index',['model'=>$this->module->paramsFilePath]);
}

/**
* @param string $file thet contain return []
* @return string
*/
public function actionEdit($file)
{
$model = new ParamsModel(['paramsFilePathAlias' => $file]);
if($model->load(\Yii::$app->request->post()))
{
$model->save();
}
return $this->render('edit',['model'=>$model]);
}
}
5 changes: 5 additions & 0 deletions messages/ru/app.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
return [
'Save settings' => 'Сохранить настройки'
];

4 changes: 4 additions & 0 deletions messages/ru/system.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
return [
];

95 changes: 95 additions & 0 deletions models/ParamsModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace fgh151\modules\params\models;

use fgh151\modules\params\Module;
use yii\helpers\VarDumper;

class ParamsModel extends \yii\base\Model{

private $_attributes=[];
public $paramsFilePathAlias;

/**
* @throws \yii\base\InvalidConfigException
*/
public function init() {
if(!isset($this->paramsFilePath))
{
throw new \yii\base\InvalidConfigException(Module::t('system','$paramsFilePathAlias variable must be set when creating the object'));
}
$this->loadFromFile();
return parent::init();
}

/**
* @return array
*/
public function attributes() {
$attributes = [];
foreach ($this->_attributes as $name => $value) {
if(is_string($value))
{
$attributes[]=$name;
}
}
return $attributes;
}

/**
* @return bool|string
*/
public function getParamsFilePath()
{
return \Yii::getAlias($this->paramsFilePathAlias);
}


public function save()
{
$this->saveToFile();
}

protected function loadFromFile()
{
$this->_attributes = require $this->paramsFilePath;
}

protected function saveToFile()
{
file_put_contents($this->paramsFilePath, "<?php\nreturn " . VarDumper::export($this->_attributes). ";\n",LOCK_EX);
}

/**
* @param string $name
* @return mixed
* @throws \yii\base\UnknownPropertyException
*/
public function __get($name) {
if(key_exists($name, $this->_attributes))
{
$result = $this->_attributes[$name];
}
else{
$result=parent::__get($name);
}
return $result;
}

/**
* @param array $values
* @param bool|false $safeOnly
*/
public function setAttributes($values, $safeOnly = false) {
if (is_array($values)) {
$attributes = array_flip($safeOnly ? $this->safeAttributes() : $this->attributes());
foreach ($values as $name => $value) {
if (isset($attributes[$name])) {
$this->_attributes[$name] = $value;
} elseif ($safeOnly) {
$this->onUnsafeAttribute($name, $value);
}
}
}
}
}
81 changes: 81 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
Русская документация скоро будет доступна доступна на сайте [OpenItStudio](https://openitstudio.ru)

Yii2 params files management utility
====================================

Installation
------------

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
php composer.phar require --prefer-dist fgh151/yii2-params "*"
```

or add

```
"fgh151/yii2-params": "*"
```

to the require section of your `composer.json` file.


Usage
-----

Once the extension is installed, simply add it in your config by :

```php
'modules' => [
...
'params' => [
'class' => 'fgh151\modules\params\Module',
'paramsFilePath' => [
'Common params' => '@common/config/params.php',
'Backend Params' => '@backend/config/params.php',
'Frontend Params' => '@frontend/config/params.php',
]
]
],
```php

add to paramsFilePath array names of files and path to them

RBAC
----

You can use RBAC with module. Simply add it in your config:

```php
'modules' => [
'params' => [
'class' => 'fgh151\modules\params\Module',
'paramsFilePath' => [
'Common params' => '@common/config/params.php',
'Backend Params' => '@backend/config/params.php',
'Frontend Params' => '@frontend/config/params.php',
],
'as access' => [
'class' => 'yii\filters\AccessControl',
'rules' => [
[
'allow' => true,
'roles' => ['admin'],
]
]
]
]
...
],
```


Usage
-----

Pretty Url's ```/params```

No pretty Url's ```index.php?r=params```
20 changes: 20 additions & 0 deletions views/main/edit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

use yii\widgets\ActiveForm;
use yii\helpers\Html;
use common\modules\params\Module;

/* @var $model common\modules\params\models\ParamsModel */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="app_params_settings-main-index">
<h1><?= $model->paramsFilePath ?></h1>
<?php $form = ActiveForm::begin();
foreach ($model->attributes() as $attribute):
echo $form->field($model,$attribute);
endforeach;
echo Html::submitButton(Module::t('app','Save settings'),['class'=>'btn btn-success']);
?>
<?php ActiveForm::end()?>
</div>
8 changes: 8 additions & 0 deletions views/main/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

/* @var $model array */
?>

<?foreach($model as $name => $file):?>
<p><a href="<?=Yii::$app->urlManager->createUrl(['/params/main/edit', 'file' => $file])?>"><?=$name?></a> </p>
<?endforeach;?>

0 comments on commit d2a96bd

Please sign in to comment.