简体   繁体   English

Laravel 4软件包配置文件

[英]Laravel 4 package config file

I developed a laravel 4 package that backs up mysql database and pushed to github and packagist. 我开发了一个laravel 4程序包,用于备份mysql数据库并将其推送到github和packagist。 When I pull the package on another laravel installation, all works fine. 当我在另一个laravel安装上拉包装时,一切正常。 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 供应商/ 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;
}
}

Vendor/rafia/db-backup/src/Rafia/DbBackup/DbBackupServiceProvider.php 供应商/ 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();
}

}

Vendor/rafia/db-backup/src/config/config.php 供应商/ 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'
];

Vendor/rafia/db-backup/src/Rafia/DbBackup/Facades/BackupDatabaseFacade.php 供应商/ 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'; }

}

Thank you in advance for your help 预先感谢您的帮助

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 这是我找到解决方案的文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Laravel 包读取包配置文件和未发布的配置文件 - Laravel package reading package config file and not published config file 在工作台软件包中为Laravel 4.2添加配置文件 - Add config file in workbench package for Laravel 4.2 Laravel 5 - 获取包配置 - Laravel 5 - Get Package Config Laravel package 配置未合并 - Laravel package config does not merge Laravel环境软件包配置不起作用 - Laravel environment package config not working 如何使包配置将其不存在的键合并到 Laravel 7.x 中的应用程序配置文件中? - How can I make the package config merge it's non-existing keys into the app config file in laravel 7.x? Laravel4无法读取软件包发布的配置文件,而是使用默认值 - Laravel4 unable to read a package published config file, default is used instead Laravel googlemapper软件包,无法从配置文件中获取变量,“需要Google Maps API密钥。”错误 - Laravel googlemapper package, Cannot get variables from the config file, “Google maps API key is required.” ERROR Laravel 包单元测试在配置文件中的辅助函数失败(调用未定义的方法 base_path()) - Laravel package unit testing failing for helper function in config file (call to undefined method base_path()) 从包含的 package 发布配置和迁移到 Laravel - Publishing config and migrations from included package in Laravel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM