This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
vbbs/app/Console/Commands/FrameImport.php

92 lines
2.4 KiB
PHP
Raw Normal View History

2018-12-03 12:59:22 +00:00
<?php
namespace App\Console\Commands;
use App\Models\Frame;
2018-12-25 01:48:57 +00:00
use App\Models\Mode;
2018-12-03 12:59:22 +00:00
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class FrameImport extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'frame:import {frame} {index} {file} '.
2018-12-11 12:31:44 +00:00
'{--access=0 : Is frame accessible }'.
2018-12-25 01:48:57 +00:00
'{--public=0 : Is frame limited to CUG }'.
2018-12-03 12:59:22 +00:00
'{--cost=0 : Frame Cost }'.
2018-12-25 01:48:57 +00:00
'{--mode=videotex : Frame Emulation Mode }'.
2018-12-03 12:59:22 +00:00
'{--replace : Replace existing frame}'.
'{--type=i : Frame Type}'.
'{--trim : Trim off header (first 40 chars)}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Import frames into the database. The frames should be in binary format.';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
* @throws \Exception
*/
2018-12-11 12:31:44 +00:00
public function handle()
{
2018-12-03 12:59:22 +00:00
if (! is_numeric($this->argument('frame')))
throw new \Exception('Frame is not numeric: '.$this->argument('frame'));
if (strlen($this->argument('index')) != 1 OR ! preg_match('/^[a-z]$/',$this->argument('index')))
throw new \Exception('Subframe failed validation');
if (! file_exists($this->argument('file')))
throw new \Exception('File not found: '.$this->argument('file'));
2018-12-25 01:48:57 +00:00
$mo = Mode::where('name',$this->option('mode'))->firstOrFail();
2018-12-03 12:59:22 +00:00
$o = new Frame;
if ($this->option('replace')) {
try {
$o = $o->where('frame',$this->argument('frame'))
->where('index',$this->argument('index'))
2018-12-25 01:48:57 +00:00
->where('mode_id',$mo->id)
2018-12-03 12:59:22 +00:00
->firstOrFail();
} catch (ModelNotFoundException $e) {
$this->error('Page not found to replace: '.$this->argument('frame').$this->argument('index'));
die(1);
}
} else {
$o->frame = $this->argument('frame');
$o->index = $this->argument('index');
2018-12-25 01:48:57 +00:00
$o->mode_id = $mo->id;
2018-12-03 12:59:22 +00:00
}
$o->content = ($this->option('trim'))
? substr(file_get_contents($this->argument('file')),40)
: file_get_contents($this->argument('file'));
2018-12-11 12:31:44 +00:00
$o->access = $this->option('access');
2018-12-25 01:48:57 +00:00
$o->public = $this->option('public');
2018-12-03 12:59:22 +00:00
$o->cost = $this->option('cost');
$o->type = $this->option('type');
$o->save();
2018-12-11 12:31:44 +00:00
}
2018-12-25 01:48:57 +00:00
}