Advertisement
Guest User

laravel update app !!!

a guest
Aug 24th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.96 KB | None | 0 0
  1. <?php
  2. // ...
  3. Route::get('/update', function(){
  4.     // define some vars
  5.     $base = __DIR__.'/../';
  6.     $temp_path = $base . 'storage/tmp';
  7.     $controller_path = $base . 'app/Http/Controllers';
  8.     $routes_path = $base . 'routes/';
  9.     $routes_file = $routes_path . 'web.php';
  10.     $home_controller = 'HomeController.php'; // update existing file
  11.     $test_controller = 'TestController.php'; // will add new file
  12.  
  13.     // make tmp dir to download
  14.     if ( ! file_exists($temp_path)) {
  15.         mkdir($temp_path);
  16.     }
  17.  
  18.     // keep backup ?!!
  19.     $files_to_backup = [
  20.         $controller_path . '/' . $home_controller,
  21.         // some how list all files
  22.     ];
  23.     foreach ($files_to_backup as $file) {
  24.         if( file_exists($file) )
  25.             rename($file, $file.'.bk'); // move some where else??!!
  26.     }
  27.  
  28.     // download update
  29.     $fp = fopen ($temp_path . '/update.tmp', 'w+');
  30.     $url = 'http://localhost/HomeController.php.txt';
  31.  
  32.     $ch = curl_init($url);
  33.     curl_setopt($ch, CURLOPT_TIMEOUT, 50);
  34.     curl_setopt($ch, CURLOPT_FILE, $fp);
  35.     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  36.     curl_exec($ch);
  37.     curl_close($ch);
  38.     fclose($fp);
  39.  
  40.     // override existing
  41.     rename($temp_path.'/update.tmp', $controller_path . '/' . $home_controller);
  42.  
  43.     // add new file
  44.     // assume that you have downloaded this file also
  45.     // :P too lazy to make new file just for test
  46.     $content = '<?php
  47.        namespace App\Http\Controllers;
  48.  
  49.        class TestController extends Controller
  50.        {
  51.            public function index()
  52.            {
  53.                return view("welcome");
  54.            }
  55.        }';
  56.  
  57.     file_put_contents($controller_path . '/' . $test_controller, $content);
  58.  
  59.     // update routes
  60.     $data = file_get_contents($routes_file);
  61.     $data .= "
  62.        Route::get('/test', 'TestController@index');
  63.    "; // all the new routes :p
  64.  
  65.     file_put_contents($routes_file, $data);
  66.  
  67.     echo 'all done';
  68. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement