<commit_before><?php

namespace Spatie\Seeders;

use Faker\Factory;
use DB;
use File;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Seeder;
use Spatie\MediaLibrary\MediaLibraryModel\MediaLibraryModelInterface;
use Spatie\MediaLibrary\Models\Media;
use Spatie\SuperSeeder\Parsers\YamlParser;
use Spatie\SuperSeeder\SuperSeeder;

class DatabaseSeeder extends Seeder
{
    /**
     * @var string
     */
    protected $tempImageDir;

    /**
     * @var \Faker\Generator
     */
    protected $faker;

    public function __construct()
    {
        $this->tempImageDir = storage_path() . '/tempSeeder';
        $this->faker        = Factory::create();
    }

    /**
     * Run the seeds
     */
    public function run()
    {
        DB::connection()->disableQueryLog();

        Model::unguard();

        $this->truncate((new Media)->getTable());

        $this->createTemporaryImageDirectory();

        if (app()->environment() == 'local') {
            $this->clearMediaDirectory();
        }
    }

    /**
     * @param  \Spatie\SuperSeeder\Factory $factory
     * @param  string $filename
     */
    protected function superSeeder($factory, $filename)
    {
        $superSeeder = new SuperSeeder($factory);

        $file = base_path("database/seeds/data/$filename.yml");

        return $superSeeder->seedFromFile($file, new YamlParser);
    }

    /**
     * @param  string $tables,...
     */
    protected function truncate(...$tables)
    {
        DB::statement("SET FOREIGN_KEY_CHECKS=0");

        foreach($tables as $table) {
            DB::table($table)->truncate();
        }
        
        DB::statement("SET FOREIGN_KEY_CHECKS=1");
    }

    protected function createTemporaryImageDirectory()
    {
        if (File::isDirectory($this->tempImageDir)) {
            File::cleanDirectory($this->tempImageDir);
        } else {
            File::makeDirectory($this->tempImageDir, 493, true);
        }
    }

    protected function clearMediaDirectory()
    {
        File::cleanDirectory(public_path().'/media');
    }

    /**
     * @param  \Spatie\MediaLibrary\MediaLibraryModel\MediaLibraryModelInterface $model
     * @param  int $minAmount
     * @param  int $maxAmount
     */
    protected function addImages(MediaLibraryModelInterface $model, $minAmount = 1, $maxAmount = 3)
    {
        foreach (range(1, $this->faker->numberBetween($minAmount, $maxAmount)) as $index) {
            $model->addMedia($this->faker->image($this->tempImageDir, 640, 480), 'images', false, false);
        }
    }
}<commit_msg>add method to truncate all tables<commit_after><?php

namespace Spatie\Seeders;

use Faker\Factory;
use DB;
use File;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Seeder;
use Spatie\MediaLibrary\MediaLibraryModel\MediaLibraryModelInterface;
use Spatie\MediaLibrary\Models\Media;
use Spatie\SuperSeeder\Parsers\YamlParser;
use Spatie\SuperSeeder\SuperSeeder;

class DatabaseSeeder extends Seeder
{
    /**
     * @var string
     */
    protected $tempImageDir;

    /**
     * @var \Faker\Generator
     */
    protected $faker;

    public function __construct()
    {
        $this->tempImageDir = storage_path() . '/tempSeeder';
        $this->faker        = Factory::create();
    }

    /**
     * Run the seeds
     */
    public function run()
    {
        DB::connection()->disableQueryLog();

        Model::unguard();

        $this->truncate((new Media)->getTable());

        $this->createTemporaryImageDirectory();

        if (app()->environment() == 'local') {
            $this->clearMediaDirectory();
        }
    }

    /**
     * @param  \Spatie\SuperSeeder\Factory $factory
     * @param  string $filename
     */
    protected function superSeeder($factory, $filename)
    {
        $superSeeder = new SuperSeeder($factory);

        $file = base_path("database/seeds/data/$filename.yml");

        return $superSeeder->seedFromFile($file, new YamlParser);
    }

    /**
     * Truncate the given tables
     *
     * @param  string $tables,...
     */
    protected function truncate(...$tables)
    {
        DB::statement("SET FOREIGN_KEY_CHECKS=0");

        foreach($tables as $table) {
            DB::table($table)->truncate();
        }
        
        DB::statement("SET FOREIGN_KEY_CHECKS=1");
    }

    /**
     * Truncate all tables
     */
    protected function truncateAllTables()
    {
        $this->truncate($this->getAllTableNames());
    }

    /**
     * Get the names of all tables
     *
     * @return array
     */
    protected function getAllTableNames()
    {
        $query = sprintf('SELECT TABLE_NAME as name FROM information_schema.tables WHERE table_schema="%s"', DB::connection()->getDatabaseName());

        $tableNames = array_map(function($rawResult) {
            return $rawResult->name;
        },DB::select($query));

        return $tableNames;
    }

    protected function createTemporaryImageDirectory()
    {
        if (File::isDirectory($this->tempImageDir)) {
            File::cleanDirectory($this->tempImageDir);
        } else {
            File::makeDirectory($this->tempImageDir, 493, true);
        }
    }

    protected function clearMediaDirectory()
    {
        File::cleanDirectory(public_path().'/media');
    }

    /**
     * Add images to the given model.
     *
     * @param  \Spatie\MediaLibrary\MediaLibraryModel\MediaLibraryModelInterface $model
     * @param  int $minAmount
     * @param  int $maxAmount
     */
    protected function addImages(MediaLibraryModelInterface $model, $minAmount = 1, $maxAmount = 3)
    {
        foreach (range(1, $this->faker->numberBetween($minAmount, $maxAmount)) as $index) {
            $model->addMedia($this->faker->image($this->tempImageDir, 640, 480), 'images', false, false);
        }
    }
}
{'fraction_non_alphanumeric': 0.1379903647625602, 'fraction_numerical': 0.004817618719889883, 'mean_word_length': 2.5187651331719128, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '623992', 'n_tokens_mistral': 1765, 'n_tokens_neox': 1705, 'n_words': 314}