|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tobya\FixSQLDate\Console; |
| 4 | + |
| 5 | + |
| 6 | +use Illuminate\Console\Command; |
| 7 | + |
| 8 | +/** |
| 9 | + * There is an international date format Y-m-d that is supposed to be universal, however the MSSQL implementation is flawed |
| 10 | + * and is not universal and incorrect interprets it as Y-d-m which is beyond idiotic. |
| 11 | + * Laravel uses Y-m-d as their international format, which can lead to errors depending on SQL SERVER Settings. |
| 12 | + * This command checks the vendor directory for the file and updates it if required. |
| 13 | + * |
| 14 | + * It is suggested that you add the following to your project composer.json file so this command is automatically run |
| 15 | + * on install and update. |
| 16 | + * |
| 17 | + * |
| 18 | +"scripts": { |
| 19 | + "post-update-cmd": [ |
| 20 | + "@php artisan mssql:check-universal-date --update" |
| 21 | + ], |
| 22 | + "post-install-cmd": [ |
| 23 | + "@php artisan mssql:check-universal-date --update" |
| 24 | + ] |
| 25 | + } |
| 26 | + * |
| 27 | + */ |
| 28 | + |
| 29 | + /** |
| 30 | + * Class CheckSQLGrammerDate |
| 31 | + * @package App\Console\Commands |
| 32 | + */ |
| 33 | +class CheckSQLGrammerDate extends Command |
| 34 | +{ |
| 35 | + /** |
| 36 | + * The name and signature of the console command. |
| 37 | + * |
| 38 | + * @var string |
| 39 | + */ |
| 40 | + protected $signature = 'mssql:check-universal-date {--update : Overwrite files in Vendor directory.}'; |
| 41 | + |
| 42 | + /** |
| 43 | + * The console command description. |
| 44 | + * |
| 45 | + * @var string |
| 46 | + */ |
| 47 | + protected $description = 'Checks if the illuminate files in vendor directory are using incorrect date format. '; |
| 48 | + |
| 49 | + /** |
| 50 | + * Create a new command instance. |
| 51 | + * |
| 52 | + * @return void |
| 53 | + */ |
| 54 | + public function __construct() |
| 55 | + { |
| 56 | + parent::__construct(); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Execute the console command. |
| 61 | + * |
| 62 | + * @return int |
| 63 | + */ |
| 64 | + public function handle() |
| 65 | + { |
| 66 | + $filetocheck = base_path('vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\SqlServerGrammar.php'); |
| 67 | + if (file_exists($filetocheck)){ |
| 68 | + $file_txt = Str(file_get_contents($filetocheck)); |
| 69 | + $datestr = 'return \'Y-m-d H:i:s.v\';'; |
| 70 | + if ($file_txt->contains($datestr)){ |
| 71 | + if ($this->option('update')){ |
| 72 | + $UpdatedFile_txt = $file_txt->replace($datestr, 'return \'Ymd H:i:s.v\';'); |
| 73 | + file_put_contents($filetocheck,$UpdatedFile_txt); |
| 74 | + |
| 75 | + $this->comment(" |
| 76 | +********************************** |
| 77 | +Incorrect Date Format value found |
| 78 | +********************************** |
| 79 | +File on disk: $filetocheck"); |
| 80 | + $this->info(' |
| 81 | +------------------ |
| 82 | +Updated |
| 83 | +------------------'); |
| 84 | + return 0; |
| 85 | + } else { |
| 86 | + $this->warn( $this->NoUpdateMessage($filetocheck)); |
| 87 | + $this->warn('Not Updated'); |
| 88 | + return 0; |
| 89 | + } |
| 90 | + |
| 91 | + } |
| 92 | + } |
| 93 | + $this->info( "Date Format Appears to be ok."); |
| 94 | + return 0; |
| 95 | + } |
| 96 | + |
| 97 | + public function NoUpdateMessage($filetocheck){ |
| 98 | + return " |
| 99 | +$filetocheck |
| 100 | + has been overwritten with the wrong date format. mssql:CheckSQLDate --update to update the file. "; |
| 101 | + } |
| 102 | +} |
0 commit comments