<?php

namespace App\Modules\Home\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use ZipArchive;
class InstallController extends Controller
{

    public static function installAction()
    {   
        $dir = dirname(__FILE__);
        $exp = explode('\\', $dir);        
        $namefile = strtolower($exp[7]);
        $dir = app_path().'/Modules/'.$namefile.'/views';
        $destination = base_path().'/resources/views/';
        if (!file_exists($destination.$namefile.'.zip')) {
            // obtener la ruta real de nuestra carpeta
            $rootPath = realpath($dir);

            // inicializar el archivo  zip con sus respectivos metodos
            $zip = new ZipArchive();
            $zip->open($destination.$namefile.'.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);

            // Funciones para crear un directorio recursivo de php , funciones  de php 
            $files = new \RecursiveIteratorIterator(
                new \RecursiveDirectoryIterator($rootPath),
                \RecursiveIteratorIterator::LEAVES_ONLY
            );

            foreach ($files as $name => $file)
            {
                // Skip directories (they would be added automatically)
                if (!$file->isDir())
                {
                    // Get real and relative path for current file
                    $filePath = $file->getRealPath();
                    $relativePath = $namefile.'/'.substr($filePath, strlen($rootPath) + 1);
                    $zip->addFile($filePath, $relativePath);
                }
            }

            // Zip archive will be created only after closing object
            $zip->close();
        }
        
    }

}
