Browse Source

Merge remote-tracking branch 'upstream/master' into transalting

tags/2.0.0-rc
东方孤思子(Paris·QianSen) 10 years ago
parent
commit
b0942a22a7
  1. 2
      apps/advanced/README.md
  2. 6
      apps/advanced/common/mail/layouts/html.php
  3. 20
      apps/advanced/common/models/User.php
  4. 2
      apps/advanced/composer.json
  5. 2
      apps/advanced/frontend/tests/unit/_console.php
  6. 8
      apps/advanced/requirements.php
  7. 2
      apps/basic/composer.json
  8. 6
      apps/basic/mail/layouts/html.php
  9. 8
      apps/basic/requirements.php
  10. 2
      apps/basic/tests/README.md
  11. 356
      docs/guide-es/concept-behaviors.md
  12. 100
      docs/guide-es/concept-components.md
  13. 90
      docs/guide-ru/concept-service-locator.md
  14. 201
      docs/guide-ru/structure-widgets.md
  15. 506
      docs/guide-uk/intro-upgrade-from-v1.md
  16. 52
      docs/guide-uk/intro-yii.md
  17. 2
      docs/guide/concept-aliases.md
  18. 2
      docs/guide/extend-creating-extensions.md
  19. 39
      docs/guide/extend-customizing-core.md
  20. 18
      docs/guide/extend-using-composer.md
  21. 54
      docs/guide/helper-overview.md
  22. 2
      docs/guide/rest-response-formatting.md
  23. 165
      docs/guide/structure-extensions.md
  24. 1
      docs/guide/tutorial-mailing.md
  25. 2
      extensions/authclient/README.md
  26. 1
      extensions/bootstrap/CHANGELOG.md
  27. 3
      extensions/bootstrap/Dropdown.php
  28. 3
      extensions/bootstrap/Nav.php
  29. 3
      extensions/elasticsearch/ActiveRecord.php
  30. 1
      extensions/elasticsearch/CHANGELOG.md
  31. 34
      extensions/elasticsearch/Connection.php
  32. 9
      extensions/mongodb/ActiveRecord.php
  33. 1
      extensions/mongodb/CHANGELOG.md
  34. 9
      extensions/mongodb/file/ActiveRecord.php
  35. 3
      extensions/redis/ActiveRecord.php
  36. 1
      extensions/redis/CHANGELOG.md
  37. 2
      extensions/redis/Connection.php
  38. 9
      extensions/sphinx/ActiveRecord.php
  39. 3
      extensions/sphinx/CHANGELOG.md
  40. 14
      framework/CHANGELOG.md
  41. 50
      framework/UPGRADE.md
  42. 40
      framework/base/Security.php
  43. 4
      framework/captcha/CaptchaValidator.php
  44. 10
      framework/console/controllers/BaseMigrateController.php
  45. 3
      framework/db/ActiveRecord.php
  46. 12
      framework/db/BaseActiveRecord.php
  47. 46
      framework/db/cubrid/Schema.php
  48. 2
      framework/db/mysql/Schema.php
  49. 2
      framework/filters/auth/QueryParamAuth.php
  50. 11
      framework/helpers/BaseFileHelper.php
  51. 3
      framework/log/Dispatcher.php
  52. 9
      framework/log/Target.php
  53. 55
      framework/mail/BaseMailer.php
  54. 1
      framework/messages/pl/yii.php
  55. 6
      framework/messages/th/yii.php
  56. 2
      framework/rbac/Assignment.php
  57. 371
      framework/rbac/PhpManager.php
  58. 9
      framework/requirements/requirements.php
  59. 3
      framework/views/migration.php
  60. 4
      tests/unit/data/travis/setup-apps.sh
  61. 2
      tests/unit/framework/ar/ActiveRecordTestTrait.php
  62. 15
      tests/unit/framework/base/SecurityTest.php
  63. 37
      tests/unit/framework/rbac/ExposedPhpManager.php
  64. 173
      tests/unit/framework/rbac/ManagerTestCase.php
  65. 58
      tests/unit/framework/rbac/PhpManagerTest.php

2
apps/advanced/README.md

@ -100,7 +100,7 @@ TESTING
-------
Install additional composer packages:
* `php composer.phar require --dev "codeception/codeception: 1.8.*@dev" "codeception/specify: *" "codeception/verify: *" "yiisoft/yii2-faker: *"`
* `php composer.phar require --dev "codeception/codeception: 2.0.*" "codeception/specify: *" "codeception/verify: *" "yiisoft/yii2-faker: *"`
This application boilerplate use database in testing, so you should create three databases that are used in tests:
* `yii2_advanced_unit` - database for unit tests;

6
apps/advanced/common/mail/layouts/html.php

@ -1,9 +1,9 @@
<?php
use yii\helpers\Html;
/* @var $this \yii\web\View */
/* @var $content string */
/* @var $this \yii\web\View view component instance */
/* @var $message \yii\mail\MessageInterface the message being composed */
/* @var $content string main view render result */
?>
<?php $this->beginPage() ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

20
apps/advanced/common/models/User.php

@ -1,9 +1,10 @@
<?php
namespace common\models;
use Yii;
use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use Yii;
use yii\web\IdentityInterface;
/**
@ -25,7 +26,6 @@ class User extends ActiveRecord implements IdentityInterface
{
const STATUS_DELETED = 0;
const STATUS_ACTIVE = 10;
const ROLE_USER = 10;
/**
@ -42,13 +42,7 @@ class User extends ActiveRecord implements IdentityInterface
public function behaviors()
{
return [
'timestamp' => [
'class' => 'yii\behaviors\TimestampBehavior',
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
],
],
TimestampBehavior::className(),
];
}
@ -85,7 +79,7 @@ class User extends ActiveRecord implements IdentityInterface
/**
* Finds user by username
*
* @param string $username
* @param string $username
* @return static|null
*/
public static function findByUsername($username)
@ -96,12 +90,12 @@ class User extends ActiveRecord implements IdentityInterface
/**
* Finds user by password reset token
*
* @param string $token password reset token
* @param string $token password reset token
* @return static|null
*/
public static function findByPasswordResetToken($token)
{
$expire = \Yii::$app->params['user.passwordResetTokenExpire'];
$expire = Yii::$app->params['user.passwordResetTokenExpire'];
$parts = explode('_', $token);
$timestamp = (int) end($parts);
if ($timestamp + $expire < time()) {
@ -142,7 +136,7 @@ class User extends ActiveRecord implements IdentityInterface
/**
* Validates password
*
* @param string $password password to validate
* @param string $password password to validate
* @return boolean if password provided is valid for current user
*/
public function validatePassword($password)

2
apps/advanced/composer.json

@ -25,7 +25,7 @@
"yiisoft/yii2-gii": "*"
},
"suggest": {
"codeception/codeception": "Codeception, 1.8.*@dev is currently works well with Yii.",
"codeception/codeception": "Codeception, 2.0.* is currently works well with Yii.",
"codeception/specify": "BDD style code blocks for PHPUnit and Codeception",
"codeception/verify": "BDD Assertions for PHPUnit and Codeception",
"yiisoft/yii2-faker": "Fixtures generator for Yii2 based on Faker lib"

2
apps/advanced/frontend/tests/unit/_console.php

@ -14,7 +14,7 @@ return yii\helpers\ArrayHelper::merge(
'controllerMap' => [
'fixture' => [
'class' => 'yii\faker\FixtureController',
'fixtureDataPath' => '@console/tests/unit/fixtures/data',
'fixtureDataPath' => '@frontend/tests/unit/fixtures/data',
'templatePath' => '@common/tests/templates/fixtures'
],
],

8
apps/advanced/requirements.php

@ -69,14 +69,6 @@ $requirements = array(
'condition' => extension_loaded('apc'),
'by' => '<a href="http://www.yiiframework.com/doc-2.0/yii-caching-apccache.html">ApcCache</a>',
),
// Additional PHP extensions :
array(
'name' => 'Mcrypt extension',
'mandatory' => false,
'condition' => extension_loaded('mcrypt'),
'by' => '<a href="http://www.yiiframework.com/doc-2.0/yii-helpers-security.html">Security Helper</a>',
'memo' => 'Required by encrypt and decrypt methods.'
),
// PHP ini :
'phpSafeMode' => array(
'name' => 'PHP safe mode',

2
apps/basic/composer.json

@ -25,7 +25,7 @@
"yiisoft/yii2-gii": "*"
},
"suggest": {
"codeception/codeception": "Codeception, 1.8.*@dev is currently works well with Yii.",
"codeception/codeception": "Codeception, 2.0.* is currently works well with Yii.",
"codeception/specify": "BDD style code blocks for PHPUnit and Codeception",
"codeception/verify": "BDD Assertions for PHPUnit and Codeception"
},

6
apps/basic/mail/layouts/html.php

@ -1,9 +1,9 @@
<?php
use yii\helpers\Html;
/* @var $this \yii\web\View */
/* @var $content string */
/* @var $this \yii\web\View view component instance */
/* @var $message \yii\mail\MessageInterface the message being composed */
/* @var $content string main view render result */
?>
<?php $this->beginPage() ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

8
apps/basic/requirements.php

@ -69,14 +69,6 @@ $requirements = array(
'condition' => extension_loaded('apc'),
'by' => '<a href="http://www.yiiframework.com/doc-2.0/yii-caching-apccache.html">ApcCache</a>',
),
// Additional PHP extensions :
array(
'name' => 'Mcrypt extension',
'mandatory' => false,
'condition' => extension_loaded('mcrypt'),
'by' => '<a href="http://www.yiiframework.com/doc-2.0/yii-helpers-security.html">Security Helper</a>',
'memo' => 'Required by encrypt and decrypt methods.'
),
// PHP ini :
'phpSafeMode' => array(
'name' => 'PHP safe mode',

2
apps/basic/tests/README.md

@ -6,7 +6,7 @@ After creating the basic application, follow these steps to prepare for the test
1. Install additional composer packages:
```
php composer.phar require --dev "codeception/codeception: 1.8.*@dev" "codeception/specify: *" "codeception/verify: *"
php composer.phar require --dev "codeception/codeception: 2.0.*" "codeception/specify: *" "codeception/verify: *"
```
2. In the file `_bootstrap.php`, modify the definition of the constant `TEST_ENTRY_URL` so
that it points to the correct entry script URL.

356
docs/guide-es/concept-behaviors.md

@ -0,0 +1,356 @@
Comportamientos
===============
Comportamientos son instancias de [[yii\base\Behavior]] o sus clases "hija". Comportamientos, también conocido como
[mixins](http://en.wikipedia.org/wiki/Mixin), te permiten mejorar la funcionalidad de un [[yii\base\Component|componente]]
existente sin necesidad de modificar su herencia de clases.
Cuando un comportamiento se une a un componente, "inyectará" sus métodos y propiedades dentro del componente, y podrás
acceder a esos métodos y propiedades como si hubieran estado definidos por la clase de componente. Además, un
comportamiento puede responder a [eventos](concept-events.md) disparados por el componente de modo que se pueda personalizar
o adaptar a la ejecución normal del código del componente.
Usando comportamientos <a name="using-behaviors"></a>
----------------------
Para poder utilizar un comportamiento, primero tienes que unirlo a un [[yii\base\Component|componente]]. Describiremos cómo
puedes vincular un comportamiento en la próxima sub-sección.
Una vez que el comportamiento ha sido vinculado a un componente, su uso es sencillo.
Puedes usar a una variable *pública* o a una [propiedad](concept-properties.md) definida por un `getter` y/o un `setter`
del comportamiento a través del componente con el que se ha vinculado, como por ejemplo,
```php
// "prop1" es una propiedad definida en la clase comportamiento
echo $component->prop1;
$component->prop1 = $value;
```
También puedes llamar métodos *públicos* del comportamiento de una forma similar,
```php
// bar() es un método público definido dentro de la clase comportamiento
$component->bar();
```
Como puedes ver, aunque `$component` no tiene definida `prop1` y `bar()`, pueden ser usadas como si fueran parte
definida del componente.
Si dos comportamientos definen la misma propiedad o método y ambos están vinculados con el mismo componente, el
comportamiento que ha sido vinculado primero tendrá preferencia cuando se esté accediendo a la propiedad o método.
Un comportamiento puede estar asociado con un nombre cuando se une a un componente. Si este es el caso, es posible
acceder al objeto de comportamiento mediante el nombre, como se muestra a continuación,
```php
$behavior = $component->getBehavior('myBehavior');
```
También puedes acceder a todos los comportamientos vinculados al componente:
```php
$behaviors = $component->getBehaviors();
```
Vinculando Comportamientos <a name="attaching-behaviors"></a>
--------------------------
Puedes vincular un comportamiento a un [[yii\base\Component|componente]] ya sea estática o dinámicamente. La primera forma
es la más comúnmente utilizada en la práctica.
Para unir un comportamiento estáticamente, reemplaza el método [[yii\base\Component::behaviors()|behaviors()]] de la
clase componente que se está conectando. Por ejemplo,
```php
namespace app\models;
use yii\db\ActiveRecord;
use app\components\MyBehavior;
class User extends ActiveRecord
{
public function behaviors()
{
return [
// comportamiento anónimo, sólo el nombre de la clase del comportamiento
MyBehavior::className(),
// comportamiento nombrado, sólo el nombre de la clase del comportamiento
'myBehavior2' => MyBehavior::className(),
// comportamiento anónimo, matriz de configuración
[
'class' => MyBehavior::className(),
'prop1' => 'value1',
'prop2' => 'value2',
],
// comportamiento nombrado, matriz de configuración
'myBehavior4' => [
'class' => MyBehavior::className(),
'prop1' => 'value1',
'prop2' => 'value2',
]
];
}
}
```
El método [[yii\base\Component::behaviors()|behaviors()]] tiene que devolver la lista de los comportamientos
[configuraciones](concept-configurations.md).
Cada configuración de un comportamiento puede ser el nombre de la clase o una matriz de configuración.
Puedes asociciar un nombre a un comportamiento especificándolo en la clave de la matriz correspondiente a la configuración
del comportamiento. En este caso, el comportamiento puede ser llamado un *comportamiento nombrado* (named behavior). En
el ejemplo anterior, hay dos tipos de comportamientos nombrados: `myBehavior2` y `myBehavior4`. Si un comportamiento
no está asociado con un nombre, se le llama *comportamiento anónimo* (anonymous behavior).
Para vincular un comportamiento dinámicamente, llama al método [[yii\base\Component::attachBehavior()]] desde el componente al
que se le va a unir el comportamiento. Por ejemplo,
```php
use app\components\MyBehavior;
// vincular un objeto comportamiento "behavior"
$component->attachBehavior('myBehavior1', new MyBehavior);
// vincular una clase comportamiento
$component->attachBehavior('myBehavior2', MyBehavior::className());
// asociar una matriz de configuración
$component->attachBehavior('myBehavior3', [
'class' => MyBehavior::className(),
'prop1' => 'value1',
'prop2' => 'value2',
]);
```
You may attach multiple behaviors at once by using the [[yii\base\Component::attachBehaviors()]] method.
For example,
```php
$component->attachBehaviors([
'myBehavior1' => new MyBehavior, // a named behavior
MyBehavior::className(), // an anonymous behavior
]);
```
También puedes asociar comportamientos a traves de [configuraciones](concept-configurations.md) compor el siguiente
ejemplo. Para más detalles, por favor visita la sección [Configuraciones](concept-configurations.md#configuration-format).
```php
[
'as myBehavior2' => MyBehavior::className(),
'as myBehavior3' => [
'class' => MyBehavior::className(),
'prop1' => 'value1',
'prop2' => 'value2',
],
]
```
Desasociar Comportamientos <a name="detaching-behaviors"></a>
--------------------------
Para desasociar un comportamiento, puedes llamar el método [[yii\base\Component::detachBehavior()]] con el nombre con el
que se le asoció:
```php
$component->detachBehavior('myBehavior1');
```
También puedes desvincular *todos* los comportamientos:
```php
$component->detachBehaviors();
```
Definiendo Comportamientos <a name="defining-behaviors"></a>
--------------------------
Para definir un comportamiento, crea una clase extendendiéndola de [[yii\base\Behavior]] o una de sus clases "hija". Por ejemplo,
```php
namespace app\components;
use yii\base\Model;
use yii\base\Behavior;
class MyBehavior extends Behavior
{
public $prop1;
private $_prop2;
public function getProp2()
{
return $this->_prop2;
}
public function setProp2($value)
{
$this->_prop2 = $value;
}
public function foo()
{
// ...
}
}
```
El código anterior define la clase del comportamiento `app\components\MyBehavior` que provee dos propiedades `prop1` y
`prop2`, y un método `foo()` al componente con el que está asociado.
The above code defines the behavior class `app\components\MyBehavior` which will provide two properties
`prop1` and `prop2`, and one method `foo()` to the component it is attached to. Fíjese que la propiedad `prop2` esta
definida a través del getter `getProp2()` y el setter `setProp2()`. Esto es debido a que [[yii\base\Object]] es una
clase "ancestro" (o padre) de [[yii\base\Behavior]], la cual soporta la definición de [propiedades](concept-properties.md) por
getters/setters.
En un comportamiento, puedes acceder al componente al que está vinculado a través de la propiedad [[yii\base\Behavior::owner]].
Si un omportamiento necesita responder a los eventos que han sido disparados desde el componente al qu están asociados,
debería sobreescribir el método [[yii\base\Behavior::events()]]. Por ejemplo,
```php
namespace app\components;
use yii\db\ActiveRecord;
use yii\base\Behavior;
class MyBehavior extends Behavior
{
// ...
public function events()
{
return [
ActiveRecord::EVENT_BEFORE_VALIDATE => 'beforeValidate',
];
}
public function beforeValidate($event)
{
// ...
}
}
```
El método [[yii\base\Behavior::events()|events()]] tiene que devolver un listado de eventos y sus correspondientes
controladores (handlers). El código anterior declara el evento [[yii\db\ActiveRecord::EVENT_BEFORE_VALIDATE|EVENT_BEFORE_VALIDATE]]
con su controlador `beforeValidate()`. Cuando se especifica un controlador de evento, pudes utilizar uno de los siguientes
formatos:
* una cadena que se refiere al nombre de un método de la clase comportamiento, como el ejemplo anterior;
* una matriz con un objeto o nombre de la clase, y el nombre de un método, por ejemplo, `[$object, 'nombreMétodo']`;
* una función anónima.
El formato de un controlador de eventos tendría que ser como se describe a continuación, donde `$event` se refiere al
parámetro `evento`. Por favor, visita la sección [Eventos](concept-events.md) para obtener más información acerca de
eventos.
```php
function ($event) {
}
```
Utilizando `TimestampBehavior` <a name="using-timestamp-behavior"></a>
-----------------------------
Para terminar, vamos a echar un vistazo a [[yii\behaviors\TimestampBehavior]] - un comportamiento que soporta de forma
automática la actualización de atributos `timestamp` (sellos de tiempo) de un [[yii\db\ActiveRecord|Registro Activo]]
(Active Record) cuando éste está siendo guardado.
Primero, vincula este comportamiento a la clase [[yii\db\ActiveRecord|Active Record]] que desees utilizar.
```php
namespace app\models\User;
use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
class User extends ActiveRecord
{
// ...
public function behaviors()
{
return [
[
'class' => TimestampBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
],
],
];
}
}
```
La configuración del comportamiento anterior especifica que
* cuando el registro está siendo insertado, el comportamiento debe asignar el sello de tiempo actual a los atributos
`created_at` y `updated_at`;
* cuando el registro está siendo actualizado, el comportamiento debe asignar el sello de tiempo actual al atributo
`updated_at.
Ahora si tienes un objeto `User` e intentas guardarlo, descubrirás que sus campos `created_at` y `updated_at` están
automáticamente actualizados con el sello de tiempo actual:
```php
$user = new User;
$user->email = 'test@example.com';
$user->save();
echo $user->created_at; // muestra el sello tiempo actual (timestamp)
```
El comportamiento [[yii\behaviors\TimestampBehavior|TimestampBehavior]] también ofrece un método muy útil llamado
[[yii\behaviors\TimestampBehavior::touch()|touch()]], que asigna el sello de tiempo actual a un atributo especificado y
lo guarda automáticamente en la base de datos:
```php
$user->touch('login_time');
```
Comparación con Traits <a name="comparison-with-traits"></a>
----------------------
Mientras que los comportamientos son similares a [traits](http://www.php.net/traits) en cuanto que ambos "inyectan" sus
métodos y propiedades a la clase primaria, son diferentes en muchos aspectos. Tal y como se describe abajo, los dos
tienen sus ventajas y desventajas. Son much mejor descritos como complementos y no como reemplazos entre sí.
### Las Ventajas de los Comportamientos <a name="pros-for-behaviors"></a>
Las clases de comportamientos (Behaviors), como todas las clases, soportan herencias. Traits, por otro lado, pueden ser
considerados como un copia-y-pega de PHP. Los Traits no soportan la herencia de clases.
Los comportamientos pueden ser asociados y desasociados a un componente dinámicamente sin necesidad de que la clase del
componente sea modificada. Para usar un trait, debes modificar la clase que la usa.
Los comportamientos son configurables mientras que los traits no.
Los comportamientos pueden personalizar la ejecución de un componente al responder a sus eventos.
Cuando hay un conflicto de nombre entre los diferentes comportamientos vinculados a un mismo componente, el conflicto es
automáticamente resuelto respetando al que ha sido asociado primero.
El conflicto de nombres en traits requiere que manualmente sean resueltos cambiando el nombre de las propiedades o métodos
afectados.
### Las Ventajas de los Traits <a name="pros-for-traits"></a>
Los Traits son mucho más eficientes que los comportamientos debido a que los últimos son objetos que consumen tiempo y
memoria.
Los IDEs (Programas de desarrollo) trabajan mucho mejor con traits ya que forman parte del lenguaje PHP.

100
docs/guide-es/concept-components.md

@ -0,0 +1,100 @@
Componentes
===========
Los componentes son los principales bloques de construcción de las aplicaciones Yii. Los componentes son instancias de
[[yii\base\Component]] o de una clase extendida. Las tres características principales que los componentes proporcionan
a las otras clases son:
* [Propiedades](concept-properties.md)
* [Eventos](concept-events.md)
* [Comportamientos](concept-behaviors.md)
Por separado y combinadas, estas características hacen que las clases Yii sean mucho mas personalizables y sean mucho
más fáciles de usar. Por ejemplo, el incluido [[yii\jui\DatePicker|widget de selección de fecha]], un componente de la
interfaz de usuario, puede ser utilizado en una [vista](structure-view.md) para generar un selector de fechas interactivo:
```php
use yii\jui\DatePicker;
echo DatePicker::widget([
'language' => 'ru',
'name' => 'country',
'clientOptions' => [
'dateFormat' => 'yy-mm-dd',
],
]);
```
Las propiedades del widget son facilmente modificables porque la clase se extiende de [[yii\base\Component]].
Mientras que los componentes son muy potentes, son un poco más pesados que los objetos normales, debido al hecho de que
necesitan más memoria y tiempo de CPU para poder soportar [eventos](concept-events.md) y [comportamientos](concept-behaviors.md) en particular.
Si tus componentes no necesitan estas dos características, deberías considerar extender tu componente directamente de
[[yii\base\Object]] en vez de [[yii\base\Component]]. De esta manera harás que tus componentes sean mucho más eficientes que
que objetos PHP normales, pero con el añadido soporte para [propiedades](concept-properties.md).
Cuando extiendes tu clase de [[yii\base\Component]] o [[yii\base\Object]], se recomienda que sigas las siguientes
convenciones:
- Si sobrescribes el constructor, especifica un parámetro `$config` como el *último* parámetro del constructor, y después
pasa este parámetro al constructor de la clase "padre".
- Siempre llama al constructor del "padre" al *final* de su propio constructor.
- Si sobrescribes el método [[yii\base\Object::init()]], asegúrate de que llamas a la implementación de la clase "padre"
*al principio* de tu método `init`.
Por ejemplo:
```php
namespace yii\components\MyClass;
use yii\base\Object;
class MyClass extends Object
{
public $prop1;
public $prop2;
public function __construct($param1, $param2, $config = [])
{
// ... inicialización antes de la configuración está siendo aplicada
parent::__construct($config);
}
public function init()
{
parent::init();
// ... inicialización despues de la configuración esta siendo aplicada
}
}
```
Siguiendo esas directrices hará que tus componentes sean [configurables](concept-configurations.md) cuando son creados. Por ejemplo:
```php
$component = new MyClass(1, 2, ['prop1' => 3, 'prop2' => 4]);
// alternativamente
$component = \Yii::createObject([
'class' => MyClass::className(),
'prop1' => 3,
'prop2' => 4,
], [1, 2]);
```
> Información: Mientras que el enfoque de llamar [[Yii::createObject()]] parece mucho más complicado, es mucho más potente
debido al hecho de que se implementa en la parte superior de un [contenedor de inyección de dependencia](concept-di-container.md).
La clase [[yii\base\Object]] hace cumplir el siguiente ciclo de vida del objeto:
1. Pre-inicialización en el constructor. Puedes establecer los valores predeterminados de propiedades aquí.
2. Configuración del objeto a través de `$config`. La configuración puede sobrescribir los valores prdeterminados dentro
del constructor.
3. Post-inicialización dentro de [[yii\base\Object::init()|init()]]. Puedes sobrescribir este método para realizar
comprobaciones de validez y normalización de las propiedades.
4. LLamadas a métodos del objeto.
Los tres primeros pasos ocurren dentro del constructor del objeto. Esto significa que una vez obtengas la instancia de
un objeto, ésta ha sido inicializada para que puedas utilizarla adecuadamente.

90
docs/guide-ru/concept-service-locator.md

@ -0,0 +1,90 @@
Service Locator
===============
Service Locator является объектом, который знает, как обеспечить всевозможные службы (или компоненты), которые могут понадобиться в приложении.
В пределах Service Locator'а, каждый компонент имеет только один экземпляр, который уникально определяется с помощью идентификатора (ID).
Уникальный идентификатор (ID) может быть использован для извлечения компонента из Service Locator'а.
В Yii Service Locator является экземпляром класса [[yii\di\ServiceLocator]] или его дочернего класса.
Наиболее часто используемый Service Locator в Yii - это объект *приложения*, который можно получить через
`\Yii::$app`. Обеспечиваемые им службы называют *компонентами приложения*, такие, как компоненты `запрос`, `ответ`, `UrlManager`.
Вы легко можете настроить эти компоненты или даже заменить их собственными реализациями,
благодаря функциональным службам, предоставляемым Service Locator'ом.
Помимо объекта приложения, объект каждого модуля так же является Service Locator'ом.
Для использования Service Locator'а первым шагом является регистрация компонентов.
Компонент может быть зарегистрирован с помощью [[yii\di\ServiceLocator::set()]].
Следующий код демонстрирует различные способы регистрации компонентов:
```php
use yii\di\ServiceLocator;
use yii\caching\FileCache;
$locator = new ServiceLocator;
// Зарегистрирует "cache", используя имя класса, которое может быть использовано для создания компонента.
$locator->set('cache', 'yii\caching\ApcCache');
// Зарегистрирует "db", используя конфигурационный массив, который может быть использован для создания компонента.
$locator->set('db', [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=demo',
'username' => 'root',
'password' => '',
]);
// Зарегистрирует "search", используя анонимную функцию, которая создаёт компонент
$locator->set('search', function () {
return new app\components\SolrService;
});
// Зарегистрирует "pageCache", используя компонент
$locator->set('pageCache', new FileCache);
```
После того, как компонент зарегистрирован, вы можете получить к нему доступ, используя уникальный идентификатор (ID),
одним из двух следующих способов:
```php
$cache = $locator->get('cache');
// или альтернативный
$cache = $locator->cache;
```
Как видно выше, [[yii\di\ServiceLocator]] позволяет обратиться к компоненту, как к свойству,
при помощи идентификатора (ID) компонента.
При обращении к компоненту впервые, [[yii\di\ServiceLocator]] будет использовать информацию о регистрации компонента,
что бы создать новый экземпляр компонента и вернёт его.
В дальнейшем при обращении к компоненту снова, Service Locator вернёт тот же экземпляр.
Что бы проверить, был ли идентификатор (ID) компонента уже зарегистрирован, можно использовать [[yii\di\ServiceLocator::has()]].
Если вы вызовете [[yii\di\ServiceLocator::get()]] с недопустимым идентификатором (ID), тогда будет выброшено исключение.
Поскольку Service Locator`ы зачастую создаются с [конфигурациями](concept-configurations.md),
записываемое свойство с именем [[yii\di\ServiceLocator::setComponents()|components]] предоставляется так,
что Вы можете его настроить и зарегистрировать несколько компонентов одновременно.
Следующий код демонстрирует конфигурационный массив,
который может использоваться для настройки приложения и регистрации компонентов "db", "cache" и "search" :
```php
return [
// ...
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=demo',
'username' => 'root',
'password' => '',
],
'cache' => 'yii\caching\ApcCache',
'search' => function () {
return new app\components\SolrService;
},
],
];
```

201
docs/guide-ru/structure-widgets.md

@ -0,0 +1,201 @@
Виджеты
=======
Виджеты представляют собой многоразовые строительные блоки, используемые в [представлениях](structure-views.md)
для создания сложных и настраиваемых элементов пользовательского интерфейса в рамках объектно-ориентированного
подхода. Например, виджет выбора даты (date picker) позволяет генерировать интерактивный интерфейс для выбора дат,
предоставляя пользователям приложения удобный способ для ввода данных такого типа. Все, что нужно для
подключения виджета - это добавить следующий код в представление:
```php
<?php
use yii\bootstrap\DatePicker;
?>
<?= DatePicker::widget(['name' => 'date']) ?>
```
В комплект Yii входит большое количество виджетов, например: [[yii\widgets\ActiveForm|active form]],
[[yii\widgets\Menu|menu]], [виджеты jQuery UI](widget-jui.md), [виджеты Twitter Bootstrap](widget-bootstrap.md).
Далее будут представлены базовые сведения о виджетах. Для получения сведений относительно использования
конкретного виджета, следует обратиться к документации соответствующего класса.
## Использование Виджетов <a name="using-widgets"></a>
Главным образом, виджеты применяют в [представлениях](structure-views.md). Для того, чтобы использовать виджет
в представлении, достаточно вызвать метод [[yii\base\Widget::widget()]]. Метод принимает массив [настроек](concept-configurations.md)
для инициализации виджета и возвращает результат его рендеринга. Например, следующий
код добавляет виджет для выбора даты, сконфигурированный для использования русского в качестве языка интерфейса
виджета и хранения вводимых данных в атрибуте `from_date` модели `$model`.
```php
<?php
use yii\bootstrap\DatePicker;
?>
<?= DatePicker::widget([
'model' => $model,
'attribute' => 'from_date',
'language' => 'ru',
'clientOptions' => [
'dateFormat' => 'yy-mm-dd',
],
]) ?>
```
Некоторые виджеты могут иметь внутреннее содержимое, которое следует располагать между вызовами методов
[[yii\base\Widget::begin()]] и [[yii\base\Widget::end()]]. Например, для генерации формы входа, в следующем
фрагменте кода используется виджет [[yii\widgets\ActiveForm]]. Этот виджет сгенерирует открывающий и закрывающий
тэги `<form>` в местах вызова методов `begin()` и `end()` соответственно. При этом, содержимое, расположенное
между вызовами указанных методов будет выведено без каких-либо изменений.
```php
<?php
use yii\widgets\ActiveForm;
use yii\helpers\Html;
?>
<?php $form = ActiveForm::begin(['id' => 'login-form']); ?>
<?= $form->field($model, 'username') ?>
<?= $form->field($model, 'password')->passwordInput() ?>
<div class="form-group">
<?= Html::submitButton('Login') ?>
</div>
<?php ActiveForm::end(); ?>
```
Обратите внимание на то, что в отличие от метода [[yii\base\Widget::widget()]], который возвращает результат
рендеринга, метод [[yii\base\Widget::begin()]] возвращает экземпляр виджета, который может быть
использован в дальнейшем для формирования его внутреннего содержимого.
## Создание Виджетов <a name="creating-widgets"></a>
Для того, чтобы создать виджет, следует унаследовать класс [[yii\base\Widget]] и переопределить методы
[[yii\base\Widget::init()]] и/или [[yii\base\Widget::run()]]. Как правило, метод `init()` должен содержать
код, выполняющий нормализацию свойств виджета, а метод `run()` - код, возвращающий результат рендеринга виджета.
Результат рендеринга может быть выведен непосредственно с помощью конструкции "echo" или же возвращен
в строке методом `run()`.
В следующем примере, виджет `HelloWidget` HTML-кодирует и отображает содержимое, присвоенное свойству `message`.
В случае, если указанное свойство не установлено, виджет, в качестве значения по умолчанию отобразит строку "Hello World".
```php
namespace app\components;
use yii\base\Widget;
use yii\helpers\Html;
class HelloWidget extends Widget
{
public $message;
public function init()
{
parent::init();
if ($this->message === null) {
$this->message = 'Hello World';
}
}
public function run()
{
return Html::encode($this->message);
}
}
```
Для того, что бы использовать этот виджет, достаточно добавить в представление следующий код:
```php
<?php
use app\components\HelloWidget;
?>
<?= HelloWidget::widget(['message' => 'Good morning']) ?>
```
Ниже представлен вариант виджета `HelloWidget`, который принимает содержимое, обрамленное вызовами методов
`begin()` и `end()`, HTML-кодирует его и выводит.
```php
namespace app\components;
use yii\base\Widget;
use yii\helpers\Html;
class HelloWidget extends Widget
{
public function init()
{
parent::init();
ob_start();
}
public function run()
{
$content = ob_get_clean();
return Html::encode($content);
}
}
```
Как Вы можете видеть, в методе `init()` происходит включение буферизации вывода PHP таким образом, что весь вывод
между вызовами `init()` и `run()` может быть перехвачен, обработан и возвращен в `run()`.
> Информация: При вызове метода [[yii\base\Widget::begin()]] будет создан новый экземпляр виджета, при этом
вызов метода `init()` произойдет сразу после выполнения остального кода в конструкторе виджета.
При вызове метода [[yii\base\Widget::end()]], будет вызван метод `run()`, а возвращенное им значение будет выведено
методом `end()`.
Следующий фрагмент кода содержит пример использования модифицированного варианта `HelloWidget`:
```php
<?php
use app\components\HelloWidget;
?>
<?php HelloWidget::begin(); ?>
content that may contain <tag>'s
<?php HelloWidget::end(); ?>
```
В некоторых случаях, виджету может потребоваться вывести крупный блок содержимого. И хотя это содержимое может
быть встроено непосредственно в метод `run()`, целесообразней поместить его в [представление](structure-views.md)
и вызвать метод [[yii\base\Widget::render()]] для его рендеринга. Например,
```php
public function run()
{
return $this->render('hello');
}
```
По умолчанию, файлы представлений виджетов должны находиться в директории `WidgetPath/views`, где `WidgetPath` -
директория, содержащая файл класса виджета. Таким образом, в приведенном выше примере, для виджета будет
использован файл представления `@app/components/views/hello.php`, при этом файл с классом виджета расположен в
`@app/components`. Для того, что бы изменить директорию, в которой содержатся файлы-представления для виджета,
следует переопределить метод [[yii\base\Widget::getViewPath()]].
## Лучшие Практики <a name="best-practices"></a>
Виджеты представляют собой объектно-ориентированный подход к повторному использованию кода пользовательского
интерфейса.
При создании виджетов, следует придерживаться основных принципов концепции MVC. В общем случае, основную логику
следует располагать в классе виджета, разделяя при этом код, отвечающий за разметку в [представления](structure-views.md).
Разрабатываемые виджеты должны быть самодостаточными. Это означает, что для их использования должно быть
достаточно всего лишь добавить виджет в представление. Добиться этого бывает затруднительно в том случае,
когда для его функционирования требуются внешние ресурсы, такие как CSS, JavaScript, изображения и т.д.
К счастью, Yii предоставляет поддержку механизма для работы с ресурсами [asset bundles](structure-asset-bundles.md),
который может быть успешно использован для решения данной проблемы.
В случае, когда виджет не содержит логики, а содержит только код, отвечающий за вывод разметки, он мало
отличается от [представления](structure-views.md). В действительности, единственное его отличие состоит в том, что
виджет представляет собой отдельный и удобный для распространения класс, в то время как представление - это
обычный PHP скрипт, подходящий для использования только лишь в конретном приложении.

506
docs/guide-uk/intro-upgrade-from-v1.md

@ -0,0 +1,506 @@
Оновлення із версії 1.1
=======================
Між версіями 1.1 і 2.0 існує багато відмінностей, так як Yii був повністю переписаний для версії 2.0.
Таким чином, оновлення з версії 1.1 не є таким же тривіальним, як оновлення між мінорними версіями.
У цьому посібнику з оновлення наведено основні відмінності між двома версіями.
Якщо раніше ви не використовували Yii 1.1, ви можете пропустити цей розділ і перейти до розділу
"[Встановлення Yii](start-installation.md)".
Також врахуйте, що Yii 2.0 включає більше нового функціоналу, ніж той, що буде описано тут. Наполегливо рекомендується,
що ви прочитаєте весь посібник, щоб дізнатися який функціонал було додано. Можливо, що необхідний функціонал,
який ви до цього розробляли самі, тепер є частиною фреймворка.
Встановлення
------------
Yii 2.0 повністю заснований на [Composer](https://getcomposer.org/), який де факто є менеджером залежностей для PHP.
Установка фреймворка, також як і розширень, здійснюється через Composer. Більш детальні відомості по встановленню Yii 2.0
приведені в розділі [Встановлення Yii](start-installation.md). Відомості про те, як створювати розширення для Yii 2.0
або адаптувати вже наявні розширення для версії 1.1 під версію 2.0, наведені в розділі
[Створення розширень](extend-creating-extensions.md).
Вимоги PHP
----------
Yii 2.0 використовує PHP 5.4 або вище, який включає велику кількість поліпшень в порівнянні з версією 5.2,
яка використовувалася Yii 1.1. Таким чином, існує багато відмінностей у мові, які ви повинні приймати до уваги.
Нижче наведені основні зміни в PHP:
- [Простори імен](http://php.net/manual/en/language.namespaces.php);
- [Анонімні функції](http://php.net/manual/en/functions.anonymous.php);
- Використання короткого синтаксису для масивів: `[...елементи...]` замість `array(...елементи...)`;
- Використання тегів `<?=` для вивода у файлах представлень.
З версії PHP 5.4 дану можливість можна використовувати без побоювань;
- [Класи та інтерфейси SPL](http://php.net/manual/en/book.spl.php);
- [Пізнє статичне звʼязування (LSB)](http://php.net/manual/en/language.oop5.late-static-bindings.php);
- [Класи для дати та часу](http://php.net/manual/en/book.datetime.php);
- [Трейти](http://php.net/manual/en/language.oop5.traits.php);
- [Інтернаціонализація (Intl)](http://php.net/manual/en/book.intl.php); Yii 2.0 використовує розширення PHP `intl`
для різного функціоналу інтернаціоналізації.
Простори імен
-------------
Одним з основних змін в Yii 2.0 є використання просторів імен. Майже кожен клас фреймворку знаходиться у просторі імен,
наприклад, `yii\web\Request`. Префікс "С" більше не використовується в іменах класів. Угода іменування відповідає
структурі каталога, в якій розташовується клас. Наприклад, `yii\web\Request` означає, що відповідний клас знаходиться
у файлі `web/Request.php` в каталогу Yii фреймворка. (Завдяки завантажувачу класів Yii, ви можете використовувати
будь-який клас фреймворку без необхідності безпосередньо підключати його).
Компонент та Обʼєкт
-------------------
В Yii 2.0 клас `CComponent` із версії 1.1 був розділений на два класи: [[yii\base\Object]] і [[yii\base\Component]].
Клас [[yii\base\Object|Object]] є простим базовим класом, який дозволяє використовувати
[геттери та сеттери](concept-properties.md) для властивостей. Клас [[yii\base\Component|Component]] наслідується
від класа [[yii\base\Object|Object]] та підтримує [події](concept-events.md) та [поведінки](concept-behaviors.md).
Якщо вашому класу не потрібно використовувати функціонал подій та поведінок, ви можете використати
[[yii\base\Object|Object]] у якості базового класу. В основному, це випадки, коли класи представляють собою базові
структури даних.
Конфігурація обʼєкта
--------------------
Клас [[yii\base\Object|Object]] надає єдиний спосіб конфігурування обʼєктів. Будь-який дочірній клас
[[yii\base\Object|Object]] може визначити конструктор (якщо потрібно) для своєї конфігурації наступним чином:
```php
class MyClass extends \yii\base\Object
{
public function __construct($param1, $param2, $config = [])
{
// ... ініціализація до того, як буде застосована конфігурація
parent::__construct($config);
}
public function init()
{
parent::init();
// ... ініціализація після того, як була застосована конфігурація
}
}
```
У прикладі вище, останній параметр конструктора повинен бути масивом конфігурації, який містить пари у форматі
ключ-значення для ініціалізації властивостей обʼєкта. Ви можете перевизначити метод [[yii\base\Object::init()|init()]]
для ініціалізації обʼєкту після того, як до нього була застосована конфігурація.
Слідуючи цій угоді, ви зможете створювати і конфігурувати нові обʼєкти за допомогою масиву конфігурації:
```php
$object = Yii::createObject([
'class' => 'MyClass',
'property1' => 'abc',
'property2' => 'cde',
], [$param1, $param2]);
```
Більш детальна інформація про конфігурацію представлена у розділі [Конфігурації обʼєктів](concept-configurations.md).
Події
-----
В Yii1, події створювалися за допомогою оголошення методу `on` (наприклад, `onBeforeSave`).
В Yii2 ви можете тепер використовувати будь-яке імʼя події. Ви ініціюєте подію за допомогою виклику методу
[[yii\base\Component::trigger()|trigger()]].
```php
$event = new \yii\base\Event;
$component->trigger($eventName, $event);
```
Для прикріплення обробника події використовуйте метод [[yii\base\Component::on()|on()]].
```php
$component->on($eventName, $handler);
// To detach the handler, use:
// $component->off($eventName, $handler);
```
Є також і інші покращення у функціоналі подій. Більш детальна інформація про конфігурація представлена у розділі
[Події](concept-events.md).
Псевдоніми шляху
----------------
Yii 2.0 розширює спосіб використання псевдонімів шляху як для файлів і каталогів, так і для URL.
У Yii 2.0 тепер також потрібно, щоб імʼя псевдоніма починалося із символу `@`, для розмежування псевдонімів від
звичайних шляхів файлів/каталогів і URL. Наприклад, псевдонім `@yii` відповідає каталогу встановлення Yii.
Псевдоніми шляху використовуються в багатьох місцях коду Yii. Наприклад, [[yii\caching\FileCache::cachePath]]
може використовувати як псевдонім шляху, так і звичайний шлях до каталогу.
Псевдоніми шляху тісно повʼязані з простором імен класів. Рекомендується, що ви визначите псевдонім шляху
для кожного базового простору імен, таким чином завантажувач класів Yii може використовуватися без будь-якої
додаткової конфігурації. Наприклад, `@yii` відповідає каталогу встановлення Yii, тому клас `yii\webRequest`
може бути завантажений. Якщо ви використовуєте сторонні бібліотеки, такі як Zend Framework, ви можете також визначити
псевдонім шляху `@Zend`, який відповідає каталогу встановлення фреймворка. Одного разу зробивши це - Yii буде
здатний автоматично завантажувати будь-який клас Zend Framework.
Більш детальна інформація про конфігурації представлена у розділі [Псевдоніми шляху](concept-aliases.md).
Представлення
-------------
Однією із основних змін в Yii2 є те, що спеціальна змінна `$this` у представленні більше не відповідає
поточному контролеру або віджету. Замість цього, `$this` тепер відповідає обʼєкту *представлення*, нової можливості,
яка була введена у версії 2.0. Обʼєкт представлення має тип [[yii\web\View]], який являє собою частину *представлення*
у шаблоні проектування MVC. Якщо ви хочете отримати доступ до контролера або віджету, то використовуйте вираз `$this->context`.
Для рендеринга часткових представлень тепер використовується метод `$this->render()`, а не `$this->renderPartial()`.
Результат виклику методу `render` тепер повинен бути виведений безпосередньо, так як `render` повертає результат
рендеринга, а не відображає його одразу. Наприклад,
```php
echo $this->render('_item', ['item' => $item]);
```
Крім використання PHP у якості основного шаблонізатору, Yii 2.0 також включає офіційні розширення для основних
популярних шаблонізаторів: Smarty і Twig. Шаблонізатор Prado більше не підтримується. Для використання вказаних
шаблонізаторів вам необхідно налаштувати компонент додатка `view` за допомогою вказівки властивостей
[[yii\base\View::$renderers|View::$renderers]].
Більш детальна інформація представлена у розділі [Шаблонізатори](tutorial-template-engines.md).
Моделі
------
Yii 2.0 використовує основний клас [[yii\base\Model]] для моделей, аналогічний класу `CModel` у версії 1.1.
Клас `CFormModel` більше не підтримується. Замість цього, для створення моделі форми у Yii 2.0 ви повинні
безпосередньо успадковуватися від класу [[yii\base\Model]].
У Yii 2.0 зʼявився новий метод [[yii\base\Model::scenarios()|scenarios()]] для оголошення сценаріїв, які підтримуються,
і для позначення в якому сценарії атрибути повинні перевірятися, вважатися безпечними і т. п. Наприклад,
```php
public function scenarios()
{
return [
'backend' => ['email', 'role'],
'frontend' => ['email', '!role'],
];
}
```
У прикладі вище, оголошено два сценарії: `backend` і `frontend`. Для сценарію `backend` обидва атрибута `email` і `role`
є безпечними і можуть бути масово привласнені. Для сценарію `frontend` атрибут `email` може бути масово присвоєний,
а атрибут `role` - ні. Обидва атрибути `email` та `role` повинні бути перевірені за допомогою правил валідації.
Метод [[yii\base\Model::rules()|rules()]] як і раніше використовується для оголошення правил валідації.
Зверніть увагу, що у звʼязку з появою нового методу [[yii\base\Model::scenarios()|scenarios()]] -
більше не підтримується валідатор `unsafe`.
У більшості випадків вам не потрібно перевизначати метод [[yii\base\Model::scenarios()|scenarios()]],
якщо метод [[yii\base\Model::rules()|rules()]] повністю вказує всі існуючі сценарії і якщо немає потреби
в оголошенні атрибутів небезпечними.
Більш детальна інформація представлена у розділі [Моделі](structure-models.md).
Контролери
----------
В якості базового класу для контролерів в Yii 2.0 використовується [[yii\web\Controller]],
аналогічний `CWebController` у Yii 1.1. Базовим класом для всіх дій є [[yii\base\Action]].
Однією із основних змін є те, що дія контролера тепер має повернути результат замість того, щоб напряму виводити його:
```php
public function actionView($id)
{
$model = \app\models\Post::findOne($id);
if ($model) {
return $this->render('view', ['model' => $model]);
} else {
throw new \yii\web\NotFoundHttpException;
}
}
```
Більш детальна інформація представлена у розділі [Контролери](structure-controllers.md).
Віджети
-------
У Yii 2.0 клас [[yii\base\Widget]] використовується як базовий клас для віджетів, аналогічно `CWidget` у Yii 1.1.
Для кращої підтримки фреймворку в IDE, Yii 2.0 використовує новий синтаксис для віджетів. Нові статичні методи
[[yii\base\Widget::begin()|begin()]], [[yii\base\Widget::end()|end()]], та [[yii\base\Widget::widget()|widget()]]
використовуються таким чином:
```php
use yii\widgets\Menu;
use yii\widgets\ActiveForm;
// Зверніть увагу, що ви повинні виводити результат
echo Menu::widget(['items' => $items]);
// Вказуємо масив для конфігурації властивостей обʼєкта
$form = ActiveForm::begin([
'options' => ['class' => 'form-horizontal'],
'fieldConfig' => ['inputOptions' => ['class' => 'input-xlarge']],
]);
... поля форми ...
ActiveForm::end();
```
Більш детальна інформація представлена у розділі [Віджети](structure-widgets.md).
Теми
----
У Yii 2.0 теми працюють абсолютно по-іншому. Тепер вони засновані на механізмі співставлення шляхів вихідного файлу
представлення із темізованим файлом. Наприклад, якщо використовується співставлення шляхів
`['/web/views' => '/web/themes/basic']`, то темізована версія файлу представлення `/web/views/site/index.php` буде
знаходитися у `/web/themes/basic/site/index.php`. З цієї причини теми можуть бути застосовані до будь-якого файлу
представлення, навіть до представлення, яке відрендерене всередині контексту контролера або віджету. Також, більше
не існує компонента `CThemeManager`. Замість цього, `theme` є конфігурованою властивістю компонента додатка `view`.
Більш детальна інформація представлена у розділі [Темізація](output-theming.md).
Консольні додатки
-----------------
Консольні додатки тепер організовані як контролери, аналогічно веб додаткам. Консольні контролери
повинні бути успадковані від класу [[yii\console\Controller]], аналогічного `CConsoleCommand` у версії 1.1.
Для виконання консольної команди, використовуйте `yii <маршрут>`, де `<маршрут>` це маршрут контролера
(наприклад, `sitemap/index`). Додаткові анонімні аргументи будуть передані у якості параметрів відповідній дії
контролера, у той час, як іменовані аргументи будуть передані у відповідності із оголошеннями у
[[yii\console\Controller::options()]].
Yii 2.0 підтримує автоматичну генерацію довідкової інформації із блоків коментарів.
Більш детальна інформація представлена у розділі [Консольні команди](tutorial-console.md).
I18N
----
У Yii 2.0 були прибрані вбудовані форматтери часу та чисел на користь
[PECL intl PHP розширення](http://pecl.php.net/package/intl).
Переклад повідомлень тепер здійснюється через компонент додатка `i18n`. Даний компонент управляє безліччю вихідних сховищ
повідомлень, що дозволяє вам використовувати різні сховища для вихідних повідомлень залежно від категорії повідомлення.
Більш детальна інформація представлена у розділі [Інтернаціоналізація](tutorial-i18n.md).
Фільтри дій
-----------
Фільтри дій тепер зроблені за допомогою поведінок. Для визначення нового фільтру - успадкуйте від [[yii\base\ActionFilter]].
Для використання фільтра - прикріпіть його до контролера у якості поведінки. Наприклад, для використання фільтра
[[yii\filters\AccessControl]] слід зробити наступне:
```php
public function behaviors()
{
return [
'access' => [
'class' => 'yii\filters\AccessControl',
'rules' => [
['allow' => true, 'actions' => ['admin'], 'roles' => ['@']],
],
],
];
}
```
Більш детальна інформація представлена у розділі [Фільтри](structure-filters.md).
Ресурси
-------
У Yii 2.0 представлена нова можливість *звʼязки ресурсів*, яка замінює концепт пакетів скриптів у Yii 1.1.
Звʼязка ресурсів - це колекція файлів ресурсів (наприклад, Javascript файли, CSS файли, файли зображень, і т. п.)
у певній папці. Кожна звʼязка ресурсів представленакласом, успадкованим від [[yii\web\AssetBundle]].
Звʼязка ресурсів стає доступною через веб, за допомогою реєстрації її методом [[yii\web\AssetBundle::register()]].
На відміну від Yii 1.1, сторінка, яка реєструє звʼязку ресурсів, автоматично буде містити посилання на
Javascript і CSS файли, які зазначені у звʼязці.
Більш детальна інформація представлена у розділі [Ресурси](structure-assets.md).
Хелпери
-------
У Yii 2.0 включено багато широко використовуваних статичних класів.
* [[yii\helpers\Html]]
* [[yii\helpers\ArrayHelper]]
* [[yii\helpers\StringHelper]]
* [[yii\helpers\FileHelper]]
* [[yii\helpers\Json]]
Більш детальна інформація представлена у розділі [Хелпери](helper-overview.md).
Форми
-----
Yii 2.0 вводить нове поняття *поле* для побудови форм за допомогою [[yii\widgets\ActiveForm]]. Поле - це
контейнер, що містить лейбл, поле введення, повідомлення про помилку і/або допоміжний текст.
Поле представлено обʼєктом [[yii\widgets\ActiveField|ActiveField]]. Використовуючи поля, ви можете будувати
форми набагато простіше, ніж це було раніше:
```php
<?php $form = yii\widgets\ActiveForm::begin(); ?>
<?= $form->field($model, 'username') ?>
<?= $form->field($model, 'password')->passwordInput() ?>
<div class="form-group">
<?= Html::submitButton('Login') ?>
</div>
<?php yii\widgets\ActiveForm::end(); ?>
```
Більш детальна інформація представлена у розділі [Робота з формами](input-forms.md).
Конструктор запитів
-------------------
У версії 1.1, побудова запиту була розкидана серед декількох класів, включаючи `CDbCommand`,
`CDbCriteria` та `CDbCommandBuilder`. У Yii 2.0 запит до БД представлений в рамках обʼєкта [[yii\db\Query|Query]],
який може бути перетворений у SQL вираз за допомогою [[yii\db\QueryBuilder|QueryBuilder]]. Наприклад,
```php
$query = new \yii\db\Query();
$query->select('id, name')
->from('user')
->limit(10);
$command = $query->createCommand();
$sql = $command->sql;
$rows = $command->queryAll();
```
Найкращим способом використання даних методів є робота з [Active Record](db-active-record.md).
Більш детальна інформація представлена у розділі [Конструктор запитів](db-query-builder.md).
Active Record
-------------
У Yii 2.0 внесено безліч змін в роботу [Active Record](db-active-record.md). Два основних з них включають в себе
побудову запитів і роботу із звʼязками.
Клас `CDbCriteria` у версії 1.1 був замінений на [[yii\db\ActiveQuery]] у Yii 2.0. Це клас успадковується від
[[yii\db\Query]] і таким чином отримує всі методи, які необхідні для побудови запиту. Для побудови запиту вам слід
викликати метод [[yii\db\ActiveRecord::find()]]:
```php
// Отримуємо всіх *активних* клієнтів і сортуємо їх по ID
$customers = Customer::find()
->where(['status' => $active])
->orderBy('id')
->all();
```
Для оголошення звʼязку слід просто оголосити геттер, який повертає обʼєкт [[yii\db\ActiveQuery|ActiveQuery]].
Імʼя властивості, яке визначене геттером, представляє собою назву звʼязку. Наприклад, наступний код оголошує звʼязок
`orders` (у версії 1.1 вам потрібно було б оголосити звʼязки в одному центральному місці - `relations()`):
```php
class Customer extends \yii\db\ActiveRecord
{
public function getOrders()
{
return $this->hasMany('Order', ['customer_id' => 'id']);
}
}
```
Тепер ви можете використовувати вираз `$customer->orders` для отримання всіх замовлень клієнта із звʼязаної таблиці.
Ви також можете використовувати наступний код, щоб застосувати потрібні умови "на льоту":
```php
$orders = $customer->getOrders()->andWhere('status=1')->all();
```
Yii 2.0 здійснює жадібне завантаження (eager loading) звʼязку по-іншому, на відміну від версії 1.1. Зокрема,
у версії 1.1 для вибору даних із основної і звʼязаної таблиць буде використаний запит JOIN. У Yii 2.0 будуть виконані
два запити без використання JOIN: перший запит повертає дані для основної таблиці, а другий - для звʼязаної,
за допомогою фільтрації по первинних ключах основної таблиці.
Замість того, щоб повертати обʼєкти [[yii\db\ActiveRecord|ActiveRecord]], ви можете використовувати метод
[[yii\db\ActiveQuery::asArray()|asArray()]] при побудові запиту, для вибірки великої кількості записів.
Це змусить повернути результат запиту у якості масива, що може суттєво знизити час, який потрібен ЦПУ і памʼяті
при великій кількості записів. Наприклад:
```php
$customers = Customer::find()->asArray()->all();
```
Ще одна зміна повʼязана з тим, що ви більше не можете визначати значення по-замовчуванням як властивостей.
Ви повинні встановити їх у методі `init` вашого класу, якщо це потрібно.
```php
public function init()
{
parent::init();
$this->status = self::STATUS_NEW;
}
```
Також у версії 1.1 були деякі проблеми із перевизначенням конструктора ActiveRecord. Дані проблеми не присутні
у версії 2.0. Зверніть увагу, що при додаванні параметрів у конструктор, вам, можливо, знадобиться перевизначити метод
[[yii\db\ActiveRecord::instantiate()]]. Перевизначення може не знадобитися, якщо параметри, передані в конструктор
матимуть значення за умовчанням, наприклад `null`.
Існує також безліч інших покращень у ActiveRecord. Більш детальна інформація про конфігурацію представлена у розділі
[Active Record](db-active-record.md).
Компонент додатку `user`
------------------------
Клас `CWebUser` у версії 1.1 тепер замінений класом [[yii\web\User]], а також більше не існує класу `CUserIdentity`.
Замість цього, ви повинні надати реалізацію інтерфейсу [[yii\web\IdentityInterface]], що набагато простіше у використанні.
Більш детальна інформація представлена у розділах [Аутентифікація](security-authentication.md),
[Авторизація](security-authorization.md) та [Шаблон додатка advanced](tutorial-advanced-app.md).
Розбір та генерація URL
-----------------------
Робота з URL в Yii 2.0 аналогічна тій, що була у версії 1.1. Основна зміна полягає в тому, що тепер підтримуються
додаткові параметри. Наприклад, якщо у вас є правило, оголошене наступним чином, то воно співпаде з `post/popular` та
`post/1/popular`. У версії 1.1 вам довелося б використовувати два правила для отримання того ж результату.
```php
[
'pattern' => 'post/<page:\d+>/<tag>',
'route' => 'post/index',
'defaults' => ['page' => 1],
]
```
Більш детальна інформація представлена у розділі [Розбір та генерація URL](runtime-url-handling.md).
Використання Yii 1.1 разом із 2.x
---------------------------------
Інформація про використання коду для Yii 1.1 разом із Yii 2.0 представлена у розділі
[Одночасне використання Yii 1.1 та 2.0](extend-using-v1-v2.md).

52
docs/guide-uk/intro-yii.md

@ -0,0 +1,52 @@
Що таке Yii?
============
Yii – це високопродуктивний компонентний PHP фреймворк, призначений для швидкої розробки сучасних веб додатків.
Слово Yii (вимовляється як `Йі` `[ji:]`) в китайській мові означає "простий та еволюційний".
Також Yii може розшифровуватись акронім **Yes It Is**!
Для яких завдань найбільше підходить Yii?
-----------------------------------------
Yii – це універсальний фреймворк і може бути задіяний у всіх типах веб додатків. Завдяки його компонентній структурі
і відмінній підтримці кешування, фреймворк особливо підходить для розробки таких великих проектів як портали,
форуми, CMS, магазини або RESTful-додатки.
Порівняння Yii з іншими фреймворками
------------------------------------
- Як і багато інших PHP фреймворків, для організації коду Yii використовує модель MVC (Model-View-Controller).
- Yii дотримується філософії простого й елегантного коду, не намагаючись ускладнювати дизайн тільки заради слідування
будь-яким шаблонами проектування.
- Yii є full-stack фреймворком і включає в себе перевірені можливості, які добре себе зарекомендували,
такі як ActiveRecord для реляційних та NoSQL баз даних, підтримку REST API, багаторівневе кешування та інші.
- Yii відмінно розширюваний. Ви можете налаштувати або замінити практично будь-яку частину основного коду.
Використовуючи архітектуру розширень - легко ділитися кодом або використовувати код спільноти.
- Висока продуктивність завжди є головною ціллю Yii.
Yii — не проект однієї людини. Він підтримується і розвивається [сильною командою][] і великою спільнотою розробників,
які їй допомагають. Розробники фреймворка стежать за тенденціями веб розробки і розвитком інших проектів.
Найбільш значимі можливості і кращі практики регулярно впроваджуються у фреймворк у вигляді простих і елегантних інтерфейсів.
[сильна команда розробників]: http://www.yiiframework.com/about/
Версії Yii
----------
На даний момент існує дві основні гілки Yii: 1.1 та 2.0. Гілка 1.1 є попереднім поколінням і знаходиться у стані підтримки.
Версія 2.0 - це повністю переписаний Yii, що використовує останні технології і протоколи, такі як Composer, PSR, простори імен,
типажі (traits) і багато іншого. 2.0 - останнє покоління фреймворка. На цій версії будуть зосереджені основні зусилля
кілька наступних років. Даний посібник призначений в основному версії 2.0.
Вимоги до ПЗ і знань
--------------------
Yii 2.0 потребує PHP 5.4.0 та вище. Щоб дізнатися вимоги для окремих можливостей ви можете запустити скрипт перевірки вимог,
який поставляється із кожним релізом фреймворка.
Для розробки на Yii буде потрібне загальне розуміння ООП, так як фреймворк повністю слідує цій парадигмі.
Також слід вивчити такі сучасні можливості PHP як [простори імен](http://www.php.net/manual/en/language.namespaces.php)
і [типажі](http://www.php.net/manual/en/language.oop5.traits.php).

2
docs/guide/concept-aliases.md

@ -109,7 +109,7 @@ The following is the list of the predefined aliases:
- `@yii`: the directory where the `BaseYii.php` file is located (also called the framework directory).
- `@app`: the [[yii\base\Application::basePath|base path]] of the currently running application.
- `@runtime`: the [[yii\base\Application::runtimePath|runtime path]] of the currently running application.
- `@vendor`: the [[yii\base\Application::vendorPath|Composer vendor directory].
- `@vendor`: the [[yii\base\Application::vendorPath|Composer vendor directory]].
- `@webroot`: the Web root directory of the currently running Web application.
- `@web`: the base URL of the currently running Web application.

2
docs/guide/extend-creating-extensions.md

@ -3,7 +3,7 @@ Extending Yii
> Note: This section is under development.
The Yii framework was designed to be easily extendable. Additional features can be added to your project and then reused, either by yourself on other projects or by sharing your work as a formal Yii extension.
The Yii framework was designed to be easily extensible. Additional features can be added to your project and then reused, either by yourself on other projects or by sharing your work as a formal Yii extension.
Code style
----------

39
docs/guide/extend-customizing-core.md

@ -1,39 +0,0 @@
Helper Classes
==============
> Note: This section is under development.
Yii provides many classes that help simplify common coding tasks, such as string or array manipulations,
HTML code generation, and so forth. These helper classes are organized under the `yii\helpers` namespace and
are all static classes (meaning they contain only static properties and methods and should not be instantiated).
You use a helper class by directly calling one of its static methods:
```
use yii\helpers\ArrayHelper;
$c = ArrayHelper::merge($a, $b);
```
Extending Helper Classes
------------------------
To make helper classes easier to extend, Yii breaks each helper class into two classes: a base class (e.g. `BaseArrayHelper`)
and a concrete class (e.g. `ArrayHelper`). When you use a helper, you should only use the concrete version, never use the base class.
If you want to customize a helper, perform the following steps (using `ArrayHelper` as an example):
1. Name your class the same as the concrete class provided by Yii, including the namespace: `yii\helpers\ArrayHelper`
2. Extend your class from the base class: `class ArrayHelper extends \yii\helpers\BaseArrayHelper`.
3. In your class, override any method or property as needed, or add new methods or properties.
4. Tell your application to use your version of the helper class by including the following line of code in the bootstrap script:
```php
Yii::$classMap['yii\helpers\ArrayHelper'] = 'path/to/ArrayHelper.php';
```
Step 4 above will instruct the Yii class autoloader to load your version of the helper class instead of the one included in the Yii distribution.
> Tip: You can use `Yii::$classMap` to replace ANY core Yii class with your own customized version, not just helper classes.

18
docs/guide/extend-using-composer.md

@ -22,6 +22,20 @@ curl -s http://getcomposer.org/installer | php
We strongly recommend a global composer installation.
Installing Composer Class Autoloader
------------------------------------
Make sure the [entry script](concept-entry-scripts.md) of your application contains the following lines of code:
```php
// install Composer's class autoloader
require(__DIR__ . '/../vendor/autoload.php');
// include Yii class file
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
```
Working with composer
---------------------
@ -52,7 +66,7 @@ composer.phar update
As an example, packages on `dev-master` will constantly get new updates when you run `update`, while running `install` won't, unless you've pulled an update of the `composer.lock` file.
There are several paramaters available to the above commands. Very commonly used ones are `--no-dev`, which would skip packages in the `require-dev` section and `--prefer-dist`, which downloads archives if available, instead of checking out repositories to your `vendor` folder.
There are several parameters available to the above commands. Very commonly used ones are `--no-dev`, which would skip packages in the `require-dev` section and `--prefer-dist`, which downloads archives if available, instead of checking out repositories to your `vendor` folder.
> Composer commands must be executed within your Yii project's directory, where the `composer.json` file can be found.
Depending upon your operating system and setup, you may need to provide paths to the PHP executable and
@ -100,7 +114,7 @@ afterwards.
> Depending on the package additional configuration may be required (eg. you have to register a module in the config), but autoloading of the classes should be handled by composer.
Using a specifc version of a package
Using a specific version of a package
------------------------------------
Yii always comes with the latest version of a required library that it is compatible with, but allows you to use an older version if you need to.

54
docs/guide/helper-overview.md

@ -3,14 +3,26 @@ Helpers
> Note: This section is under development.
Helper classes typically contain static methods only and are used as follows:
Yii provides many classes that help simplify common coding tasks, such as string or array manipulations,
HTML code generation, and so on. These helper classes are organized under the `yii\helpers` namespace and
are all static classes (meaning they contain only static properties and methods and should not be instantiated).
You use a helper class by directly calling one of its static methods, like the following:
```php
use \yii\helpers\Html;
use yii\helpers\Html;
echo Html::encode('Test > test');
```
There are several classes provided by framework:
> Note: To support [extending helper classes](#extending-helper-classes), Yii breaks each core helper class
into two classes: a base class (e.g. `BaseArrayHelper`) and a concrete class (e.g. `ArrayHelper`).
When you use a helper, you should only use the concrete version and never use the base class.
## Core Helper Classes
The following core helper classes are provided in the Yii releases:
- ArrayHelper
- Console
@ -25,3 +37,39 @@ There are several classes provided by framework:
- StringHelper
- Url
- VarDumper
## Extending Helper Classes
To custom a core helper class (e.g. `yii\helpers\ArrayHelper`), you should extend from its corresponding base class
(e.g. `yii\helpers\BaseArrayHelper`) and name your class the same as the corresponding concrete class
(e.g. `yii\helpers\ArrayHelper`), including its namespace.
The following example shows how to customize the [[yii\helpers\ArrayHelper::merge()|merge()]] method of the
[[yii\helpers\ArrayHelper]] class:
```php
namespace yii\helpers;
use yii\helpers\BaseArrayHelper;
class ArrayHelper extends BaseArrayHelper
{
public static function merge($a, $b)
{
// your custom implementation
}
}
```
Save your class in a file named `ArrayHelper.php`. The file can be in any directory, such as `@app/components`.
Next, in your application's [entry script](structure-entry-scripts.md), add the following line of code
after including the `yii.php` file:
```php
Yii::$classMap['yii\helpers\ArrayHelper'] = 'path/to/ArrayHelper.php';
```
The above line instructs the [Yii class autoloader](concept-autoloading.md) to load your version of the helper
class, instead of the one included in the Yii releases.

2
docs/guide/rest-response-formatting.md

@ -15,7 +15,7 @@ with response formatting:
## Content Negotiation <a name="content-negotiation"></a>
Yii supports content negotiation via the [[yii\filters\ContentNegotiator]] filter. The the RESTful API base
Yii supports content negotiation via the [[yii\filters\ContentNegotiator]] filter. The RESTful API base
controller class [[yii\rest\Controller]] is equipped with this filter under the name of `contentNegotiator`.
The filer provides response format negotiation as well as language negotiation. For example, if a RESTful
API request contains the following header,

165
docs/guide/structure-extensions.md

@ -3,33 +3,36 @@ Extensions
> Note: This section is under development.
Extensions are redistributable software packages that extend Yii by providing extra features. For example,
the [yii2-debug](tool-debugger.md) extension adds a handy debug toolbar to every page in your application
to help you more easily grasp how the pages are generated. You can install and use extensions in your
applications to accelerate your development process. You can also package your code in terms of extensions
to share with other people your great work.
Extensions are redistributable software packages specifically designed to be used in Yii applications and provide
ready-to-use features. For example, the [yiisoft/yii2-debug](tool-debugger.md) extension adds a handy debug toolbar
at the bottom of every page in your application to help you more easily grasp how the pages are generated. You can
use extensions to accelerate your development process. You can also package your code as extensions to share with
other people your great work.
> Info: We use the term "extension" to refer to Yii-specific software packages. For general purpose software packages
that can be used without Yii, we will refer to them using the term "package".
## Getting Extensions
The easiest way of getting extensions is using [Composer](https://getcomposer.org/). The name "extension"
is also known as "package" in Composer's terminology. To do so, you will need to
## Using Extensions
To use an extension, you need to install it first. Most extensions are distributed as [Composer](https://getcomposer.org/)
packages, and you can take the following two simple steps to install such an extension:
1. modify the `composer.json` file of your application and specify which extensions (Composer packages) you want to install.
2. run `php composer.phar install` to install the specified extensions.
1. install [Composer](https://getcomposer.org/), which you probably have already done when
[installing Yii](start-installation.md).
2. [specify which repositories](https://getcomposer.org/doc/05-repositories.md#repository) you would like
to get extensions from. In most cases you can skip this if you only want to install open source extensions
hosted on [Packagist](https://packagist.org/) - the default and biggest Composer repository.
3. modify the `composer.json` file of your application and specify which extensions you want to use.
4. run `php composer.phar install` to install the specified extensions.
You may need to install [Composer](https://getcomposer.org/) if you do not have it. Composer is a dependency
manager. This means when installing a package, it will install all its dependent packages automatically.
You usually only need to do Step 1 and 2 once. You may need to do Step 3 and 4 multiple times depending on your
evolving requirements for extensions.
> Info: By default, Composer installs packages registered on [Packagist](https://packagist.org/) - the biggest repository
for open source Composer packages. You may also [create your own repository](https://getcomposer.org/doc/05-repositories.md#repository)
and configure Composer to use it. This is useful if you are developing closed open extensions and want to share
within your projects.
By default, extensions installed by Composer are stored under the `BasePath/vendor` directory, where `BasePath`
refers to the application's [base path](structure-applications.md#basePath).
Extensions installed by Composer are stored under the `BasePath/vendor` directory, where `BasePath` refers to the
application's [base path](structure-applications.md#basePath).
For example, to get the `yii2-imagine` extension maintained officially by Yii, modify your `composer.json` like the following:
For example, to install the `yiisoft/yii2-imagine` extension, modify your `composer.json` like the following:
```json
{
@ -43,33 +46,15 @@ For example, to get the `yii2-imagine` extension maintained officially by Yii, m
}
```
The installed extension will be located in the `vendor/yiisoft/yii2-imagine` directory.
After the installation, you should see the directory `yiisoft/yii2-imagine` under `BasePath/vendor`. You should
also see another directory `imagine/imagine` which contains the installed dependent package.
> Info: All extensions officially maintained by Yii are named as `yiisoft/yii2-xyz`, where `xyz` varies for different
extensions.
## Using Extensions
> Info: The `yiisoft/yii2-imagine` is a core extension developed and maintained by the Yii developer team. All
core extensions are hosted on [Packagist](https://packagist.org/) and named like `yiisoft/yii2-xyz`, where `xyz`
varies for different extensions.
In order for your applications to use extensions, you will need to install Composer's class autoloader. This is
necessary such that PHP classes in extensions can be properly autoloaded when you reference them in the application code.
To do so, use the following lines in the [entry script](concept-entry-scripts.md) of your application:
```php
// install Composer's class autoloader
require(__DIR__ . '/../vendor/autoload.php');
// include Yii class file
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
```
> Info: If you are using the [basic application template](start-installation.md) or
[advanced application template](tutorial-advanced-app.md), you do not need to do anything because the application templates
already contain the above code.
Now you can enjoy using the installed extensions. For example, to use the `yii2-imagine` extension shown in the last
subsection, you can write code like the following in your application, where `Image` is the class provided by
the extension:
Now you can use the installed extensions like they are part of your application. The following example shows
how you can use the `yii\imagine\Image` class provided by the `yiisoft/yii2-imagine` extension:
```php
use Yii;
@ -80,8 +65,98 @@ Image::thumbnail('@webroot/img/test-image.jpg', 120, 120)
->save(Yii::getAlias('@runtime/thumb-test-image.jpg'), ['quality' => 50]);
```
> Info: Extension classes are autoloaded using the [Yii class autoloader](concept-autoloading.md). Yii automatically
creates [aliases](concept-aliases.md#extension-aliases) for the root namespaces declared by the extensions.
Also make sure in the [application configuration](structure-applications.md#application-configurations), you have
configured `extension
## Creating Extensions
You may consider creating an extension when you feel the need to redistribute some of your great code so that
they can be easily reused by other people or in your other projects.
An extension can contain any code you like, such as a helper class, a widget, a module, etc.
It is recommended that you create an extension in terms of a Composer package so that it can be more easily
used elsewhere, liked described in the last subsection. Below are the steps you may follow to create an extension.
1. Put all the files you plan to include in the extension in a single directory. The directory should contain
no other irrelevant files. For simplicity, let's call this directory the extension's *root directory*.
2. Create a `composer.json` file directly under the root directory. The file is required by Composer, which describes
the metadata about your extension. Please refer to the [Composer Manual](https://getcomposer.org/doc/01-basic-usage.md#composer-json-project-setup)
for more details about the file format.
3. Create a VCS (version control system) repository to host the extension files. Any future development
and maintenance work about the extension should be done on this repository.
4. Register your extension with a Composer repository so that other users can find and install your extension.
If you are creating an open source extension, you can register it with [Packagist](https://packagist.org/);
If you are creating a private extension for internal use, you may register it with
[your own repository](https://getcomposer.org/doc/05-repositories.md#hosting-your-own).
As an example, you may refer to the [yiisoft/yii2-bootstrap](widget-bootstrap) extension which provides a set of
widgets encapsulating the Twitter Bootstrap plugins. The extension is hosted on [GitHub](https://github.com/yiisoft/yii2-bootstrap)
and registered with [Packagist](https://packagist.org/packages/yiisoft/yii2-bootstrap). Below is the content
of its `composer.json` file (some unimportant content is removed for simplicity):
```json
{
"name": "yiisoft/yii2-bootstrap",
"description": "The Twitter Bootstrap extension for the Yii framework",
"keywords": ["yii2", "bootstrap"],
"type": "yii2-extension",
"license": "BSD-3-Clause",
"require": {
"yiisoft/yii2": "*",
"twbs/bootstrap": "3.1.* | 3.0.*"
},
"autoload": {
"psr-4": {
"yii\\bootstrap\\": ""
}
}
}
```
### Yii2 Extensions
When creating a Composer package, you may specify the package type to be `yii2-extension`, like shown in the example
in the last subsection. This is recommended if your package is an extension that is specifically designed to be
used in Yii applications. By using `yii2-extension` as the package type, your package can get the following extra benefits:
For example, if your package contains a Yii [widget](structure-widgets.md), it is most likely
that the package
## Installing Extensions Manually
In some rare occasions, you may want to install some or all extensions manually, rather than relying on Composer.
To do so, you should
1. download the extension archive files and unpack them in the `vendor` directory.
2. install the class autoloaders provided by the extensions, if any.
3. download and install all dependent extensions as instructed.
If an extension does not have a class autoloader but follows the
[PSR-4 standard](https://github.com/php-fig/fig-standards/blob/master/proposed/psr-4-autoloader/psr-4-autoloader.md),
you may use the class autoloader provided by Yii to autoload the extension classes. All you need to do is just to
declare a [root alias](concept-aliases.md#defining-aliases) for the extension root directory. For example,
assuming you have installed an extension in the directory `vendor/mycompany/myext`, and the extension classes
are under the `myext` namespace, then you can include the following code in your application configuration:
```php
[
'aliases' => [
'@myext' => '@vendor/mycompany/myext',
],
]
```
## Core Extensions
## Best Practices

1
docs/guide/tutorial-mailing.md

@ -141,6 +141,7 @@ Layout can be used to setup mail CSS styles or other shared content:
use yii\helpers\Html;
/* @var $this \yii\web\View view component instance */
/* @var $message \yii\mail\MessageInterface the message being composed */
/* @var $content string main view render result */
?>
<?php $this->beginPage() ?>

2
extensions/authclient/README.md

@ -63,7 +63,7 @@ class SiteController extends Controller
'class' => 'yii\authclient\AuthAction',
'successCallback' => [$this, 'successCallback'],
],
]
];
}
public function successCallback($client)

1
extensions/bootstrap/CHANGELOG.md

@ -9,6 +9,7 @@ Yii Framework 2 bootstrap extension Change Log
- Bug #3749: Fixed invalid plugin registration and ensure clickable links in dropdown (kartik-v)
- Enh #4024: Added ability to `yii\bootstrap\Tabs` to encode each `Tabs::items['label']` separately (creocoder, umneeq)
- Chg #3036: Upgraded Twitter Bootstrap to 3.1.x (qiangxue)
- Enh #4120: Added ability for each item to choose it's encoding option in `Dropdown` and `Nav` (Alex-Code)
2.0.0-beta April 13, 2014
-------------------------

3
extensions/bootstrap/Dropdown.php

@ -80,7 +80,8 @@ class Dropdown extends Widget
if (!isset($item['label'])) {
throw new InvalidConfigException("The 'label' option is required.");
}
$label = $this->encodeLabels ? Html::encode($item['label']) : $item['label'];
$encodeLabel = isset($item['encode']) ? $item['encode'] : $this->encodeLabels;
$label = $encodeLabel ? Html::encode($item['label']) : $item['label'];
$options = ArrayHelper::getValue($item, 'options', []);
$linkOptions = ArrayHelper::getValue($item, 'linkOptions', []);
$linkOptions['tabindex'] = '-1';

3
extensions/bootstrap/Nav.php

@ -148,7 +148,8 @@ class Nav extends Widget
if (!isset($item['label'])) {
throw new InvalidConfigException("The 'label' option is required.");
}
$label = $this->encodeLabels ? Html::encode($item['label']) : $item['label'];
$encodeLabel = isset($item['encode']) ? $item['encode'] : $this->encodeLabels;
$label = $encodeLabel ? Html::encode($item['label']) : $item['label'];
$options = ArrayHelper::getValue($item, 'options', []);
$items = ArrayHelper::getValue($item, 'items');
$url = ArrayHelper::getValue($item, 'url', '#');

3
extensions/elasticsearch/ActiveRecord.php

@ -416,8 +416,9 @@ class ActiveRecord extends BaseActiveRecord
$this->_version = $response['_version'];
$this->_score = null;
$changedAttributes = array_fill_keys(array_keys($values), null);
$this->setOldAttributes($values);
$this->afterSave(true, $values);
$this->afterSave(true, $changedAttributes);
return true;
}

1
extensions/elasticsearch/CHANGELOG.md

@ -7,6 +7,7 @@ Yii Framework 2 elasticsearch extension Change Log
- Bug #3587: Fixed an issue with storing empty records (cebe)
- Enh #3520: Added `unlinkAll()`-method to active record to remove all records of a model relation (NmDimas, samdark, cebe)
- Enh #3527: Added `highlight` property to Query and ActiveRecord. (Borales)
- Enh #4086: changedAttributes of afterSave Event now contain old values (dizews)
- Enh: Make error messages more readable in HTML output (cebe)
- Chg: asArray in ActiveQuery is now equal to using the normal Query. This means, that the output structure has changed and `with` is supported anymore. (cebe)
- Chg: Deletion of a record is now also considered successful if the record did not exist. (cebe)

34
extensions/elasticsearch/Connection.php

@ -433,23 +433,23 @@ class Connection extends Component
}
}
/**
* Try to decode error information if it is valid json, return it if not.
* @param $body
* @return mixed
*/
protected function decodeErrorBody($body)
{
try {
$decoded = Json::decode($body);
if (isset($decoded['error'])) {
$decoded['error'] = preg_replace('/\b\w+?Exception\[/', "<span style=\"color: red;\">\\0</span>\n ", $decoded['error']);
}
return $decoded;
} catch(InvalidParamException $e) {
return $body;
}
}
/**
* Try to decode error information if it is valid json, return it if not.
* @param $body
* @return mixed
*/
protected function decodeErrorBody($body)
{
try {
$decoded = Json::decode($body);
if (isset($decoded['error'])) {
$decoded['error'] = preg_replace('/\b\w+?Exception\[/', "<span style=\"color: red;\">\\0</span>\n ", $decoded['error']);
}
return $decoded;
} catch(InvalidParamException $e) {
return $body;
}
}
public function getNodeInfo()
{

9
extensions/mongodb/ActiveRecord.php

@ -224,8 +224,9 @@ abstract class ActiveRecord extends BaseActiveRecord
$this->setAttribute('_id', $newId);
$values['_id'] = $newId;
$changedAttributes = array_fill_keys(array_keys($values), null);
$this->setOldAttributes($values);
$this->afterSave(true, $values);
$this->afterSave(true, $changedAttributes);
return true;
}
@ -260,10 +261,12 @@ abstract class ActiveRecord extends BaseActiveRecord
throw new StaleObjectException('The object being updated is outdated.');
}
$changedAttributes = [];
foreach ($values as $name => $value) {
$this->setOldAttribute($name, $this->getAttribute($name));
$changedAttributes[$name] = $this->getOldAttribute($name);
$this->setOldAttribute($name, $value);
}
$this->afterSave(false, $values);
$this->afterSave(false, $changedAttributes);
return $rows;
}

1
extensions/mongodb/CHANGELOG.md

@ -8,6 +8,7 @@ Yii Framework 2 mongodb extension Change Log
- Enh #3520: Added `unlinkAll()`-method to active record to remove all records of a model relation (NmDimas, samdark, cebe)
- Enh #3778: Gii generator for Active Record model added (klimov-paul)
- Enh #3947: Migration support added (klimov-paul)
- Enh #4086: changedAttributes of afterSave Event now contain old values (dizews)
2.0.0-beta April 13, 2014

9
extensions/mongodb/file/ActiveRecord.php

@ -127,8 +127,9 @@ abstract class ActiveRecord extends \yii\mongodb\ActiveRecord
$this->setAttribute('_id', $newId);
$values['_id'] = $newId;
$changedAttributes = array_fill_keys(array_keys($values), null);
$this->setOldAttributes($values);
$this->afterSave(true, $values);
$this->afterSave(true, $changedAttributes);
return true;
}
@ -196,10 +197,12 @@ abstract class ActiveRecord extends \yii\mongodb\ActiveRecord
}
}
$changedAttributes = [];
foreach ($values as $name => $value) {
$this->setOldAttribute($name, $this->getAttribute($name));
$changedAttributes[$name] = $this->getOldAttribute($name);
$this->setOldAttribute($name, $value);
}
$this->afterSave(false, $values);
$this->afterSave(false, $changedAttributes);
return $rows;
}

3
extensions/redis/ActiveRecord.php

@ -124,8 +124,9 @@ class ActiveRecord extends BaseActiveRecord
}
$db->executeCommand('HMSET', $args);
$changedAttributes = array_fill_keys(array_keys($values), null);
$this->setOldAttributes($values);
$this->afterSave(true, $values);
$this->afterSave(true, $changedAttributes);
return true;
}

1
extensions/redis/CHANGELOG.md

@ -5,6 +5,7 @@ Yii Framework 2 redis extension Change Log
--------------------------
- Enh #3520: Added `unlinkAll()`-method to active record to remove all records of a model relation (NmDimas, samdark, cebe)
- Enh #4086: changedAttributes of afterSave Event now contain old values (dizews)
2.0.0-beta April 13, 2014

2
extensions/redis/Connection.php

@ -20,7 +20,7 @@ use yii\helpers\Inflector;
* When the server needs authentication, you can set the [[password]] property to
* authenticate with the server after connect.
*
* The ecexution of [redis commands](http://redis.io/commands) is possible with via [[executeCommand()]].
* The execution of [redis commands](http://redis.io/commands) is possible with via [[executeCommand()]].
*
* @method mixed set($key, $value) Set the string value of a key
* @method mixed get($key) Set the string value of a key

9
extensions/sphinx/ActiveRecord.php

@ -395,8 +395,9 @@ abstract class ActiveRecord extends BaseActiveRecord
return false;
}
$changedAttributes = array_fill_keys(array_keys($values), null);
$this->setOldAttributes($values);
$this->afterSave(true, $values);
$this->afterSave(true, $changedAttributes);
return true;
}
@ -530,10 +531,12 @@ abstract class ActiveRecord extends BaseActiveRecord
}
}
$changedAttributes = [];
foreach ($values as $name => $value) {
$this->setOldAttribute($name, $this->getAttribute($name));
$changedAttributes[$name] = $this->getOldAttribute($name);
$this->setOldAttribute($name, $value);
}
$this->afterSave(false, $values);
$this->afterSave(false, $changedAttributes);
return $rows;
}

3
extensions/sphinx/CHANGELOG.md

@ -6,8 +6,9 @@ Yii Framework 2 sphinx extension Change Log
- Bug #3668: Escaping of the special characters at 'MATCH' statement added (klimov-paul)
- Bug #4018: AR relation eager loading does not work with db models (klimov-paul)
- Enh: Added support for using sub-queries when building a DB query with `IN` condition (qiangxue)
- Enh #3520: Added `unlinkAll()`-method to active record to remove all records of a model relation (NmDimas, samdark, cebe)
- Enh #4086: changedAttributes of afterSave Event now contain old values (dizews)
- Enh: Added support for using sub-queries when building a DB query with `IN` condition (qiangxue)
- Chg #2287: Split `yii\sphinx\ColumnSchema::typecast()` into two methods `phpTypecast()` and `dbTypecast()` to allow specifying PDO type explicitly (cebe)

14
framework/CHANGELOG.md

@ -62,6 +62,9 @@ Yii Framework 2 Change Log
- Bug #3989: Fixed yii\log\FileTarget::$rotateByCopy to avoid any rename (cebe)
- Bug #3996: Traversing `Yii::$app->session` may cause a PHP error (qiangxue)
- Bug #4020: OCI column detection did not work so gii and other things failed (Sanya1991)
- Bug #4123: Trace level in logger had no effect in Targets, traces where not logged (cebe)
- Bug #4127: `CaptchaValidator` clientside error message wasn't formed properly (samdark)
- Bug #4162: Fixed bug where schema name was not used in ’SHOW CREATE TABLE’ query in `yii\db\mysql\Schema` (stevekr)
- Bug: Fixed inconsistent return of `\yii\console\Application::runAction()` (samdark)
- Bug: URL encoding for the route parameter added to `\yii\web\UrlManager` (klimov-paul)
- Bug: Fixed the bug that requesting protected or private action methods would cause 500 error instead of 404 (qiangxue)
@ -120,8 +123,17 @@ Yii Framework 2 Change Log
- Removed character maps for non-latin languages.
- Improved overall slug results.
- Added note about the fact that intl is required for non-latin languages to requirements checker.
- Enh #3992: In mail layouts you can now access the message object via `$message` variable (qiangxue)
- Enh #4028: Added ability to `yii\widgets\Menu` to encode each item's label separately (creocoder, umneeq)
- Enh #4072: `\yii\rbac\PhpManager` adjustments (samdark)
- Data is now stored in three separate files for items, assignments and rules. File format is simpler.
- Removed `authFile`. Added `itemFile`, `assignmentFile` and `ruleFile`.
- `createdAt` and `updatedAt` are now properly filled with corresponding file modification time.
- `save()` and `load()` are now protected instead of public.
- Added unit test for saving and loading data.
- Enh #4080: Added proper handling and support of the symlinked directories in `FileHelper`, added $options parameter in `FileHelper::removeDirectory()` (resurtm)
- Enh #4086: changedAttributes of afterSave Event now contain old values (dizews)
- Enh #4114: Added `Security::generateRandomBytes()`, improved tests (samdark)
- Enh: Added support for using sub-queries when building a DB query with `IN` condition (qiangxue)
- Enh: Supported adding a new response formatter without the need to reconfigure existing formatters (qiangxue)
- Enh: Added `yii\web\UrlManager::addRules()` to simplify adding new URL rules (qiangxue)
@ -154,6 +166,7 @@ Yii Framework 2 Change Log
- Chg #3956: Flash messages set via `Yii::$app->session->setFlash()` will be removed only if they are accessed (qiangxue)
- Chg #3989: The default value for `yii\log\FileTarget::$rotateByCopy` now defaults to true to work on windows by default (cebe)
- Chg #4071: `mail` component renamed to `mailer`, `yii\log\EmailTarget::$mail` renamed to `yii\log\EmailTarget::$mailer` (samdark)
- Chg #4147: `BaseMailer::compose()` will not overwrite the `message` parameter if it is explicitly provided (qiangxue)
- Chg: Replaced `clearAll()` and `clearAllAssignments()` in `yii\rbac\ManagerInterface` with `removeAll()`, `removeAllRoles()`, `removeAllPermissions()`, `removeAllRules()` and `removeAllAssignments()` (qiangxue)
- Chg: Added `$user` as the first parameter of `yii\rbac\Rule::execute()` (qiangxue)
- Chg: `yii\grid\DataColumn::getDataCellValue()` visibility is now `public` to allow accessing the value from a GridView directly (cebe)
@ -161,6 +174,7 @@ Yii Framework 2 Change Log
- Chg: Removed `yii\rest\ActiveController::$transactional` property and connected functionality (samdark)
- Chg: Changed the default value of the `keyPrefix` property of cache components to be null (qiangxue)
- Chg: Added `prefix` column to `yii\log\DbTarget` to have the same amount of information logged as in files and emails (cebe)
- Chg: Use `limit(null)` instead of `limit(-1)` in migration controller to be compatible to more backends (cebe)
- New #3911: Added `yii\behaviors\SluggableBehavior` that fills the specified model attribute with the transliterated and adjusted version to use in URLs (creocoder)

50
framework/UPGRADE.md

@ -61,6 +61,7 @@ Upgrade from Yii 2.0 Beta
* The behavior and signature of `ActiveRecord::afterSave()` has changed. `ActiveRecord::$isNewRecord` will now always be
false in afterSave and also dirty attributes are not available. This change has been made to have a more consistent and
expected behavior. The changed attributes are now available in the new parameter of afterSave() `$changedAttributes`.
`$changedAttributes` contains the old values of attributes that had changed and were saved.
* `ActiveRecord::updateAttributes()` has been changed to not trigger events and not respect optimistic locking anymore to
differentiate it more from calling `update(false)` and to ensure it can be used in `afterSave()` without triggering infinite
@ -73,6 +74,54 @@ Upgrade from Yii 2.0 Beta
* `mail` component was renamed to `mailer`, `yii\log\EmailTarget::$mail` was renamed to `yii\log\EmailTarget::$mailer`.
Please update all references in the code and config files.
* `\yii\rbac\PhpManager` now stores data in three separate files instead of one. In order to convert old file to
new ones save the following code as `convert.php` that should be placed in the same directory your `rbac.php` is in:
```php
<?php
$oldFile = 'rbac.php';
$itemsFile = 'items.php';
$assignmentsFile = 'assignments.php';
$rulesFile = 'rules.php';
$oldData = include $oldFile;
function saveToFile($data, $fileName) {
$out = var_export($data, true);
$out = "<?php\nreturn " . $out . ";";
$out = str_replace(['array (', ')'], ['[', ']'], $out);
file_put_contents($fileName, $out);
}
$items = [];
$assignments = [];
if (isset($oldData['items'])) {
foreach ($oldData['items'] as $name => $data) {
if (isset($data['assignments'])) {
foreach ($data['assignments'] as $userId => $assignmentData) {
$assignments[$userId] = $assignmentData['roleName'];
}
unset($data['assignments']);
}
$items[$name] = $data;
}
}
$rules = [];
if (isset($oldData['rules'])) {
$rules = $oldData['rules'];
}
saveToFile($items, $itemsFile);
saveToFile($assignments, $assignmentsFile);
saveToFile($rules, $rulesFile);
echo "Done!\n";
```
Run it once, delete `rbac.php`. If you've configured `authFile` property, remove the line from config and instead
configure `itemFile`, `assignmentFile` and `ruleFile`.
* Static helper `yii\helpers\Security` has been converted into an application component. You should change all usage of
its methods to a new syntax, for example: instead of `yii\helpers\Security::hashData()` use `Yii::$app->getSecurity()->hashData()`.
Default encryption and hash parameters has been upgraded. If you need to decrypt/validate data that was encrypted/hashed
@ -87,7 +136,6 @@ Upgrade from Yii 2.0 Beta
'deriveKeyStrategy' => 'hmac', // for PHP version < 5.5.0
//'deriveKeyStrategy' => 'pbkdf2', // for PHP version >= 5.5.0
'useDeriveKeyUniqueSalt' => false,
'autoGenerateSecretKey' => true,
],
// ...
],

40
framework/base/Security.php

@ -212,7 +212,7 @@ class Security extends Component
if (function_exists('hash_pbkdf2')) {
return hash_pbkdf2($this->derivationHash, $password, $salt, $this->derivationIterations, $this->cryptKeySize, true);
} else {
throw new InvalidConfigException('Derive key strategy "pbkdf2" requires PHP >= 5.5.0, either upgrade your environment or use another strategy.');
throw new InvalidConfigException('Security::$deriveKeyStrategy is set to "pbkdf2", which requires PHP >= 5.5.0. Either upgrade your run-time environment or use another strategy.');
}
}
@ -298,22 +298,46 @@ class Security extends Component
}
if (!isset($this->_keys[$name]) || $regenerate) {
$this->_keys[$name] = utf8_encode(static::generateRandomKey($length));
$this->_keys[$name] = $this->generateRandomKey($length);
file_put_contents($keyFile, json_encode($this->_keys));
}
return utf8_decode($this->_keys[$name]);
return $this->_keys[$name];
}
/**
* Generates a random key.
* Note the generated key is a binary string with the specified number of bytes in it.
* @param integer $length the length of the key that should be generated
* Generates specified number of random bytes.
* Note that output may not be ASCII.
* @see generateRandomKey() if you need a string.
*
* @param integer $length the number of bytes to generate
* @throws Exception on failure.
* @return string the generated random bytes
*/
public function generateRandomBytes($length = 32)
{
if (!extension_loaded('mcrypt')) {
throw new InvalidConfigException('The mcrypt PHP extension is not installed.');
}
$bytes = mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
if ($bytes === false) {
throw new Exception('Unable to generate random bytes.');
}
return $bytes;
}
/**
* Generates a random string of specified length.
* The string generated matches [A-Za-z0-9_.-]+
*
* @param integer $length the length of the key in characters
* @throws Exception Exception on failure.
* @return string the generated random key
*/
public function generateRandomKey($length = 32)
{
return mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
$bytes = $this->generateRandomBytes($length);
return strtr(StringHelper::byteSubstr(base64_encode($bytes), 0, $length), '+/=', '_-.');
}
/**
@ -448,7 +472,7 @@ class Security extends Component
}
// Get 20 * 8bits of random entropy
$rand = $this->generateRandomKey(20);
$rand = $this->generateRandomBytes(20);
// Add the microtime for a little more entropy.
$rand .= microtime(true);

4
framework/captcha/CaptchaValidator.php

@ -92,9 +92,9 @@ class CaptchaValidator extends Validator
'hash' => $hash,
'hashKey' => 'yiiCaptcha/' . $this->captchaAction,
'caseSensitive' => $this->caseSensitive,
'message' => strtr($this->message, [
'message' => Yii::$app->getI18n()->format($this->message, [
'attribute' => $object->getAttributeLabel($attribute),
]),
], Yii::$app->language),
];
if ($this->skipOnEmpty) {
$options['skipOnEmpty'] = 1;

10
framework/console/controllers/BaseMigrateController.php

@ -328,7 +328,7 @@ abstract class BaseMigrateController extends Controller
}
// try mark down
$migrations = array_keys($this->getMigrationHistory(-1));
$migrations = array_keys($this->getMigrationHistory(null));
foreach ($migrations as $i => $migration) {
if (strpos($migration, $version . '_') === 0) {
if ($i === 0) {
@ -544,7 +544,7 @@ abstract class BaseMigrateController extends Controller
protected function migrateToTime($time)
{
$count = 0;
$migrations = array_values($this->getMigrationHistory(-1));
$migrations = array_values($this->getMigrationHistory(null));
while ($count < count($migrations) && $migrations[$count] > $time) {
++$count;
}
@ -575,7 +575,7 @@ abstract class BaseMigrateController extends Controller
}
// try migrate down
$migrations = array_keys($this->getMigrationHistory(-1));
$migrations = array_keys($this->getMigrationHistory(null));
foreach ($migrations as $i => $migration) {
if (strpos($migration, $version . '_') === 0) {
if ($i === 0) {
@ -598,7 +598,7 @@ abstract class BaseMigrateController extends Controller
protected function getNewMigrations()
{
$applied = [];
foreach ($this->getMigrationHistory(-1) as $version => $time) {
foreach ($this->getMigrationHistory(null) as $version => $time) {
$applied[substr($version, 1, 13)] = true;
}
@ -621,7 +621,7 @@ abstract class BaseMigrateController extends Controller
/**
* Returns the migration history.
* @param integer $limit the maximum number of records in the history to be returned
* @param integer $limit the maximum number of records in the history to be returned. `null` for "no limit".
* @return array the migration history
*/
abstract protected function getMigrationHistory($limit);

3
framework/db/ActiveRecord.php

@ -462,8 +462,9 @@ class ActiveRecord extends BaseActiveRecord
}
}
$changedAttributes = array_fill_keys(array_keys($values), null);
$this->setOldAttributes($values);
$this->afterSave(true, $values);
$this->afterSave(true, $changedAttributes);
return true;
}

12
framework/db/BaseActiveRecord.php

@ -714,10 +714,12 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
throw new StaleObjectException('The object being updated is outdated.');
}
$changedAttributes = [];
foreach ($values as $name => $value) {
$this->_oldAttributes[$name] = $this->_attributes[$name];
$changedAttributes[$name] = isset($this->_oldAttributes[$name]) ? $this->_oldAttributes[$name] : null;
$this->_oldAttributes[$name] = $value;
}
$this->afterSave(false, $values);
$this->afterSave(false, $changedAttributes);
return $rows;
}
@ -875,7 +877,11 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
* the event is triggered.
* @param boolean $insert whether this method called while inserting a record.
* If false, it means the method is called while updating a record.
* @param array $changedAttributes The attribute values that had changed and were saved.
* @param array $changedAttributes The old values of attributes that had changed and were saved.
* You can use this parameter to take action based on the changes made for example send an email
* when the password had changed or implement audit trail that tracks all the changes.
* `$changedAttributes` gives you the old attribute values while the active record (`$this`) has
* already the new, updated values.
*/
public function afterSave($insert, $changedAttributes)
{

46
framework/db/cubrid/Schema.php

@ -298,27 +298,27 @@ class Schema extends \yii\db\Schema
return isset($typeMap[$type]) ? $typeMap[$type] : \PDO::PARAM_STR;
}
/**
* @inheritdoc
* @see http://www.cubrid.org/manual/91/en/sql/transaction.html#database-concurrency
*/
public function setTransactionIsolationLevel($level)
{
// translate SQL92 levels to CUBRID levels:
switch ($level) {
case Transaction::SERIALIZABLE:
$level = '6'; // SERIALIZABLE
break;
case Transaction::REPEATABLE_READ:
$level = '5'; // REPEATABLE READ CLASS with REPEATABLE READ INSTANCES
break;
case Transaction::READ_COMMITTED:
$level = '4'; // REPEATABLE READ CLASS with READ COMMITTED INSTANCES
break;
case Transaction::READ_UNCOMMITTED:
$level = '3'; // REPEATABLE READ CLASS with READ UNCOMMITTED INSTANCES
break;
}
parent::setTransactionIsolationLevel($level);
}
/**
* @inheritdoc
* @see http://www.cubrid.org/manual/91/en/sql/transaction.html#database-concurrency
*/
public function setTransactionIsolationLevel($level)
{
// translate SQL92 levels to CUBRID levels:
switch ($level) {
case Transaction::SERIALIZABLE:
$level = '6'; // SERIALIZABLE
break;
case Transaction::REPEATABLE_READ:
$level = '5'; // REPEATABLE READ CLASS with REPEATABLE READ INSTANCES
break;
case Transaction::READ_COMMITTED:
$level = '4'; // REPEATABLE READ CLASS with READ COMMITTED INSTANCES
break;
case Transaction::READ_UNCOMMITTED:
$level = '3'; // REPEATABLE READ CLASS with READ UNCOMMITTED INSTANCES
break;
}
parent::setTransactionIsolationLevel($level);
}
}

2
framework/db/mysql/Schema.php

@ -225,7 +225,7 @@ class Schema extends \yii\db\Schema
*/
protected function getCreateTableSql($table)
{
$row = $this->db->createCommand('SHOW CREATE TABLE ' . $this->quoteSimpleTableName($table->name))->queryOne();
$row = $this->db->createCommand('SHOW CREATE TABLE ' . $this->quoteTableName($table->fullName))->queryOne();
if (isset($row['Create Table'])) {
$sql = $row['Create Table'];
} else {

2
framework/filters/auth/QueryParamAuth.php

@ -47,6 +47,6 @@ class QueryParamAuth extends AuthMethod
*/
public function handleFailure($response)
{
throw new UnauthorizedHttpException('You are requesting with an invalid access token.');
throw new UnauthorizedHttpException(Yii::t('yii', 'You are requesting with an invalid access token.'));
}
}

11
framework/helpers/BaseFileHelper.php

@ -8,6 +8,7 @@
namespace yii\helpers;
use Yii;
use yii\base\InvalidConfigException;
use yii\base\InvalidParamException;
/**
@ -117,9 +118,17 @@ class BaseFileHelper
* @param boolean $checkExtension whether to use the file extension to determine the MIME type in case
* `finfo_open()` cannot determine it.
* @return string the MIME type (e.g. `text/plain`). Null is returned if the MIME type cannot be determined.
* @throws InvalidConfigException when the `fileinfo` PHP extension is not installed and `$checkExtension` is `false`.
*/
public static function getMimeType($file, $magicFile = null, $checkExtension = true)
{
if (!extension_loaded('fileinfo')) {
if ($checkExtension) {
return static::getMimeTypeByExtension($file, $magicFile);
} else {
throw new InvalidConfigException('The fileinfo PHP extension is not installed.');
}
}
$info = finfo_open(FILEINFO_MIME_TYPE, $magicFile);
if ($info) {
@ -131,7 +140,7 @@ class BaseFileHelper
}
}
return $checkExtension ? static::getMimeTypeByExtension($file) : null;
return $checkExtension ? static::getMimeTypeByExtension($file, $magicFile) : null;
}
/**

3
framework/log/Dispatcher.php

@ -9,6 +9,7 @@ namespace yii\log;
use Yii;
use yii\base\Component;
use yii\base\ErrorHandler;
/**
* Dispatcher manages a set of [[Target|log targets]].
@ -183,7 +184,7 @@ class Dispatcher extends Component
} catch (\Exception $e) {
$target->enabled = false;
$targetErrors[] = [
'Unable to send log via '. get_class($target) .': ' . $e->getMessage(),
'Unable to send log via ' . get_class($target) . ': ' . ErrorHandler::convertExceptionToString($e),
Logger::LEVEL_WARNING,
__METHOD__,
microtime(true),

9
framework/log/Target.php

@ -235,9 +235,16 @@ abstract class Target extends Component
if (!is_string($text)) {
$text = VarDumper::export($text);
}
$traces = [];
if (isset($message[4])) {
foreach($message[4] as $trace) {
$traces[] = "in {$trace['file']}:{$trace['line']}";
}
}
$prefix = $this->getMessagePrefix($message);
return date('Y-m-d H:i:s', $timestamp) . " {$prefix}[$level][$category] $text";
return date('Y-m-d H:i:s', $timestamp) . " {$prefix}[$level][$category] $text"
. (empty($traces) ? '' : "\n " . implode("\n ", $traces));
}
/**

55
framework/mail/BaseMailer.php

@ -144,6 +144,8 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont
return Yii::createObject($config);
}
private $_message;
/**
* Creates a new message instance and optionally composes its body content via view rendering.
*
@ -167,30 +169,41 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont
public function compose($view = null, array $params = [])
{
$message = $this->createMessage();
if ($view !== null) {
if ($view === null) {
return $message;
}
if (!array_key_exists('message', $params)) {
$params['message'] = $message;
if (is_array($view)) {
if (isset($view['html'])) {
$html = $this->render($view['html'], $params, $this->htmlLayout);
}
if (isset($view['text'])) {
$text = $this->render($view['text'], $params, $this->textLayout);
}
} else {
$html = $this->render($view, $params, $this->htmlLayout);
}
$this->_message = $message;
if (is_array($view)) {
if (isset($view['html'])) {
$html = $this->render($view['html'], $params, $this->htmlLayout);
}
if (isset($html)) {
$message->setHtmlBody($html);
if (isset($view['text'])) {
$text = $this->render($view['text'], $params, $this->textLayout);
}
if (isset($text)) {
$message->setTextBody($text);
} elseif (isset($html)) {
if (preg_match('|<body[^>]*>(.*?)</body>|is', $html, $match)) {
$html = $match[1];
}
$html = preg_replace('|<style[^>]*>(.*?)</style>|is', '', $html);
$message->setTextBody(strip_tags($html));
} else {
$html = $this->render($view, $params, $this->htmlLayout);
}
$this->_message = null;
if (isset($html)) {
$message->setHtmlBody($html);
}
if (isset($text)) {
$message->setTextBody($text);
} elseif (isset($html)) {
if (preg_match('|<body[^>]*>(.*?)</body>|is', $html, $match)) {
$html = $match[1];
}
$html = preg_replace('|<style[^>]*>(.*?)</style>|is', '', $html);
$message->setTextBody(strip_tags($html));
}
return $message;
}
@ -277,7 +290,7 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont
{
$output = $this->getView()->render($view, $params, $this);
if ($layout !== false) {
return $this->getView()->render($layout, ['content' => $output], $this);
return $this->getView()->render($layout, ['content' => $output, 'message' => $this->_message], $this);
} else {
return $output;
}

1
framework/messages/pl/yii.php

@ -20,6 +20,7 @@ return [
'(not set)' => '(brak wartości)',
'An internal server error occurred.' => 'Wystąpił wewnętrzny błąd serwera.',
'Delete' => 'Usuń',
'Are you sure you want to delete this item?' => 'Czy na pewno usunąć ten element?',
'Error' => 'Błąd',
'File upload failed.' => 'Wgrywanie pliku nie powiodło się.',
'Home' => 'Strona domowa',

6
framework/messages/th/yii.php

@ -64,11 +64,11 @@ return [
'in {delta, plural, =1{a month} other{# months}}' => 'ใน {delta} เดอน',
'in {delta, plural, =1{a year} other{# years}}' => 'ใน {delta} ป',
'the input value' => 'คาปอนทเขามา',
'{attribute} "{value}" has already been taken.' => '{attribute} "{value}" ไดบการบนท',
'{attribute} "{value}" has already been taken.' => '{attribute} "{value}" อยแล',
'{attribute} cannot be blank.' => '{attribute} ตองไมางเปลา',
'{attribute} is invalid.' => '{attribute} ไมกตอง',
'{attribute} is not a valid URL.' => '{attribute} URL ไมกตอง',
'{attribute} is not a valid email address.' => '{attribute} ปแบบอเมลไมกตอง',
'{attribute} is not a valid URL.' => '{attribute} ไมใชปแบบ URL ทกตอง',
'{attribute} is not a valid email address.' => '{attribute} ไมใชปแบบอเมลทกตอง',
'{attribute} must be "{requiredValue}".' => '{attribute} ตองการ "{requiredValue}"',
'{attribute} must be a number.' => '{attribute} ตองเปนตวเลขเทานน',
'{attribute} must be a string.' => '{attribute} ตองเปนตวอกขระเทานน',

2
framework/rbac/Assignment.php

@ -13,8 +13,6 @@ use yii\base\Object;
/**
* Assignment represents an assignment of a role to a user.
*
* It includes additional assignment information including [[ruleName]] and [[data]].
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Alexander Kochetov <creocoder@gmail.com>
* @since 2.0

371
framework/rbac/PhpManager.php

@ -26,34 +26,52 @@ use yii\helpers\VarDumper;
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Alexander Kochetov <creocoder@gmail.com>
* @author Christophe Boulain <christophe.boulain@gmail.com>
* @author Alexander Makarov <sam@rmcreative.ru>
* @since 2.0
*/
class PhpManager extends BaseManager
{
/**
* @var string the path of the PHP script that contains the authorization data.
* @var string the path of the PHP script that contains the authorization items.
* This can be either a file path or a path alias to the file.
* Make sure this file is writable by the Web server process if the authorization needs to be changed online.
* @see loadFromFile()
* @see saveToFile()
*/
public $authFile = '@app/data/rbac.php';
public $itemFile = '@app/rbac/items.php';
/**
* @var string the path of the PHP script that contains the authorization assignments.
* This can be either a file path or a path alias to the file.
* Make sure this file is writable by the Web server process if the authorization needs to be changed online.
* @see loadFromFile()
* @see saveToFile()
*/
public $assignmentFile = '@app/rbac/assignments.php';
/**
* @var string the path of the PHP script that contains the authorization rules.
* This can be either a file path or a path alias to the file.
* Make sure this file is writable by the Web server process if the authorization needs to be changed online.
* @see loadFromFile()
* @see saveToFile()
*/
public $ruleFile = '@app/rbac/rules.php';
/**
* @var Item[]
*/
private $_items = []; // itemName => item
protected $items = []; // itemName => item
/**
* @var array
*/
private $_children = []; // itemName, childName => child
protected $children = []; // itemName, childName => child
/**
* @var Assignment[]
*/
private $_assignments = []; // userId, itemName => assignment
protected $assignments = []; // userId, itemName => assignment
/**
* @var Rule[]
*/
private $_rules = []; // ruleName => rule
protected $rules = []; // ruleName => rule
/**
@ -64,7 +82,9 @@ class PhpManager extends BaseManager
public function init()
{
parent::init();
$this->authFile = Yii::getAlias($this->authFile);
$this->itemFile = Yii::getAlias($this->itemFile);
$this->assignmentFile = Yii::getAlias($this->assignmentFile);
$this->ruleFile = Yii::getAlias($this->ruleFile);
$this->load();
}
@ -82,7 +102,7 @@ class PhpManager extends BaseManager
*/
public function getAssignments($userId)
{
return isset($this->_assignments[$userId]) ? $this->_assignments[$userId] : [];
return isset($this->assignments[$userId]) ? $this->assignments[$userId] : [];
}
/**
@ -100,12 +120,12 @@ class PhpManager extends BaseManager
*/
protected function checkAccessRecursive($user, $itemName, $params, $assignments)
{
if (!isset($this->_items[$itemName])) {
if (!isset($this->items[$itemName])) {
return false;
}
/* @var $item Item */
$item = $this->_items[$itemName];
$item = $this->items[$itemName];
Yii::trace($item instanceof Role ? "Checking role: $itemName" : "Checking permission : $itemName", __METHOD__);
if (!$this->executeRule($user, $item, $params)) {
@ -116,7 +136,7 @@ class PhpManager extends BaseManager
return true;
}
foreach ($this->_children as $parentName => $children) {
foreach ($this->children as $parentName => $children) {
if (isset($children[$itemName]) && $this->checkAccessRecursive($user, $parentName, $params, $assignments)) {
return true;
}
@ -130,7 +150,7 @@ class PhpManager extends BaseManager
*/
public function addChild($parent, $child)
{
if (!isset($this->_items[$parent->name], $this->_items[$child->name])) {
if (!isset($this->items[$parent->name], $this->items[$child->name])) {
throw new InvalidParamException("Either '{$parent->name}' or '{$child->name}' does not exist.");
}
@ -144,11 +164,11 @@ class PhpManager extends BaseManager
if ($this->detectLoop($parent, $child)) {
throw new InvalidCallException("Cannot add '{$child->name}' as a child of '{$parent->name}'. A loop has been detected.");
}
if (isset($this->_children[$parent->name][$child->name])) {
if (isset($this->children[$parent->name][$child->name])) {
throw new InvalidCallException("The item '{$parent->name}' already has a child '{$child->name}'.");
}
$this->_children[$parent->name][$child->name] = $this->_items[$child->name];
$this->save();
$this->children[$parent->name][$child->name] = $this->items[$child->name];
$this->saveItems();
return true;
}
@ -165,10 +185,10 @@ class PhpManager extends BaseManager
if ($child->name === $parent->name) {
return true;
}
if (!isset($this->_children[$child->name], $this->_items[$parent->name])) {
if (!isset($this->children[$child->name], $this->items[$parent->name])) {
return false;
}
foreach ($this->_children[$child->name] as $grandchild) {
foreach ($this->children[$child->name] as $grandchild) {
/* @var $grandchild Item */
if ($this->detectLoop($parent, $grandchild)) {
return true;
@ -183,9 +203,9 @@ class PhpManager extends BaseManager
*/
public function removeChild($parent, $child)
{
if (isset($this->_children[$parent->name][$child->name])) {
unset($this->_children[$parent->name][$child->name]);
$this->save();
if (isset($this->children[$parent->name][$child->name])) {
unset($this->children[$parent->name][$child->name]);
$this->saveItems();
return true;
} else {
return false;
@ -197,7 +217,7 @@ class PhpManager extends BaseManager
*/
public function hasChild($parent, $child)
{
return isset($this->_children[$parent->name][$child->name]);
return isset($this->children[$parent->name][$child->name]);
}
/**
@ -205,18 +225,18 @@ class PhpManager extends BaseManager
*/
public function assign($role, $userId, $ruleName = null, $data = null)
{
if (!isset($this->_items[$role->name])) {
if (!isset($this->items[$role->name])) {
throw new InvalidParamException("Unknown role '{$role->name}'.");
} elseif (isset($this->_assignments[$userId][$role->name])) {
} elseif (isset($this->assignments[$userId][$role->name])) {
throw new InvalidParamException("Authorization item '{$role->name}' has already been assigned to user '$userId'.");
} else {
$this->_assignments[$userId][$role->name] = new Assignment([
$this->assignments[$userId][$role->name] = new Assignment([
'userId' => $userId,
'roleName' => $role->name,
'createdAt' => time(),
]);
$this->save();
return $this->_assignments[$userId][$role->name];
$this->saveAssignments();
return $this->assignments[$userId][$role->name];
}
}
@ -225,9 +245,9 @@ class PhpManager extends BaseManager
*/
public function revoke($role, $userId)
{
if (isset($this->_assignments[$userId][$role->name])) {
unset($this->_assignments[$userId][$role->name]);
$this->save();
if (isset($this->assignments[$userId][$role->name])) {
unset($this->assignments[$userId][$role->name]);
$this->saveAssignments();
return true;
} else {
return false;
@ -239,11 +259,11 @@ class PhpManager extends BaseManager
*/
public function revokeAll($userId)
{
if (isset($this->_assignments[$userId]) && is_array($this->_assignments[$userId])) {
foreach ($this->_assignments[$userId] as $itemName => $value) {
unset($this->_assignments[$userId][$itemName]);
if (isset($this->assignments[$userId]) && is_array($this->assignments[$userId])) {
foreach ($this->assignments[$userId] as $itemName => $value) {
unset($this->assignments[$userId][$itemName]);
}
$this->save();
$this->saveAssignments();
return true;
} else {
return false;
@ -255,7 +275,7 @@ class PhpManager extends BaseManager
*/
public function getAssignment($roleName, $userId)
{
return isset($this->_assignments[$userId][$roleName]) ? $this->_assignments[$userId][$roleName] : null;
return isset($this->assignments[$userId][$roleName]) ? $this->assignments[$userId][$roleName] : null;
}
/**
@ -265,7 +285,7 @@ class PhpManager extends BaseManager
{
$items = [];
foreach ($this->_items as $name => $item) {
foreach ($this->items as $name => $item) {
/* @var $item Item */
if ($item->type == $type) {
$items[$name] = $item;
@ -281,15 +301,15 @@ class PhpManager extends BaseManager
*/
public function removeItem($item)
{
if (isset($this->_items[$item->name])) {
foreach ($this->_children as &$children) {
if (isset($this->items[$item->name])) {
foreach ($this->children as &$children) {
unset($children[$item->name]);
}
foreach ($this->_assignments as &$assignments) {
foreach ($this->assignments as &$assignments) {
unset($assignments[$item->name]);
}
unset($this->_items[$item->name]);
$this->save();
unset($this->items[$item->name]);
$this->saveItems();
return true;
} else {
return false;
@ -301,7 +321,7 @@ class PhpManager extends BaseManager
*/
public function getItem($name)
{
return isset($this->_items[$name]) ? $this->_items[$name] : null;
return isset($this->items[$name]) ? $this->items[$name] : null;
}
/**
@ -310,10 +330,10 @@ class PhpManager extends BaseManager
public function updateRule($name, $rule)
{
if ($rule->name !== $name) {
unset($this->_rules[$name]);
unset($this->rules[$name]);
}
$this->_rules[$rule->name] = $rule;
$this->save();
$this->rules[$rule->name] = $rule;
$this->saveRules();
return true;
}
@ -322,7 +342,7 @@ class PhpManager extends BaseManager
*/
public function getRule($name)
{
return isset($this->_rules[$name]) ? $this->_rules[$name] : null;
return isset($this->rules[$name]) ? $this->rules[$name] : null;
}
/**
@ -330,7 +350,7 @@ class PhpManager extends BaseManager
*/
public function getRules()
{
return $this->_rules;
return $this->rules;
}
/**
@ -340,7 +360,7 @@ class PhpManager extends BaseManager
{
$roles = [];
foreach ($this->getAssignments($userId) as $name => $assignment) {
$roles[$name] = $this->_items[$assignment->roleName];
$roles[$name] = $this->items[$assignment->roleName];
}
return $roles;
@ -358,8 +378,8 @@ class PhpManager extends BaseManager
}
$permissions = [];
foreach (array_keys($result) as $itemName) {
if (isset($this->_items[$itemName]) && $this->_items[$itemName] instanceof Permission) {
$permissions[$itemName] = $this->_items[$itemName];
if (isset($this->items[$itemName]) && $this->items[$itemName] instanceof Permission) {
$permissions[$itemName] = $this->items[$itemName];
}
}
return $permissions;
@ -373,8 +393,8 @@ class PhpManager extends BaseManager
*/
protected function getChildrenRecursive($name, &$result)
{
if (isset($this->_children[$name])) {
foreach ($this->_children[$name] as $child) {
if (isset($this->children[$name])) {
foreach ($this->children[$name] as $child) {
$result[$child->name] = true;
$this->getChildrenRecursive($child->name, $result);
}
@ -398,8 +418,8 @@ class PhpManager extends BaseManager
$permissions = [];
foreach (array_keys($result) as $itemName) {
if (isset($this->_items[$itemName]) && $this->_items[$itemName] instanceof Permission) {
$permissions[$itemName] = $this->_items[$itemName];
if (isset($this->items[$itemName]) && $this->items[$itemName] instanceof Permission) {
$permissions[$itemName] = $this->items[$itemName];
}
}
return $permissions;
@ -410,7 +430,7 @@ class PhpManager extends BaseManager
*/
public function getChildren($name)
{
return isset($this->_children[$name]) ? $this->_children[$name] : [];
return isset($this->children[$name]) ? $this->children[$name] : [];
}
/**
@ -418,10 +438,10 @@ class PhpManager extends BaseManager
*/
public function removeAll()
{
$this->_children = [];
$this->_items = [];
$this->_assignments = [];
$this->_rules = [];
$this->children = [];
$this->items = [];
$this->assignments = [];
$this->rules = [];
$this->save();
}
@ -448,9 +468,9 @@ class PhpManager extends BaseManager
protected function removeAllItems($type)
{
$names = [];
foreach ($this->_items as $name => $item) {
foreach ($this->items as $name => $item) {
if ($item->type == $type) {
unset($this->_items[$name]);
unset($this->items[$name]);
$names[$name] = true;
}
}
@ -458,25 +478,25 @@ class PhpManager extends BaseManager
return;
}
foreach ($this->_assignments as $i => $assignment) {
foreach ($this->assignments as $i => $assignment) {
if (isset($names[$assignment->roleName])) {
unset($this->_assignments[$i]);
unset($this->assignments[$i]);
}
}
foreach ($this->_children as $name => $children) {
foreach ($this->children as $name => $children) {
if (isset($names[$name])) {
unset($this->_children[$name]);
unset($this->children[$name]);
} else {
foreach ($children as $childName => $item) {
if (isset($names[$childName])) {
unset($children[$childName]);
}
}
$this->_children[$name] = $children;
$this->children[$name] = $children;
}
}
$this->save();
$this->saveItems();
}
/**
@ -484,11 +504,11 @@ class PhpManager extends BaseManager
*/
public function removeAllRules()
{
foreach ($this->_items as $item) {
foreach ($this->items as $item) {
$item->ruleName = null;
}
$this->_rules = [];
$this->save();
$this->rules = [];
$this->saveRules();
}
/**
@ -496,8 +516,8 @@ class PhpManager extends BaseManager
*/
public function removeAllAssignments()
{
$this->_assignments = [];
$this->save();
$this->assignments = [];
$this->saveAssignments();
}
/**
@ -505,14 +525,14 @@ class PhpManager extends BaseManager
*/
protected function removeRule($rule)
{
if (isset($this->_rules[$rule->name])) {
unset($this->_rules[$rule->name]);
foreach ($this->_items as $item) {
if (isset($this->rules[$rule->name])) {
unset($this->rules[$rule->name]);
foreach ($this->items as $item) {
if ($item->ruleName === $rule->name) {
$item->ruleName = null;
}
}
$this->save();
$this->saveRules();
return true;
} else {
return false;
@ -524,8 +544,8 @@ class PhpManager extends BaseManager
*/
protected function addRule($rule)
{
$this->_rules[$rule->name] = $rule;
$this->save();
$this->rules[$rule->name] = $rule;
$this->saveRules();
return true;
}
@ -534,25 +554,25 @@ class PhpManager extends BaseManager
*/
protected function updateItem($name, $item)
{
$this->_items[$item->name] = $item;
$this->items[$item->name] = $item;
if ($name !== $item->name) {
if (isset($this->_items[$item->name])) {
if (isset($this->items[$item->name])) {
throw new InvalidParamException("Unable to change the item name. The name '{$item->name}' is already used by another item.");
}
if (isset($this->_items[$name])) {
unset ($this->_items[$name]);
if (isset($this->items[$name])) {
unset ($this->items[$name]);
if (isset($this->_children[$name])) {
$this->_children[$item->name] = $this->_children[$name];
unset ($this->_children[$name]);
if (isset($this->children[$name])) {
$this->children[$item->name] = $this->children[$name];
unset ($this->children[$name]);
}
foreach ($this->_children as &$children) {
foreach ($this->children as &$children) {
if (isset($children[$name])) {
$children[$item->name] = $children[$name];
unset ($children[$name]);
}
}
foreach ($this->_assignments as &$assignments) {
foreach ($this->assignments as &$assignments) {
if (isset($assignments[$name])) {
$assignments[$item->name] = $assignments[$name];
unset($assignments[$name]);
@ -560,7 +580,7 @@ class PhpManager extends BaseManager
}
}
}
$this->save();
$this->saveItems();
return true;
}
@ -577,9 +597,9 @@ class PhpManager extends BaseManager
$item->updatedAt = $time;
}
$this->_items[$item->name] = $item;
$this->items[$item->name] = $item;
$this->save();
$this->saveItems();
return true;
@ -588,95 +608,63 @@ class PhpManager extends BaseManager
/**
* Loads authorization data from persistent storage.
*/
public function load()
{
$this->_children = [];
$this->_rules = [];
$this->_assignments = [];
$this->_items = [];
$data = $this->loadFromFile($this->authFile);
if (isset($data['items'])) {
foreach ($data['items'] as $name => $item) {
$class = $item['type'] == Item::TYPE_PERMISSION ? Permission::className() : Role::className();
$this->_items[$name] = new $class([
'name' => $name,
'description' => isset($item['description']) ? $item['description'] : null,
'ruleName' => isset($item['ruleName']) ? $item['ruleName'] : null,
'data' => isset($item['data']) ? $item['data'] : null,
'createdAt' => isset($item['createdAt']) ? $item['createdAt'] : null,
'updatedAt' => isset($item['updatedAt']) ? $item['updatedAt'] : null,
]);
}
protected function load()
{
$this->children = [];
$this->rules = [];
$this->assignments = [];
$this->items = [];
$items = $this->loadFromFile($this->itemFile);
$itemsMtime = @filemtime($this->itemFile);
$assignments = $this->loadFromFile($this->assignmentFile);
$assignmentsMtime = @filemtime($this->assignmentFile);
$rules = $this->loadFromFile($this->ruleFile);
foreach ($items as $name => $item) {
$class = $item['type'] == Item::TYPE_PERMISSION ? Permission::className() : Role::className();
$this->items[$name] = new $class([
'name' => $name,
'description' => isset($item['description']) ? $item['description'] : null,
'ruleName' => isset($item['ruleName']) ? $item['ruleName'] : null,
'data' => isset($item['data']) ? $item['data'] : null,
'createdAt' => $itemsMtime,
'updatedAt' => $itemsMtime,
]);
}
foreach ($data['items'] as $name => $item) {
if (isset($item['children'])) {
foreach ($item['children'] as $childName) {
if (isset($this->_items[$childName])) {
$this->_children[$name][$childName] = $this->_items[$childName];
}
}
}
if (isset($item['assignments'])) {
foreach ($item['assignments'] as $userId => $assignment) {
$this->_assignments[$userId][$name] = new Assignment([
'userId' => $userId,
'roleName' => $assignment['roleName'],
'createdAt' => isset($assignment['createdAt']) ? $assignment['createdAt'] : null,
]);
foreach ($items as $name => $item) {
if (isset($item['children'])) {
foreach ($item['children'] as $childName) {
if (isset($this->items[$childName])) {
$this->children[$name][$childName] = $this->items[$childName];
}
}
}
}
if (isset($data['rules'])) {
foreach ($data['rules'] as $name => $ruleData) {
$this->_rules[$name] = unserialize($ruleData);
}
foreach ($assignments as $userId => $role) {
$this->assignments[$userId][$role] = new Assignment([
'userId' => $userId,
'roleName' => $role,
'createdAt' => $assignmentsMtime,
]);
}
foreach ($rules as $name => $ruleData) {
$this->rules[$name] = unserialize($ruleData);
}
}
/**
* Saves authorization data into persistent storage.
*/
public function save()
protected function save()
{
$items = [];
foreach ($this->_items as $name => $item) {
/* @var $item Item */
$items[$name] = array_filter([
'type' => $item->type,
'description' => $item->description,
'ruleName' => $item->ruleName,
'data' => $item->data,
]);
if (isset($this->_children[$name])) {
foreach ($this->_children[$name] as $child) {
/* @var $child Item */
$items[$name]['children'][] = $child->name;
}
}
}
foreach ($this->_assignments as $userId => $assignments) {
foreach ($assignments as $name => $assignment) {
/* @var $assignment Assignment */
if (isset($items[$name])) {
$items[$name]['assignments'][$userId] = [
'roleName' => $assignment->roleName,
];
}
}
}
$rules = [];
foreach ($this->_rules as $name => $rule) {
$rules[$name] = serialize($rule);
}
$this->saveToFile(['items' => $items, 'rules' => $rules], $this->authFile);
$this->saveItems();
$this->saveAssignments();
$this->saveRules();
}
/**
@ -706,4 +694,57 @@ class PhpManager extends BaseManager
{
file_put_contents($file, "<?php\nreturn " . VarDumper::export($data) . ";\n", LOCK_EX);
}
/**
* Saves items data into persistent storage.
*/
protected function saveItems()
{
$items = [];
foreach ($this->items as $name => $item) {
/* @var $item Item */
$items[$name] = array_filter(
[
'type' => $item->type,
'description' => $item->description,
'ruleName' => $item->ruleName,
'data' => $item->data,
]
);
if (isset($this->children[$name])) {
foreach ($this->children[$name] as $child) {
/* @var $child Item */
$items[$name]['children'][] = $child->name;
}
}
}
$this->saveToFile($items, $this->itemFile);
}
/**
* Saves assignments data into persistent storage.
*/
protected function saveAssignments()
{
$assignmentData = [];
foreach ($this->assignments as $userId => $assignments) {
foreach ($assignments as $name => $assignment) {
/* @var $assignment Assignment */
$assignmentData[$userId] = $assignment->roleName;
}
}
$this->saveToFile($assignmentData, $this->assignmentFile);
}
/**
* Saves rules data into persistent storage.
*/
protected function saveRules()
{
$rules = [];
foreach ($this->rules as $name => $rule) {
$rules[$name] = serialize($rule);
}
$this->saveToFile($rules, $this->ruleFile);
}
}

9
framework/requirements/requirements.php

@ -39,6 +39,13 @@ return array(
'memo' => 'Required for multibyte encoding string processing.'
),
array(
'name' => 'Mcrypt extension',
'mandatory' => false,
'condition' => extension_loaded('mcrypt'),
'by' => '<a href="http://www.yiiframework.com/doc-2.0/yii-base-security.html">Security Component</a>',
'memo' => 'Required by encrypt and decrypt methods.'
),
array(
'name' => 'Intl extension',
'mandatory' => false,
'condition' => $this->checkPhpExtensionVersion('intl', '1.0.2', '>='),
@ -50,7 +57,7 @@ return array(
),
array(
'name' => 'Fileinfo extension',
'mandatory' => true,
'mandatory' => false,
'condition' => extension_loaded('fileinfo'),
'by' => '<a href="http://www.php.net/manual/en/book.fileinfo.php">File Information</a>',
'memo' => 'Required for files upload to detect correct file mime-types.'

3
framework/views/migration.php

@ -9,8 +9,9 @@ echo "<?php\n";
?>
use yii\db\Schema;
use yii\db\Migration;
class <?= $className ?> extends \yii\db\Migration
class <?= $className ?> extends Migration
{
public function up()
{

4
tests/unit/data/travis/setup-apps.sh

@ -7,14 +7,14 @@ else
# basic application:
composer install --dev --prefer-dist -d apps/basic
cd apps/basic && composer require --dev codeception/codeception:1.8.*@dev codeception/specify:* codeception/verify:*
cd apps/basic && composer require --dev codeception/codeception:2.0.* codeception/specify:* codeception/verify:*
php vendor/bin/codecept build && cd ../..
# advanced application:
composer install --dev --prefer-dist -d apps/advanced
cd apps/advanced && composer require --dev codeception/codeception:1.8.*@dev codeception/specify:* codeception/verify:*
cd apps/advanced && composer require --dev codeception/codeception:2.0.* codeception/specify:* codeception/verify:*
./init --env=Development
sed -i s/root/travis/ common/config/main-local.php
cd backend && php ../vendor/bin/codecept build

2
tests/unit/framework/ar/ActiveRecordTestTrait.php

@ -827,12 +827,14 @@ trait ActiveRecordTestTrait
$customerClass = $this->getCustomerClass();
/* @var $this TestCase|ActiveRecordTestTrait */
// save
/* @var $customer Customer */
$customer = $customerClass::findOne(2);
$this->assertTrue($customer instanceof $customerClass);
$this->assertEquals('user2', $customer->name);
$this->assertFalse($customer->isNewRecord);
static::$afterSaveNewRecord = null;
static::$afterSaveInsert = null;
$this->assertEmpty($customer->dirtyAttributes);
$customer->name = 'user2x';
$customer->save();

15
tests/unit/framework/base/SecurityTest.php

@ -131,4 +131,19 @@ class SecurityTest extends TestCase
$decryptedData = $this->security->decrypt($encryptedData, $key);
$this->assertEquals($data, $decryptedData);
}
public function testGenerateRandomBytes()
{
$length = 21;
$key = $this->security->generateRandomBytes($length);
$this->assertEquals($length, strlen($key));
}
public function testGenerateRandomKey()
{
$length = 21;
$key = $this->security->generateRandomKey($length);
$this->assertEquals($length, strlen($key));
$this->assertEquals(1, preg_match('/[A-Za-z0-9_.-]+/', $key));
}
}

37
tests/unit/framework/rbac/ExposedPhpManager.php

@ -0,0 +1,37 @@
<?php
namespace yiiunit\framework\rbac;
use yii\rbac\PhpManager;
/**
* Exposes protected properties and methods to inspect from outside
*/
class ExposedPhpManager extends PhpManager
{
/**
* @var \yii\rbac\Item[]
*/
public $items = []; // itemName => item
/**
* @var array
*/
public $children = []; // itemName, childName => child
/**
* @var \yii\rbac\Assignment[]
*/
public $assignments = []; // userId, itemName => assignment
/**
* @var \yii\rbac\Rule[]
*/
public $rules = []; // ruleName => rule
public function load()
{
parent::load();
}
public function save()
{
parent::save();
}
}

173
tests/unit/framework/rbac/ManagerTestCase.php

@ -7,6 +7,9 @@ use yii\rbac\Permission;
use yii\rbac\Role;
use yiiunit\TestCase;
/**
* ManagerTestCase
*/
abstract class ManagerTestCase extends TestCase
{
/**
@ -14,7 +17,7 @@ abstract class ManagerTestCase extends TestCase
*/
protected $auth;
public function testCreateRoleAndPermission()
public function testCreateRole()
{
$role = $this->auth->createRole('admin');
$this->assertTrue($role instanceof Role);
@ -57,174 +60,6 @@ abstract class ManagerTestCase extends TestCase
$this->auth->addChild($user, $changeName);
$this->assertCount(1, $this->auth->getChildren($user->name));
}
/*
public function testRemove()
{
}
public function testUpdate()
{
}
public function testCreateItem()
{
$type = Item::TYPE_TASK;
$name = 'editUser';
$description = 'edit a user';
$ruleName = 'isAuthor';
$data = [1, 2, 3];
$item = $this->auth->createItem($name, $type, $description, $ruleName, $data);
$this->assertTrue($item instanceof Item);
$this->assertEquals($item->type, $type);
$this->assertEquals($item->name, $name);
$this->assertEquals($item->description, $description);
$this->assertEquals($item->ruleName, $ruleName);
$this->assertEquals($item->data, $data);
// test shortcut
$name2 = 'createUser';
$item2 = $this->auth->createRole($name2, $description, $ruleName, $data);
$this->assertEquals($item2->type, Item::TYPE_ROLE);
// test adding an item with the same name
$this->setExpectedException('\yii\base\Exception');
$this->auth->createItem($name, $type, $description, $ruleName, $data);
}
public function testGetItem()
{
$this->assertTrue($this->auth->getItem('readPost') instanceof Item);
$this->assertTrue($this->auth->getItem('reader') instanceof Item);
$this->assertNull($this->auth->getItem('unknown'));
}
public function testRemoveItem()
{
$this->assertTrue($this->auth->getItem('updatePost') instanceof Item);
$this->assertTrue($this->auth->removeItem('updatePost'));
$this->assertNull($this->auth->getItem('updatePost'));
$this->assertFalse($this->auth->removeItem('updatePost'));
}
public function testChangeItemName()
{
$item = $this->auth->getItem('readPost');
$this->assertTrue($item instanceof Item);
$this->assertTrue($this->auth->hasItemChild('reader', 'readPost'));
$item->name = 'readPost2';
$item->save();
$this->assertNull($this->auth->getItem('readPost'));
$this->assertEquals($this->auth->getItem('readPost2'), $item);
$this->assertFalse($this->auth->hasItemChild('reader', 'readPost'));
$this->assertTrue($this->auth->hasItemChild('reader', 'readPost2'));
}
public function testAddItemChild()
{
$this->auth->addItemChild('createPost', 'updatePost');
// test adding upper level item to lower one
$this->setExpectedException('\yii\base\Exception');
$this->auth->addItemChild('readPost', 'reader');
}
public function testAddItemChild2()
{
// test adding inexistent items
$this->setExpectedException('\yii\base\Exception');
$this->assertFalse($this->auth->addItemChild('createPost2', 'updatePost'));
}
public function testRemoveItemChild()
{
$this->assertTrue($this->auth->hasItemChild('reader', 'readPost'));
$this->assertTrue($this->auth->removeItemChild('reader', 'readPost'));
$this->assertFalse($this->auth->hasItemChild('reader', 'readPost'));
$this->assertFalse($this->auth->removeItemChild('reader', 'readPost'));
}
public function testGetItemChildren()
{
$this->assertEquals([], $this->auth->getItemChildren('readPost'));
$children = $this->auth->getItemChildren('author');
$this->assertEquals(3, count($children));
$this->assertTrue(reset($children) instanceof Item);
}
public function testAssign()
{
$auth = $this->auth->assign('new user', 'createPost', 'isAuthor', 'data');
$this->assertTrue($auth instanceof Assignment);
$this->assertEquals($auth->userId, 'new user');
$this->assertEquals($auth->itemName, 'createPost');
$this->assertEquals($auth->ruleName, 'isAuthor');
$this->assertEquals($auth->data, 'data');
$this->setExpectedException('\yii\base\Exception');
$this->auth->assign('new user', 'createPost2', 'rule', 'data');
}
public function testRevoke()
{
$this->assertTrue($this->auth->isAssigned('author B', 'author'));
$auth = $this->auth->getAssignment('author B', 'author');
$this->assertTrue($auth instanceof Assignment);
$this->assertTrue($this->auth->revoke('author B', 'author'));
$this->assertFalse($this->auth->isAssigned('author B', 'author'));
$this->assertFalse($this->auth->revoke('author B', 'author'));
}
public function testRevokeAll()
{
$this->assertTrue($this->auth->revokeAll('reader E'));
$this->assertFalse($this->auth->isAssigned('reader E', 'reader'));
}
public function testGetAssignments()
{
$this->auth->assign('author B', 'deletePost');
$auths = $this->auth->getAssignments('author B');
$this->assertEquals(2, count($auths));
$this->assertTrue(reset($auths) instanceof Assignment);
}
public function testGetItems()
{
$this->assertEquals(count($this->auth->getRoles()), 4);
$this->assertEquals(count($this->auth->getOperations()), 4);
$this->assertEquals(count($this->auth->getTasks()), 1);
$this->assertEquals(count($this->auth->getItems()), 9);
$this->assertEquals(count($this->auth->getItems('author B', null)), 1);
$this->assertEquals(count($this->auth->getItems('author C', null)), 0);
$this->assertEquals(count($this->auth->getItems('author B', Item::TYPE_ROLE)), 1);
$this->assertEquals(count($this->auth->getItems('author B', Item::TYPE_OPERATION)), 0);
}
public function testClearAll()
{
$this->auth->clearAll();
$this->assertEquals(count($this->auth->getRoles()), 0);
$this->assertEquals(count($this->auth->getOperations()), 0);
$this->assertEquals(count($this->auth->getTasks()), 0);
$this->assertEquals(count($this->auth->getItems()), 0);
$this->assertEquals(count($this->auth->getAssignments('author B')), 0);
}
public function testClearAssignments()
{
$this->auth->clearAssignments();
$this->assertEquals(count($this->auth->getAssignments('author B')), 0);
}
public function testDetectLoop()
{
$this->setExpectedException('\yii\base\Exception');
$this->auth->addItemChild('readPost', 'readPost');
}
*/
public function testGetRule()
{

58
tests/unit/framework/rbac/PhpManagerTest.php

@ -3,38 +3,74 @@
namespace yiiunit\framework\rbac;
use Yii;
use yii\rbac\PhpManager;
/**
* @group rbac
* @property \yii\rbac\PhpManager $auth
* @property ExposedPhpManager $auth
*/
class PhpManagerTest extends ManagerTestCase
{
protected function getItemFile()
{
return Yii::$app->getRuntimePath() . '/rbac-items.php';
}
protected function getAssignmentFile()
{
return Yii::$app->getRuntimePath() . '/rbac-assignments.php';
}
protected function getRuleFile()
{
return Yii::$app->getRuntimePath() . '/rbac-rules.php';
}
protected function removeDataFiles()
{
@unlink($this->getItemFile());
@unlink($this->getAssignmentFile());
@unlink($this->getRuleFile());
}
protected function createManager()
{
return new ExposedPhpManager([
'itemFile' => $this->getItemFile(),
'assignmentFile' => $this->getAssignmentFile(),
'ruleFile' => $this->getRuleFile(),
]);
}
protected function setUp()
{
parent::setUp();
$this->mockApplication();
$authFile = Yii::$app->getRuntimePath() . '/rbac.php';
@unlink($authFile);
$this->auth = new PhpManager();
$this->auth->authFile = $authFile;
$this->auth->init();
$this->removeDataFiles();
$this->auth = $this->createManager();
}
protected function tearDown()
{
$this->removeDataFiles();
parent::tearDown();
@unlink($this->auth->authFile);
}
public function testSaveLoad()
{
$this->prepareData();
$items = $this->auth->items;
$children = $this->auth->children;
$assignments = $this->auth->assignments;
$rules = $this->auth->rules;
$this->auth->save();
$this->auth->removeAll();
$this->auth = $this->createManager();
$this->auth->load();
// TODO : Check if loaded and saved data are the same.
}
$this->assertEquals($items, $this->auth->items);
$this->assertEquals($children, $this->auth->children);
$this->assertEquals($assignments, $this->auth->assignments);
$this->assertEquals($rules, $this->auth->rules);
}
}
Loading…
Cancel
Save