1. Go to this page and download the library: Download tobento/app-seeding library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tobento / app-seeding example snippets
use Tobento\App\AppFactory;
use Tobento\Service\Seeder\SeedInterface;
use Tobento\App\Seeding\SeedersInterface;
// Create the app
$app = (new AppFactory())->createApp();
// Add directories:
$app->dirs()
->dir(realpath(__DIR__.'/../'), 'root')
->dir(realpath(__DIR__.'/../app/'), 'app')
->dir($app->dir('app').'config', 'config', group: 'config')
->dir($app->dir('root').'vendor', 'vendor');
// Adding boots
$app->boot(\Tobento\App\Seeding\Boot\Seeding::class);
$app->booting();
// Available Interfaces:
$seed = $app->get(SeedInterface::class);
$seeders = $app->get(SeedersInterface::class);
// Run the app
$app->run();
use Tobento\App\AppFactory;
use Tobento\Service\Seeder\SeedInterface;
use Tobento\Service\Seeder\Resource;
// Create the app
$app = (new AppFactory())->createApp();
// Add directories:
$app->dirs()
->dir(realpath(__DIR__.'/../'), 'root')
->dir(realpath(__DIR__.'/../app/'), 'app')
->dir($app->dir('app').'config', 'config', group: 'config')
->dir($app->dir('root').'vendor', 'vendor');
// Adding boots
$app->boot(\Tobento\App\Seeding\Boot\Seeding::class);
$app->booting();
// Add resources:
$app->on(SeedInterface::class, function(SeedInterface $seed) {
$seed->resources()->add(new Resource('countries', 'en', [
'Usa', 'Switzerland', 'Germany',
]));
});
$seed = $app->get(SeedInterface::class);
var_dump($seed->country());
// string(7) "Germany"
// Run the app
$app->run();
use Tobento\Service\Seeder\SeedInterface;
use Tobento\Service\Seeder\Resource;
class ServiceUsingSeed
{
public function __construct(
protected SeedInterface $seed,
) {
$seed->resources()->add(new Resource('countries', 'en', [
'Usa', 'Switzerland', 'Germany',
]));
}
}
use Tobento\App\Seeding\AbstractFactory;
class UserFactory extends AbstractFactory
{
public function definition(): array
{
return [
'firstname' => $this->seed->firstname(),
'role' => 'admin',
];
}
}
use Tobento\App\Seeding\AbstractFactory;
class UserFactory extends AbstractFactory
{
protected function createEntity(array $definition): object
{
return new User(
firstname: $definition['firstname'],
);
}
}
use Tobento\App\Seeding\AbstractFactory;
class UserFactory extends AbstractFactory
{
protected function storeEntity(array $definition): object
{
return $this->getService(UserRepositoryInterface::class)
->create($definition);
}
}
$factory = UserFactory::new();
// you may overwrite the default definition:
$factory = UserFactory::new(['role' => 'editor']);
// make 10 entities:
$users = $factory->times(10)->make();
// make one:
$user = $factory->makeOne();
use Tobento\App\Seeding\AbstractFactory;
use Tobento\Service\Seeder\SeedInterface;
class UserFactory extends AbstractFactory
{
public function withEmail(string $email): static
{
return $this->modify(fn(SeedInterface $seed, array $definition) => [
'email' => $email
]);
}
}
$users = $factory
->withEmail('[email protected]')
->times(10)
->make();
use Tobento\Service\Seeder\SeedInterface;
$users = $factory
->modifyEntity(static function (SeedInterface $seed, object $user) {
return $user->markAsDeleted();
})
->times(10)
->make();
use Tobento\App\Seeding\AbstractFactory;
use Tobento\Service\Seeder\SeedInterface;
class UserFactory extends AbstractFactory
{
public function deleted(string $email): static
{
return $this->modifyEntity(static function (SeedInterface $seed, object $user) {
return $user->markAsDeleted();
});
}
}
$users = $factory
->deleted()
->times(10)
->make();
use Tobento\App\Seeding\SeederInterface;
class UserSeeder implements SeederInterface
{
public function run(): \Generator
{
foreach (UserFactory::new()->times(100)->create() as $user) {
yield $user;
}
}
}
use Tobento\App\Seeding\SeederInterface;
use Tobento\App\User\UserRepositoryInterface;
use Tobento\Service\Repository\Storage\StorageRepository;
use Tobento\Service\Iterable\ItemFactoryIterator;
class UserSeeder implements SeederInterface
{
public function __construct(
protected UserRepositoryInterface $userRepository,
) {
if (! $userRepository instanceof StorageRepository) {
throw new \InvalidArgumentException('Not supported ...');
}
}
public function run(): \Generator
{
yield from $this->userRepository
->query()
->chunk(length: 10000)
->insertItems(new ItemFactoryIterator(
factory: function (): array {
return UserFactory::new()->definition();
},
create: 1000000,
));
}
}
use Tobento\App\Seeding\SeedersInterface;
// ...
$app->on(
SeedersInterface::class,
static function (SeedersInterface $seeders): void {
$seeders->addSeeder('users', UserSeeder::class);
}
);
php ap seed
php ap seed --name=users
php ap seed -v
php ap seed:list
use Tobento\App\Seeding\User\UserFactory;
$user = UserFactory::new()
->withEmail('[email protected]')
->withSmartphone('22334455')
->withUsername('Username')
->withPassword('123456')
->withRoleKey('admin') // 'guest' if role does not exist.
->withAddress(['firstname' => 'Firstname'])
->makeOne();
use Tobento\App\Seeding\User\UserStorageSeeder;
$app->on(
SeedersInterface::class,
static function (SeedersInterface $seeders): void {
$seeders->addSeeder('users', UserStorageSeeder::class);
}
);
use Tobento\App\Seeding\Repository\RepositoryFactory;
use Tobento\Service\Repository\RepositoryInterface;
use Tobento\Service\Seeder\SeedInterface;
use Tobento\Service\Seeder\Lorem;
$factory = RepositoryFactory::new(
repository: MyProductRepository::class, // string|RepositoryInterface
definition: function (SeedInterface $seed): array {
return [
'sku' => Lorem::word(number: 1),
'desc' => Lorem::sentence(number: 2),
];
}
);
// using the factory:
$products = $factory->times(10)->make();
use Tobento\App\Seeding\Repository\RepositoryFactory;
$factory = RepositoryFactory::new(repository: MyProductRepository::class);
// using the factory:
$products = $factory->times(10)->make();
use Tobento\App\Seeding\Repository\AbstractFactory;
class ProductFactory extends AbstractFactory
{
public const REPOSITORY = MyProductRepository::class;
}
// using the factory:
$products = ProductFactory::new()->times(10)->make();
use Tobento\App\Seeding\Repository\AbstractFactory;
class ProductFactory extends AbstractFactory
{
public function definition(): array
{
// you may call the parent if you have
// repositories with columns so it will
// automatically create the definition
// based on the columns.
$definition = parent::definition();
// and just overwrite if needed:
$definition['name'] = $this->seed->firstname();
return $definition;
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.