Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Toby Allen committed Dec 28, 2023
0 parents commit 4ca52b7
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/vendor/*
.idea/
composer.lock
28 changes: 28 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "tobya/fixsqldate",
"description": "Check and set MSSQL universal date format used by Laravel to 'Ymd' to ensure universal compatibility",
"type": "laravel-package",
"license": "MIT",
"autoload": {
"psr-4": {
"Tobya\\Fixsqldate\\": "src/"
}
},
"authors": [
{
"name": "Toby Allen",
"email": "[email protected]"
}
],
"require": {
"laravel/laravel": "^8.0|^9.0|^10.0"
},
"extra": {
"laravel": {
"providers": [
"Tobya\\Fixsqldate\\Providers\\MSSQLUniversalDateProvider"
]

}
}
}
102 changes: 102 additions & 0 deletions src/Console/CheckSQLGrammerDate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace Tobya\FixSQLDate\Console;


use Illuminate\Console\Command;

/**
* There is an international date format Y-m-d that is supposed to be universal, however the MSSQL implementation is flawed
* and is not universal and incorrect interprets it as Y-d-m which is beyond idiotic.
* Laravel uses Y-m-d as their international format, which can lead to errors depending on SQL SERVER Settings.
* This command checks the vendor directory for the file and updates it if required.
*
* It is suggested that you add the following to your project composer.json file so this command is automatically run
* on install and update.
*
*
"scripts": {
"post-update-cmd": [
"@php artisan mssql:check-universal-date --update"
],
"post-install-cmd": [
"@php artisan mssql:check-universal-date --update"
]
}
*
*/

/**
* Class CheckSQLGrammerDate
* @package App\Console\Commands
*/
class CheckSQLGrammerDate extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'mssql:check-universal-date {--update : Overwrite files in Vendor directory.}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Checks if the illuminate files in vendor directory are using incorrect date format. ';

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

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$filetocheck = base_path('vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\SqlServerGrammar.php');
if (file_exists($filetocheck)){
$file_txt = Str(file_get_contents($filetocheck));
$datestr = 'return \'Y-m-d H:i:s.v\';';
if ($file_txt->contains($datestr)){
if ($this->option('update')){
$UpdatedFile_txt = $file_txt->replace($datestr, 'return \'Ymd H:i:s.v\';');
file_put_contents($filetocheck,$UpdatedFile_txt);

$this->comment("
**********************************
Incorrect Date Format value found
**********************************
File on disk: $filetocheck");
$this->info('
------------------
Updated
------------------');
return 0;
} else {
$this->warn( $this->NoUpdateMessage($filetocheck));
$this->warn('Not Updated');
return 0;
}

}
}
$this->info( "Date Format Appears to be ok.");
return 0;
}

public function NoUpdateMessage($filetocheck){
return "
$filetocheck
has been overwritten with the wrong date format. mssql:CheckSQLDate --update to update the file. ";
}
}
32 changes: 32 additions & 0 deletions src/Providers/MSSQLUniversalDateProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Tobya\Fixsqldate\Providers;



use Tobya\FixSQLDate\Console\CheckSQLGrammerDate;

class MSSQLUniversalDateProvider extends \Illuminate\Support\ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{

}

/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
$this->commands([
CheckSQLGrammerDate::class
]);
}
}

0 comments on commit 4ca52b7

Please sign in to comment.