Add the following to your composer.json
file:
"scripts": {
"pre-autoload-dump": [
"@php artisan cashier:namespace"
]
}
<?php | |
namespace App\Console\Commands; | |
use Illuminate\Console\Command; | |
use Symfony\Component\Finder\Finder; | |
class FixCashierNamespace extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'cashier:namespace'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Fix Laravel Cashier namespaces'; | |
/** | |
* Execute the console command. | |
* | |
* @return mixed | |
*/ | |
public function handle() | |
{ | |
$this->appendStripeNamespace('Stripe'); | |
$this->appendBraintreeNamespace('Braintree'); | |
} | |
/** | |
* Append a suffix to the Stripe Cashier namespace. | |
* | |
* @param $suffix | |
*/ | |
private function appendStripeNamespace($suffix) | |
{ | |
$this->replaceComposerAutoload('laravel/cashier', $suffix); | |
$this->replaceNamespace('laravel/cashier', $suffix); | |
$this->keepStripeServiceProvider($suffix); | |
} | |
/** | |
* Append a suffix to the Braintree Cashier namespace. | |
* | |
* @param $suffix | |
*/ | |
private function appendBraintreeNamespace($suffix) | |
{ | |
$this->replaceComposerAutoload('laravel/cashier-braintree', $suffix); | |
$this->replaceNamespace('laravel/cashier-braintree', $suffix); | |
} | |
private function keepStripeServiceProvider($suffix) | |
{ | |
$composerPath = base_path("vendor/laravel/cashier/composer.json"); | |
$serviceProviderPath = base_path("vendor/laravel/cashier/src/CashierServiceProvider.php"); | |
$this->replace( | |
$composerPath, | |
"Laravel\\\\Cashier\\\\$suffix\\\\CashierServiceProvider", | |
'Laravel\\\\Cashier\\\\CashierServiceProvider' | |
); | |
$this->replace( | |
$serviceProviderPath, | |
"namespace Laravel\Cashier\\$suffix;", | |
'namespace Laravel\Cashier;' | |
); | |
} | |
private function replaceComposerAutoload($package, $suffix) | |
{ | |
$path = base_path("vendor/$package/composer.json"); | |
$search = 'Laravel\\\\Cashier\\\\'; | |
$replace = "Laravel\\\\Cashier\\\\$suffix\\\\"; | |
$this->replaceOnce($path, $search, $replace); | |
} | |
private function replaceNamespace($package, $suffix) | |
{ | |
$sourceDirectory = base_path("vendor/$package/src"); | |
if (! file_exists($sourceDirectory)) { | |
return; | |
} | |
$files = (new Finder)->files()->in($sourceDirectory); | |
foreach ($files as $file) { | |
$path = (string) $file; | |
$class = str_replace([$path, '/', '.php'], ['', '\\', ''], $file); | |
$search = "Laravel\\Cashier" . $class; | |
$replace = "Laravel\\Cashier\\$suffix" . $class; | |
$this->replaceOnce($path, $search, $replace); | |
} | |
} | |
/** | |
* Perform a search & replace safely. | |
* | |
* @param string $path | |
* @param string $search | |
* @param string $replace | |
*/ | |
private function replaceOnce($path, $search, $replace) | |
{ | |
// Perform the reverse first. | |
$this->replace($path, $replace, $search); | |
// Safe to perform again. | |
$this->replace($path, $search, $replace); | |
} | |
/** | |
* Perform a search & replace. | |
* | |
* @param string $path | |
* @param string $search | |
* @param string $replace | |
*/ | |
private function replace($path, $search, $replace) | |
{ | |
if (! file_exists($path)) { | |
return; | |
} | |
$contents = file_get_contents($path); | |
$contents = str_replace($search, $replace, $contents); | |
file_put_contents($path, $contents); | |
} | |
} |