|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- <?php
-
-
- namespace yii\test;
-
- use Yii;
- use yii\base\InvalidConfigException;
-
-
- trait FixtureTrait
- {
-
-
- private $_fixtures;
-
-
-
-
- public function fixtures()
- {
- return [];
- }
-
-
-
- public function globalFixtures()
- {
- return [];
- }
-
-
-
- public function loadFixtures($fixtures = null)
- {
- if ($fixtures === null) {
- $fixtures = $this->getFixtures();
- }
-
-
- foreach ($fixtures as $fixture) {
- $fixture->beforeLoad();
- }
- foreach ($fixtures as $fixture) {
- $fixture->load();
- }
- foreach (array_reverse($fixtures) as $fixture) {
- $fixture->afterLoad();
- }
- }
-
-
-
- public function unloadFixtures($fixtures = null)
- {
- if ($fixtures === null) {
- $fixtures = $this->getFixtures();
- }
-
-
- foreach ($fixtures as $fixture) {
- $fixture->beforeUnload();
- }
- $fixtures = array_reverse($fixtures);
- foreach ($fixtures as $fixture) {
- $fixture->unload();
- }
- foreach ($fixtures as $fixture) {
- $fixture->afterUnload();
- }
- }
-
-
-
- public function getFixtures()
- {
- if ($this->_fixtures === null) {
- $this->_fixtures = $this->createFixtures(array_merge($this->globalFixtures(), $this->fixtures()));
- }
-
- return $this->_fixtures;
- }
-
-
-
- public function getFixture($name)
- {
- if ($this->_fixtures === null) {
- $this->_fixtures = $this->createFixtures(array_merge($this->globalFixtures(), $this->fixtures()));
- }
- $name = ltrim($name, '\\');
-
- return isset($this->_fixtures[$name]) ? $this->_fixtures[$name] : null;
- }
-
-
-
- protected function createFixtures(array $fixtures)
- {
-
- $config = [];
- $aliases = [];
- foreach ($fixtures as $name => $fixture) {
- if (!is_array($fixture)) {
- $class = ltrim($fixture, '\\');
- $fixtures[$name] = ['class' => $class];
- $aliases[$class] = is_int($name) ? $class : $name;
- } elseif (isset($fixture['class'])) {
- $class = ltrim($fixture['class'], '\\');
- $config[$class] = $fixture;
- $aliases[$class] = $name;
- } else {
- throw new InvalidConfigException("You must specify 'class' for the fixture '$name'.");
- }
- }
-
-
- $instances = [];
- $stack = array_reverse($fixtures);
- while (($fixture = array_pop($stack)) !== null) {
- if ($fixture instanceof Fixture) {
- $class = get_class($fixture);
- $name = isset($aliases[$class]) ? $aliases[$class] : $class;
- unset($instances[$name]);
- $instances[$name] = $fixture;
- } else {
- $class = ltrim($fixture['class'], '\\');
- $name = isset($aliases[$class]) ? $aliases[$class] : $class;
- if (!isset($instances[$name])) {
- $instances[$name] = false;
- $stack[] = $fixture = Yii::createObject($fixture);
- foreach ($fixture->depends as $dep) {
-
- $stack[] = isset($config[$dep]) ? $config[$dep] : ['class' => $dep];
- }
- } elseif ($instances[$name] === false) {
- throw new InvalidConfigException("A circular dependency is detected for fixture '$class'.");
- }
- }
- }
-
- return $instances;
- }
- }
|