Laravel 具有特征的控制台命令
Laravel Console Command with a Trait
我为正在处理的特征设置了一些配置变量,其中之一是 data_format
变量,它可以是 json 或 xml。
是这样设置的:
$this->data_format = config('rightmove.FORMAT');
在控制器中,我可以 运行 一个 dd($this->data_format) 并且它 returns : "json"
我已经将其设为控制台命令,但是当我尝试 运行 带有 dd $this->data_format
的控制台命令时 returns
NULL
以及随后我的其余代码错误。
我已经 运行 一个 php artisan config:cache
命令来清除缓存,但控制台似乎没有获取该配置变量?
我的 App\Console\Commands\UpdateRightMove
看起来如下:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Traits\RightMoveTrait;
use App\Property;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use QCod\AppSettings\Setting\AppSettings;
class UpdateRightmove extends Command
{
use RightMoveTrait;
private $is_set;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'rightmove:update';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Updates Rightmove - Creates / Updates and Deletes properties from the Rightmove API';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
$this->is_set = setting('rightmove');
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
if($this->is_set == 1)
{
// Rightmove is Enabled via settings (Run Command)...
$now = Carbon::now();
// Check App Cache for Last Time RightMove Artisan Command was Ran...
//$last_ran = cache('rightmove_update');
$last_ran = '2019-06-07 14:52:40';
if($last_ran == NULL)
{
// Send ALL Properties to Rightmove, First Time Ran....
$properties = Property::whereNotNull('ref')
->where('beds', '>', 0 )
->where('price', '>', 0)
->where('name', '!=', '')
->where('city', '!=', '')
->whereRaw('LENGTH(postcode) >= 5')
->get();
}
else
{
$from_date = Carbon::now();
// Get a Difference between last ran and now...
$time_diff = $from_date->diffInHours($last_ran);
// This will run and get properties that have changed since the last task ran
$to_date = Carbon::now()->subHours($time_diff);
// Get Filtered Properties & One's added / updated in last 12 hours...
$properties = Property::whereNotNull('ref')
->where('beds', '>', 0 )
->where('price', '>', 0)
->where('name', '!=', '')
->where('city', '!=', '')
->whereRaw('LENGTH(postcode) >= 5')
->whereBetween('created_at', [$to_date, $from_date])
->orWhereBetween('updated_at', [$to_date, $from_date])
->get();
}
// Get Settings....
if(setting('overseas') == 0)
{
// UK Based....
$this->update_feed_uk($properties);
}
else
{
// Overseas....
}
Cache::rememberForever('rightmove_update', function()
{
return now()->toDateTimeString();
});
}
}
}
我的RightMoveTrait
如下:
<?php
namespace App\Traits;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use App\Property;
use App\PropertyType;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
trait RightMoveTrait
{
private $network_id;
private $branch_id;
private $data_format;
private $environment;
private $allow_delete;
private $current_time;
public function __construct()
{
$this->network_id = config('rightmove.NETWORK_ID');
$this->branch_id = config('rightmove.BRANCH_ID');
$this->data_format = config('rightmove.FORMAT');
$this->allow_delete = config('rightmove.ALLOW_DELETE');
$this->environment = config('rightmove.ENVIRONMENT');
$now = Carbon::now();
$this->current_time = $now;
$this->refs = [];
}
function update_feed_uk($properties)
{
dd($this->data_format);
}
}
Edit 控制台 运行 似乎跳过了特征中的 __construct
。因此,在控制台命令 (UpdateRightMove
) 的构造方法中,我添加了变量并且它可以正常工作。
public function __construct()
{
parent::__construct();
$this->is_set = setting('rightmove');
$this->network_id = config('rightmove.NETWORK_ID');
$this->branch_id = config('rightmove.BRANCH_ID');
$this->data_format = config('rightmove.FORMAT');
$this->allow_delete = config('rightmove.ALLOW_DELETE');
$this->environment = config('rightmove.ENVIRONMENT');
$now = Carbon::now();
$this->current_time = $now;
$this->refs = [];
}
你的特征的 __construct()
函数没有被调用,因为 class 使用它自己的 __construct()
函数。
在你的 UpdateRightmove
class:
use RightMoveTrait {
RightMoveTrait::__construct as private _rightMoveTraitConstruct;
}
public function __construct()
{
parent::__construct();
$this->_rightMoveTraitConstruct();
$this->is_set = setting('rightmove');
}
我为正在处理的特征设置了一些配置变量,其中之一是 data_format
变量,它可以是 json 或 xml。
是这样设置的:
$this->data_format = config('rightmove.FORMAT');
在控制器中,我可以 运行 一个 dd($this->data_format) 并且它 returns : "json"
我已经将其设为控制台命令,但是当我尝试 运行 带有 dd $this->data_format
的控制台命令时 returns
NULL
以及随后我的其余代码错误。
我已经 运行 一个 php artisan config:cache
命令来清除缓存,但控制台似乎没有获取该配置变量?
我的 App\Console\Commands\UpdateRightMove
看起来如下:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Traits\RightMoveTrait;
use App\Property;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use QCod\AppSettings\Setting\AppSettings;
class UpdateRightmove extends Command
{
use RightMoveTrait;
private $is_set;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'rightmove:update';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Updates Rightmove - Creates / Updates and Deletes properties from the Rightmove API';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
$this->is_set = setting('rightmove');
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
if($this->is_set == 1)
{
// Rightmove is Enabled via settings (Run Command)...
$now = Carbon::now();
// Check App Cache for Last Time RightMove Artisan Command was Ran...
//$last_ran = cache('rightmove_update');
$last_ran = '2019-06-07 14:52:40';
if($last_ran == NULL)
{
// Send ALL Properties to Rightmove, First Time Ran....
$properties = Property::whereNotNull('ref')
->where('beds', '>', 0 )
->where('price', '>', 0)
->where('name', '!=', '')
->where('city', '!=', '')
->whereRaw('LENGTH(postcode) >= 5')
->get();
}
else
{
$from_date = Carbon::now();
// Get a Difference between last ran and now...
$time_diff = $from_date->diffInHours($last_ran);
// This will run and get properties that have changed since the last task ran
$to_date = Carbon::now()->subHours($time_diff);
// Get Filtered Properties & One's added / updated in last 12 hours...
$properties = Property::whereNotNull('ref')
->where('beds', '>', 0 )
->where('price', '>', 0)
->where('name', '!=', '')
->where('city', '!=', '')
->whereRaw('LENGTH(postcode) >= 5')
->whereBetween('created_at', [$to_date, $from_date])
->orWhereBetween('updated_at', [$to_date, $from_date])
->get();
}
// Get Settings....
if(setting('overseas') == 0)
{
// UK Based....
$this->update_feed_uk($properties);
}
else
{
// Overseas....
}
Cache::rememberForever('rightmove_update', function()
{
return now()->toDateTimeString();
});
}
}
}
我的RightMoveTrait
如下:
<?php
namespace App\Traits;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use App\Property;
use App\PropertyType;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
trait RightMoveTrait
{
private $network_id;
private $branch_id;
private $data_format;
private $environment;
private $allow_delete;
private $current_time;
public function __construct()
{
$this->network_id = config('rightmove.NETWORK_ID');
$this->branch_id = config('rightmove.BRANCH_ID');
$this->data_format = config('rightmove.FORMAT');
$this->allow_delete = config('rightmove.ALLOW_DELETE');
$this->environment = config('rightmove.ENVIRONMENT');
$now = Carbon::now();
$this->current_time = $now;
$this->refs = [];
}
function update_feed_uk($properties)
{
dd($this->data_format);
}
}
Edit 控制台 运行 似乎跳过了特征中的 __construct
。因此,在控制台命令 (UpdateRightMove
) 的构造方法中,我添加了变量并且它可以正常工作。
public function __construct()
{
parent::__construct();
$this->is_set = setting('rightmove');
$this->network_id = config('rightmove.NETWORK_ID');
$this->branch_id = config('rightmove.BRANCH_ID');
$this->data_format = config('rightmove.FORMAT');
$this->allow_delete = config('rightmove.ALLOW_DELETE');
$this->environment = config('rightmove.ENVIRONMENT');
$now = Carbon::now();
$this->current_time = $now;
$this->refs = [];
}
你的特征的 __construct()
函数没有被调用,因为 class 使用它自己的 __construct()
函数。
在你的 UpdateRightmove
class:
use RightMoveTrait {
RightMoveTrait::__construct as private _rightMoveTraitConstruct;
}
public function __construct()
{
parent::__construct();
$this->_rightMoveTraitConstruct();
$this->is_set = setting('rightmove');
}