Laravel 4包配置文件

时间:2014-12-25 01:36:19

标签: php laravel-4 packages

我开发了一个laravel 4包来备份mysql数据库并推送到github和packagist。当我在另一个laravel安装上拉包时,一切正常。我发布包的配置文件没有问题,但是当我尝试覆盖发布的配置文件中的一些配置时,包仍然使用供应商中的原始配置文件,它不查看已发布的配置文件。这是我的代码:

供应商/ rafia / DB-备份/ 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;
}
}

供应商/ rafia / DB-备份/ 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();
}

}

供应商/ rafia / DB-备份/ SRC /配置/ config.php中

<?php

return [
'DbName' => 'packages',
'DbUser' => 'root',
'DbPass' => '',
'DbHost' => 'localhost',
'DbMysqlDumpPath' => 'C:/xampp/mysql/bin/mysqldump',
'DbBackupPath' => app_path().'/storage/DbBackup'
];

供应商/ rafia / DB-备份/ SRC / Rafia / DBBACKUP /墙面/ BackupDatabaseFacade.php

<?php


namespace Rafia\DbBackup\Facades;

use Illuminate\Support\Facades\Facade;

class BackupDatabaseFacade extends Facade {

protected static function getFacadeAccessor() { return 'BackupDatabase'; }

}

提前感谢您的帮助

1 个答案:

答案 0 :(得分:1)

在您的服务提供商定义中添加:

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