Browse Source

adding

master
Björn 3 years ago
parent
commit
35aed1daac
17 changed files with 330 additions and 82 deletions
  1. +91
    -0
      app/Commands/AdminLogsCommand.php
  2. +91
    -0
      app/Commands/AdminSourcesCommand.php
  3. +5
    -3
      app/Commands/CrawlerRunCommand.php
  4. +0
    -43
      app/Commands/SettingsMenuCommand.php
  5. +26
    -0
      app/Handlers/Action/ActionHandler.php
  6. +1
    -2
      app/Handlers/Action/ActionInterface.php
  7. +0
    -11
      app/Handlers/Action/MediaMove.php
  8. +42
    -0
      app/Handlers/Handler.php
  9. +3
    -5
      app/Handlers/Response/ResponseHandler.php
  10. +3
    -3
      app/Handlers/Response/ResponseHandlerInterface.php
  11. +20
    -0
      app/Models/Action.php
  12. +19
    -0
      app/Models/Log.php
  13. +10
    -3
      app/Models/Source.php
  14. +0
    -3
      database/migrations/2021_05_09_073140_create_actions_table.php
  15. +0
    -5
      database/migrations/2021_05_09_073140_create_logs_table.php
  16. +13
    -0
      resources/CustomHandlers/Action/FileMove.php
  17. +6
    -4
      resources/CustomHandlers/Response/Rss.php

+ 91
- 0
app/Commands/AdminLogsCommand.php View File

@ -0,0 +1,91 @@
<?php
namespace App\Commands;
use Illuminate\Console\Scheduling\Schedule;
use LaravelZero\Framework\Commands\Command;
use PhpSchool\CliMenu\Builder\CliMenuBuilder;
use PhpSchool\CliMenu\CliMenu;
class AdminLogsCommand extends Command
{
/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'admin:logs';
/**
* The description of the command.
*
* @var string
*/
protected $description = 'Command description';
/**
*
*
*/
private function addInputItem()
{
$callable = function(CliMenu $menu)
{
$input = $menu->askText();
$result = $input->ask();
$menu->redraw();
};
return $callable;
}
/**
*
*
*/
private function addCreateItem()
{
$menu = function(CliMenuBuilder $builder)
{
$data = [];
$builder
->setTitle('Settings > Add')
->disableDefaultItems()
// input name
->addItem('name', $this->addInputItem())
// input url
->addItem('url', $this->addInputItem())
->addItem('handler', $this->addInputItem())
->addLineBreak('-')
->addSubMenu('actions', $this->addActions())
->addLineBreak('-');
};
return $menu;
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
// create menu
$builder = $this->menu('Settings');
$builder
->addSubMenu('Add', $this->addCreateItem())
->addLineBreak('-');
$mainmenu = $builder->build();
$mainmenu->open();
}
}

+ 91
- 0
app/Commands/AdminSourcesCommand.php View File

@ -0,0 +1,91 @@
<?php
namespace App\Commands;
use Illuminate\Console\Scheduling\Schedule;
use LaravelZero\Framework\Commands\Command;
use PhpSchool\CliMenu\Builder\CliMenuBuilder;
use PhpSchool\CliMenu\CliMenu;
class AdminSourcesCommand extends Command
{
/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'admin:sources';
/**
* The description of the command.
*
* @var string
*/
protected $description = 'Command description';
/**
*
*
*/
private function addInputItem()
{
$callable = function(CliMenu $menu)
{
$input = $menu->askText();
$result = $input->ask();
$menu->redraw();
};
return $callable;
}
/**
*
*
*/
private function addCreateItem()
{
$menu = function(CliMenuBuilder $builder)
{
$data = [];
$builder
->setTitle('Settings > Add')
->disableDefaultItems()
// input name
->addItem('name', $this->addInputItem())
// input url
->addItem('url', $this->addInputItem())
->addItem('handler', $this->addInputItem())
->addLineBreak('-')
->addSubMenu('actions', $this->addActions())
->addLineBreak('-');
};
return $menu;
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
// create menu
$builder = $this->menu('Settings');
$builder
->addSubMenu('Add', $this->addCreateItem())
->addLineBreak('-');
$mainmenu = $builder->build();
$mainmenu->open();
}
}

+ 5
- 3
app/Commands/CrawlerRunCommand.php View File

@ -35,12 +35,14 @@ class CrawlerRunCommand extends Command
'https://steadyhq.com/rss/insertmoin?auth=d37bffc9-9a84-4eed-95f6-3b6cb77c2406'
];
$handler = new RssHandler();
$response = $handler->get($url);
$handlerClass = $source->response_handler_class;
$handler = new $handlerClass($source);
$response = $handler->getResponse();
// if it is a 200
if ($response->ok()) {
$handler->run($response, $actions);
$handler->run();
// if there is an error on the source
} else if ($response->serverError()) {


+ 0
- 43
app/Commands/SettingsMenuCommand.php View File

@ -1,43 +0,0 @@
<?php
namespace App\Commands;
use Illuminate\Console\Scheduling\Schedule;
use LaravelZero\Framework\Commands\Command;
class SettingsMenuCommand extends Command
{
/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'settings:menu';
/**
* The description of the command.
*
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
// create menu
$builder = $this->menu('Settings')
->addSubMenu('Sources', [
'd',
'e'
]);
$builder->addLineBreak('-');
$mainmenu = $builder->build();
$mainmenu->open();
}
}

+ 26
- 0
app/Handlers/Action/ActionHandler.php View File

@ -0,0 +1,26 @@
<?php
namespace App\ResponseHandler;
use App\Handlers\Handler;
/**
*
*
*
*/
class ActionHandler extends Handler implements ActionHandlerInterface
{
protected $data;
/**
*
* @param mixed $data
*
*/
public function __construct($data)
{
$this->data = $data;
}
}

+ 1
- 2
app/Handlers/Action/ActionInterface.php View File

@ -1,6 +1,6 @@
<?php
use App\Action;
namespace App\Handlers\Action;
/**
*
@ -8,6 +8,5 @@ use App\Action;
*/
interface ActionInterface
{
public function config();
public function run();
}

+ 0
- 11
app/Handlers/Action/MediaMove.php View File

@ -1,11 +0,0 @@
<?php
/**
*
*
*
*/
class FileMoveActionHandler extends ActionHandler {
}

+ 42
- 0
app/Handlers/Handler.php View File

@ -0,0 +1,42 @@
<?php
namespace App\Handlers;
/**
*
*
*
*/
abstract class Handler
{
/**
*
*
* @param String $className
* @return Mixed
*
*/
protected function getHandlerClass(String $className)
{
$result = NULL;
// create class
$class = 'App\\Handlers\\'.$className);
// if not found check for custom handlers
if (class_exists($class)) {
$result = new $class();
} else {
// create class
$class = 'CustomHandlers\\'.$className;
if (class_exists($class)) {
$result = new $class();
}
}
return $result;
}
}

+ 3
- 5
app/Handlers/Response/ResponseHandler.php View File

@ -2,20 +2,18 @@
namespace App\ResponseHandler;
use App\Handlers\Handler;
/**
*
*
*
*/
class RssResponseHandler extends ResponseHandler implements ResponseHandlerInterface
class ResponseHandler extends Handler implements ResponseHandlerInterface
{
protected $response;
protected $source;
protected $actions;
protected $beforeActions;
protected $afterActions;
/**
*
* @param [type] $actions


+ 3
- 3
app/Handlers/Response/ResponseHandlerInterface.php View File

@ -7,8 +7,8 @@ namespace App\ResponseHandler;
*
*
*/
interface RssResponseInterface
interface ResponseHandlerInterface
{
public function get();
public function handle();
public function getResponse();
public function run();
}

+ 20
- 0
app/Models/Action.php View File

@ -0,0 +1,20 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
*
*
*
*/
class Action extends Model
{
protected $fillable = [
'action_handler_class',
'priority',
'source_id'
];
}

+ 19
- 0
app/Models/Log.php View File

@ -0,0 +1,19 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
*
*
*
*/
class Log extends Model
{
protected $fillable = [
'message',
'state'
];
}

+ 10
- 3
app/Models/Source.php View File

@ -1,13 +1,20 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
*
*
*
*
*/
class Source
class Source extends Model
{
protected $fillable = [
'name',
'url',
'response_handler_class'
];
}

+ 0
- 3
database/migrations/2021_05_09_073140_create_actions_table.php View File

@ -17,9 +17,6 @@ class CreateActionsTable extends Migration
$table->id();
$table->string('action_handler_class');
$table->enum('type', [
'before', 'after'
])->nullable();
$table->integer('priority');
$table->unsignedBigInteger('source_id');


+ 0
- 5
database/migrations/2021_05_09_073140_create_logs_table.php View File

@ -21,11 +21,6 @@ class CreateLogsTable extends Migration
'critical', 'info', 'error'
]);
$table->unsignedBigInteger('source_id');
$table->foreign('source_id')
->references('id')
->on('sources');
$table->timestamps();
});
}


+ 13
- 0
resources/CustomHandlers/Action/FileMove.php View File

@ -0,0 +1,13 @@
<?php
namespace CustomHandlers\Actions;
/**
*
*
*
*/
class FileMove extends ActionHandler {
}

app/Handlers/Response/Rss.php → resources/CustomHandlers/Response/Rss.php View File

@ -1,6 +1,6 @@
<?php
namespace App\ResponseHandler;
namespace CustomHandlers\Response;
/**
* Handle Response of a Rss Feed,
@ -18,7 +18,7 @@ class Rss extends ResponseHandler
* @return object
*
*/
public function get()
public function getResponse()
{
$this->response = Http::withHeaders([
"Content-Type" => "text/xml;charset=utf-8"
@ -33,13 +33,15 @@ class Rss extends ResponseHandler
*
*
*/
public function handle()
public function run()
{
$xml = new SimpleXMLElement($this->response->body());
foreach($xml->channel->item as $item) {
foreach($this->actions as $action) {
$action->handle($item);
$actionClass = $action->action_handler_class;
$action = new ActionClass($item);
$action->run();
}
}
}

Loading…
Cancel
Save