-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding the ability to add and remove precanned messages
- Loading branch information
Showing
15 changed files
with
366 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,5 @@ yarn-error.log | |
/.idea | ||
/.vscode | ||
/config/lgtvs.php | ||
/config/precanned.php | ||
/public/aiowebostv/__pycache__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Services\ModifyConfig; | ||
use Illuminate\Console\Command; | ||
|
||
class AddPrecannedResponse extends Command | ||
{ | ||
protected $signature = 'lg:add-response { | ||
response : The response to store, wrapped in double quotes }'; | ||
|
||
protected $description = 'Add a precanned response (e.g. "Lili is awake")'; | ||
|
||
public string $file_path; | ||
|
||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
$this->file_path = base_path().'/config/precanned.php'; | ||
} | ||
|
||
public function handle() | ||
{ | ||
$config = new ModifyConfig($this->file_path); | ||
|
||
$config->add($this->argument('response')); | ||
|
||
$this->components->info("Added '{$this->argument('response')}' to the list"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Services\ModifyConfig; | ||
use Illuminate\Console\Command; | ||
|
||
class RemovePrecannedResponse extends Command | ||
{ | ||
protected $signature = 'lg:remove-response'; | ||
|
||
protected $description = 'Need to remove a saved response? Do it here.'; | ||
|
||
public string $file_path; | ||
|
||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
|
||
$this->file_path = base_path() . '/config/precanned.php'; | ||
} | ||
|
||
public function handle() | ||
{ | ||
|
||
$config = new ModifyConfig($this->file_path); | ||
$responseList = $config->read(); | ||
|
||
if (isset($responseList) && count($responseList) < 1) { | ||
$this->components->warn('No stored responses found'); | ||
return 0; | ||
} | ||
|
||
$response = $this->choice('Which response would you like to remove', $responseList); | ||
$response = intval($response); | ||
|
||
|
||
$this->components->warn('About to remove:'); | ||
$this->line(' ' . $responseList[$response]); | ||
|
||
$confirm = $this->components->confirm('Are you sure', false); | ||
|
||
if ($confirm) { | ||
$config->remove($response); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace App\Services; | ||
|
||
class ModifyConfig | ||
{ | ||
public function __construct(public string $base_path, public bool $overwrite = false) | ||
{ | ||
} | ||
|
||
public function create_blank_file(): void | ||
{ | ||
if (file_exists($this->base_path) && !$this->overwrite) { | ||
return; | ||
} | ||
|
||
$empty_file = <<< 'PHP' | ||
<?php | ||
return [ | ||
]; | ||
PHP; | ||
|
||
file_put_contents($this->base_path, $empty_file); | ||
} | ||
|
||
public function add($instance) | ||
{ | ||
if (!file_exists($this->base_path)) { | ||
$this->create_blank_file(); | ||
} | ||
|
||
$contents = $this->read(); | ||
|
||
// Don't add a duplicate; just return without doing anything | ||
if (in_array($instance, $contents)) { | ||
return; | ||
} | ||
|
||
$contents[] = $instance; | ||
$new_file = "<?php\n\nreturn " . $this->better_var_export($contents) . ';'; | ||
file_put_contents($this->base_path, $new_file); | ||
} | ||
|
||
public function remove(string $index) | ||
{ | ||
$contents = $this->read(); | ||
|
||
unset($contents[$index]); | ||
|
||
$file = "<?php\n\nreturn " . $this->better_var_export($contents) . ';'; | ||
file_put_contents($this->base_path, $file); | ||
} | ||
|
||
public function read() | ||
{ | ||
return include $this->base_path; | ||
} | ||
|
||
protected function better_var_export($expression) | ||
{ | ||
$export = var_export($expression, true); | ||
$export = preg_replace('/^([ ]*)(.*)/m', '$1$1$2', $export); | ||
$array = preg_split("/\r\n|\n|\r/", $export); | ||
$array = preg_replace(["/\s*array\s\($/", "/\)(,)?$/", "/\s=>\s$/"], [null, ']$1', ' => ['], $array); | ||
$export = implode(PHP_EOL, array_filter(['['] + $array)); | ||
|
||
return $export; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.