123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- <?php
-
- namespace Illuminate\Database\Eloquent;
-
- use ArrayAccess;
- use Faker\Generator as Faker;
- use Symfony\Component\Finder\Finder;
-
- class Factory implements ArrayAccess
- {
- /**
- * The model definitions in the container.
- *
- * @var array
- */
- protected $definitions = [];
-
- /**
- * The registered model states.
- *
- * @var array
- */
- protected $states = [];
-
- /**
- * The registered after making callbacks.
- *
- * @var array
- */
- protected $afterMaking = [];
-
- /**
- * The registered after creating callbacks.
- *
- * @var array
- */
- protected $afterCreating = [];
-
- /**
- * The Faker instance for the builder.
- *
- * @var \Faker\Generator
- */
- protected $faker;
-
- /**
- * Create a new factory instance.
- *
- * @param \Faker\Generator $faker
- * @return void
- */
- public function __construct(Faker $faker)
- {
- $this->faker = $faker;
- }
-
- /**
- * Create a new factory container.
- *
- * @param \Faker\Generator $faker
- * @param string|null $pathToFactories
- * @return static
- */
- public static function construct(Faker $faker, $pathToFactories = null)
- {
- $pathToFactories = $pathToFactories ?: database_path('factories');
-
- return (new static($faker))->load($pathToFactories);
- }
-
- /**
- * Define a class with a given short-name.
- *
- * @param string $class
- * @param string $name
- * @param callable $attributes
- * @return $this
- */
- public function defineAs($class, $name, callable $attributes)
- {
- return $this->define($class, $attributes, $name);
- }
-
- /**
- * Define a class with a given set of attributes.
- *
- * @param string $class
- * @param callable $attributes
- * @param string $name
- * @return $this
- */
- public function define($class, callable $attributes, $name = 'default')
- {
- $this->definitions[$class][$name] = $attributes;
-
- return $this;
- }
-
- /**
- * Define a state with a given set of attributes.
- *
- * @param string $class
- * @param string $state
- * @param callable|array $attributes
- * @return $this
- */
- public function state($class, $state, $attributes)
- {
- $this->states[$class][$state] = $attributes;
-
- return $this;
- }
-
- /**
- * Define a callback to run after making a model.
- *
- * @param string $class
- * @param callable $callback
- * @param string $name
- * @return $this
- */
- public function afterMaking($class, callable $callback, $name = 'default')
- {
- $this->afterMaking[$class][$name][] = $callback;
-
- return $this;
- }
-
- /**
- * Define a callback to run after making a model with given state.
- *
- * @param string $class
- * @param string $state
- * @param callable $callback
- * @return $this
- */
- public function afterMakingState($class, $state, callable $callback)
- {
- return $this->afterMaking($class, $callback, $state);
- }
-
- /**
- * Define a callback to run after creating a model.
- *
- * @param string $class
- * @param callable $callback
- * @param string $name
- * @return $this
- */
- public function afterCreating($class, callable $callback, $name = 'default')
- {
- $this->afterCreating[$class][$name][] = $callback;
-
- return $this;
- }
-
- /**
- * Define a callback to run after creating a model with given state.
- *
- * @param string $class
- * @param string $state
- * @param callable $callback
- * @return $this
- */
- public function afterCreatingState($class, $state, callable $callback)
- {
- return $this->afterCreating($class, $callback, $state);
- }
-
- /**
- * Create an instance of the given model and persist it to the database.
- *
- * @param string $class
- * @param array $attributes
- * @return mixed
- */
- public function create($class, array $attributes = [])
- {
- return $this->of($class)->create($attributes);
- }
-
- /**
- * Create an instance of the given model and type and persist it to the database.
- *
- * @param string $class
- * @param string $name
- * @param array $attributes
- * @return mixed
- */
- public function createAs($class, $name, array $attributes = [])
- {
- return $this->of($class, $name)->create($attributes);
- }
-
- /**
- * Create an instance of the given model.
- *
- * @param string $class
- * @param array $attributes
- * @return mixed
- */
- public function make($class, array $attributes = [])
- {
- return $this->of($class)->make($attributes);
- }
-
- /**
- * Create an instance of the given model and type.
- *
- * @param string $class
- * @param string $name
- * @param array $attributes
- * @return mixed
- */
- public function makeAs($class, $name, array $attributes = [])
- {
- return $this->of($class, $name)->make($attributes);
- }
-
- /**
- * Get the raw attribute array for a given named model.
- *
- * @param string $class
- * @param string $name
- * @param array $attributes
- * @return array
- */
- public function rawOf($class, $name, array $attributes = [])
- {
- return $this->raw($class, $attributes, $name);
- }
-
- /**
- * Get the raw attribute array for a given model.
- *
- * @param string $class
- * @param array $attributes
- * @param string $name
- * @return array
- */
- public function raw($class, array $attributes = [], $name = 'default')
- {
- return array_merge(
- call_user_func($this->definitions[$class][$name], $this->faker), $attributes
- );
- }
-
- /**
- * Create a builder for the given model.
- *
- * @param string $class
- * @param string $name
- * @return \Illuminate\Database\Eloquent\FactoryBuilder
- */
- public function of($class, $name = 'default')
- {
- return new FactoryBuilder(
- $class, $name, $this->definitions, $this->states,
- $this->afterMaking, $this->afterCreating, $this->faker
- );
- }
-
- /**
- * Load factories from path.
- *
- * @param string $path
- * @return $this
- */
- public function load($path)
- {
- $factory = $this;
-
- if (is_dir($path)) {
- foreach (Finder::create()->files()->name('*.php')->in($path) as $file) {
- require $file->getRealPath();
- }
- }
-
- return $factory;
- }
-
- /**
- * Determine if the given offset exists.
- *
- * @param string $offset
- * @return bool
- */
- public function offsetExists($offset)
- {
- return isset($this->definitions[$offset]);
- }
-
- /**
- * Get the value of the given offset.
- *
- * @param string $offset
- * @return mixed
- */
- public function offsetGet($offset)
- {
- return $this->make($offset);
- }
-
- /**
- * Set the given offset to the given value.
- *
- * @param string $offset
- * @param callable $value
- * @return void
- */
- public function offsetSet($offset, $value)
- {
- return $this->define($offset, $value);
- }
-
- /**
- * Unset the value at the given offset.
- *
- * @param string $offset
- * @return void
- */
- public function offsetUnset($offset)
- {
- unset($this->definitions[$offset]);
- }
- }
|