forked from wouterj/WouterJEloquentBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSeeder.php
84 lines (69 loc) · 1.93 KB
/
Seeder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
/*
* This file is part of the WouterJEloquentBundle package.
*
* (c) 2014 Wouter de Jong
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace WouterJ\EloquentBundle;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Illuminate\Database\Seeder as BaseSeeder;
use Illuminate\Database\ConnectionInterface;
/**
* @author Wouter J <wouter@wouterj.nl>
*/
class Seeder extends BaseSeeder
{
/** @var null|ContainerInterface */
protected $container;
protected $seededClasses = array();
/** @var ConnectionInterface */
protected $connection;
public function call($class)
{
$seeder = $this->resolve($class);
$seeder->setConnection($this->connection);
$seeder->run();
}
/**
* {@inheritDoc}
*
* @return Seeder
*/
public function resolve($class)
{
if ($this->getContainer()->has($class)) {
$seeder = $this->getContainer()->get($class);
} else {
$seeder = new $class;
}
if (!$seeder instanceof self) {
throw new \LogicException(sprintf('The seeder "%s" does not extend WouterJ\EloquentBundle\Seeder', get_class($seeder)));
}
$seeder->setSfContainer($this->getContainer());
$this->addSeededClass($seeder);
return $seeder;
}
public function setSfContainer(ContainerInterface $container)
{
$this->container = $container;
}
protected function getContainer()
{
return $this->container;
}
public function getSeedClasses()
{
return $this->seededClasses;
}
protected function addSeededClass($object)
{
$this->seededClasses[] = is_string($object) ? $object : get_class($object);
}
public function setConnection(ConnectionInterface $connection)
{
$this->connection = $connection;
}
}