Reputation: 433
I developed a laravel 4 package that backs up mysql database and pushed to github and packagist. When I pull the package on another laravel installation, all works fine. I publish the config file of the package with no problems, however when I try override some of the configuration in the published config file, the package still uses the original config file in the vendor, it does not look at the published config file. Here is my code:
Vendor/rafia/db-backup/src/Rafia/DbBackup/backupdatabase.php
<?php
namespace Rafia\DbBackup;
use Config;
use Illuminate\Filesystem\Filesystem;
class BackupDatabase {
/**
* @var
*/
private $filesystem;
/**
* @param Filesystem $filesystem
*/
public function __construct(Filesystem $filesystem)
{
$this->filesystem = $filesystem;
}
public function backup()
{
$this->DbBackupFolder();
if($this->runBackup() == 0)
{
return true;
}
return false;
}
private function DbBackupFolder()
{
if(!$this->filesystem->isDirectory(Config::get('DbBackup::DbBackupPath')))
{
return $this->filesystem->makeDirectory(Config::get('DbBackup::DbBackupPath'));
}
return true;
}
private function runBackup()
{
$output = array();
$return_var = NULL;
$command = Config::get('DbBackup::DbMysqlDumpPath')." --opt --host=".Config::get('DbBackup::DbHost')." --user=".Config::get('DbBackup::DbUser')." --password=".Config::get('DbBackup::DbPass')." ".Config::get('DbBackup::DbName')." > ".Config::get('DbBackup::DbBackupPath')."/".Config::get('DbBackup::DbName')."_".date('m_d_y_g-i-a').".sql";
$run = exec($command, $output, $return_var);
return $return_var;
}
}
Vendor/rafia/db-backup/src/Rafia/DbBackup/DbBackupServiceProvider.php
<?php namespace Rafia\DbBackup;
use Illuminate\Foundation\AliasLoader;
use Illuminate\Support\ServiceProvider;
class DbBackupServiceProvider extends ServiceProvider {
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app['BackupDatabase'] = $this->app->share(function($app)
{
$filesystem = $this->app->make('Illuminate\Filesystem\Filesystem');
return new BackupDatabase($filesystem);
});
$this->app->booting(function()
{
AliasLoader::getInstance()->alias('BackupDatabase', 'Rafia\DbBackup\Facades\BackupDatabaseFacade');
});
}
public function boot()
{
$this->package('Rafia/DbBackup');
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return array();
}
}
Vendor/rafia/db-backup/src/config/config.php
<?php
return [
'DbName' => 'packages',
'DbUser' => 'root',
'DbPass' => '',
'DbHost' => 'localhost',
'DbMysqlDumpPath' => 'C:/xampp/mysql/bin/mysqldump',
'DbBackupPath' => app_path().'/storage/DbBackup'
];
Vendor/rafia/db-backup/src/Rafia/DbBackup/Facades/BackupDatabaseFacade.php
<?php
namespace Rafia\DbBackup\Facades;
use Illuminate\Support\Facades\Facade;
class BackupDatabaseFacade extends Facade {
protected static function getFacadeAccessor() { return 'BackupDatabase'; }
}
Thank you in advance for your help
Upvotes: 1
Views: 740
Reputation: 613
In your service provider definition add:
public function boot() {
$this->package('vendor/name', 'namespace');
}
public function register() {
$loader = $this->app['config']->getLoader();
// Get environment name
$env = $this->app['config']->getEnvironment();
// Add package namespace with path set, override package if app config exists in the main app directory
if (file_exists(app_path() . '/config/packages/vendor/namespace')) {
$loader->addNamespace('namespace', app_path() . '/config/packages/vendor/namespace');
} else {
$loader->addNamespace('namespace', __DIR__ . '/../../config');
}
$config = $loader->load($env, 'config', 'namespace');
$this->app['config']->set('namespace::config', $config);
...
This is the article where i have found the solution
Upvotes: 1