Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
webbingbrasil/core-module / Commands / RebuildCaches.php
Size: Mime:
<?php
/**
 * Created by PhpStorm.
 * User: danilo
 * Date: 02/03/16
 * Time: 14:28
 */

namespace Modules\Core\Commands;

use Illuminate\Console\Command;
use Modules\Core\Database\Behaviours\CountCache\CountCache;
use Modules\Core\Database\Behaviours\SumCache\SumCache;

class RebuildCaches extends Command
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'webbing:database-rebuild';
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'webbing:database-rebuild {--class= : Optional classes to update} {--dir= : Directory in which to look for classes}';
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Rebuild the caches for one or more Eloquent models';

    /**
     * Create a new command instance.
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $class = $this->option('class');
        $classes = [$class];

        if (!$class) {
            $directory = $this->option('dir') ?: app_path();
            $classes = (new FindCacheableClasses($directory))->getAllCacheableClasses();
        }

        foreach ($classes as $className) {
            $this->rebuild($className);
        }
    }

    /**
     * Rebuilds the caches for the given class.
     *
     * @param string $className
     */
    private function rebuild($className)
    {
        $instance = new $className;
        if (method_exists($instance, 'countCaches')) {
            $this->info("Rebuilding [$className] count caches");
            $countCache = new CountCache($instance);
            $countCache->rebuild();
        }
        if (method_exists($instance, 'sumCaches')) {
            $this->info("Rebuilding [$className] sum caches");
            $sumCache = new SumCache($instance);
            $sumCache->rebuild();
        }
    }
}