Skip to content

Commit 4ca52b7

Browse files
author
Toby Allen
committed
initial commit
0 parents  commit 4ca52b7

File tree

4 files changed

+165
-0
lines changed

4 files changed

+165
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/vendor/*
2+
.idea/
3+
composer.lock

composer.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "tobya/fixsqldate",
3+
"description": "Check and set MSSQL universal date format used by Laravel to 'Ymd' to ensure universal compatibility",
4+
"type": "laravel-package",
5+
"license": "MIT",
6+
"autoload": {
7+
"psr-4": {
8+
"Tobya\\Fixsqldate\\": "src/"
9+
}
10+
},
11+
"authors": [
12+
{
13+
"name": "Toby Allen",
14+
"email": "toby@toflidium.com"
15+
}
16+
],
17+
"require": {
18+
"laravel/laravel": "^8.0|^9.0|^10.0"
19+
},
20+
"extra": {
21+
"laravel": {
22+
"providers": [
23+
"Tobya\\Fixsqldate\\Providers\\MSSQLUniversalDateProvider"
24+
]
25+
26+
}
27+
}
28+
}

src/Console/CheckSQLGrammerDate.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Tobya\Fixsqldate\Providers;
4+
5+
6+
7+
use Tobya\FixSQLDate\Console\CheckSQLGrammerDate;
8+
9+
class MSSQLUniversalDateProvider extends \Illuminate\Support\ServiceProvider
10+
{
11+
/**
12+
* Register services.
13+
*
14+
* @return void
15+
*/
16+
public function register()
17+
{
18+
19+
}
20+
21+
/**
22+
* Bootstrap services.
23+
*
24+
* @return void
25+
*/
26+
public function boot()
27+
{
28+
$this->commands([
29+
CheckSQLGrammerDate::class
30+
]);
31+
}
32+
}

0 commit comments

Comments
 (0)