Skip to content

Commit

Permalink
Rewrite framework structure
Browse files Browse the repository at this point in the history
  • Loading branch information
KikyTokamuro committed Jan 27, 2024
1 parent 20556c0 commit ef13e77
Show file tree
Hide file tree
Showing 13 changed files with 318 additions and 151 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022 Kiky Tokamuro (Daniil Archangelsky)
Copyright (c) 2022-2024 Kiky Tokamuro (Daniil Archangelsky)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,4 @@ Tiny framework for Yandex Alice

### Adding your own сommands
1. Add new command to `src/AliceFramework/Commands/` using examples `Example.php` and `RandomQuote.php` (`start` method must be present)
2. Add path to new command class in `src/AliceFramework/Commands/Common.php`

### TODO
- Command arguments
2. Add path to new command class in `src/AliceFramework/Commands/CommandList.php`
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
}
],
"license": "MIT",
"version": "1.0.0",
"version": "2.0.0",
"require": {
"php": "^8.0.0"
}
Expand Down
8 changes: 2 additions & 6 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

require __DIR__ . '/vendor/autoload.php';

use AliceFramework\Core;

header('Content-Type: application/json');

// Get request and start
$core = new Core(file_get_contents('php://input'), 'мой информер');
echo $core->start();

// Get request and start app
echo (new AliceFramework\App(file_get_contents('php://input'), 'Моя автоматизация'))->start();
64 changes: 64 additions & 0 deletions src/AliceFramework/App.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace AliceFramework;

use AliceFramework\Commands\CommandList;

class App
{
/**
* @var string Dialog name
*/
private string $name;

/**
* @var Request Dialog request
*/
private Request $request;

/**
* @param string $data Request
* @param string $name Dialog Name
*/
public function __construct(string $data, string $name = "AliceFramework")
{
$this->name = strtolower($name);
$this->request = new Request($data);
}

/**
* Start processing request data
*
* @return false|string
*/
public function start() : false|string
{
if (!$this->request->isValid())
return Response::error('Wrong request');

$response = new Response(
$this->request->getSessionId(),
$this->request->getMessageId(),
$this->request->getUserId()
);

// Command includes skill name
if (str_contains($this->request->getCommand(), $this->name) || empty($this->request->getCommand()))
return $response->success('Hello i am ' . $this->name);

// Check command
if (!CommandList::exists($this->request->getCommand()))
return $response->success('I do not understand');

// Command processing
try {
return $response->success(
(new CommandList::$commands[$this->request->getCommand()])->start()
);
}
catch (\Exception $e)
{
return Response::error('Command error');
}
}
}
8 changes: 8 additions & 0 deletions src/AliceFramework/Commands/Command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace AliceFramework\Commands;

interface Command
{
public function start() : string;
}
25 changes: 25 additions & 0 deletions src/AliceFramework/Commands/CommandList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace AliceFramework\Commands;

class CommandList
{
/**
* @var array $commands List of all active commands
*/
public static array $commands = [
"случайная цитата" => RandomQuote::class,
"пример" => Example::class
];

/**
* Check if command exists
*
* @param string $command Command name
* @return bool
*/
public static function exists(string $command) : bool
{
return array_key_exists($command, CommandList::$commands);
}
}
14 changes: 0 additions & 14 deletions src/AliceFramework/Commands/Common.php

This file was deleted.

12 changes: 5 additions & 7 deletions src/AliceFramework/Commands/Example.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
namespace AliceFramework\Commands;

/**
* Example class - Example command
* Example command
*/
class Example
class Example implements Command
{
public function __construct() { }

/**
* start - Start command
* Start command
*/
public function start()
public function start() : string
{
return "это пример";
return "This is example";
}
}
24 changes: 17 additions & 7 deletions src/AliceFramework/Commands/RandomQuote.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,33 @@
namespace AliceFramework\Commands;

/**
* RandomQuote class - Command for get random quote
* Command for get random quote
*/
class RandomQuote
class RandomQuote implements Command
{
/**
* @var string API url
*/
private string $url;

public function __construct()
{
$rand = random_int(0, 999999);
$this->url = "http://api.forismatic.com/api/1.0/?method=getQuote&key=".$rand."&format=json&lang=ru";
}

/**
* start - Start command
* Start command
*
* @return string
*/
public function start()
public function start() : string
{
$json = file_get_contents($this->url);
$decoded = json_decode($json, true);
return $decoded['quoteText'] . " - " . $decoded['quoteAuthor'];
$decoded = json_decode(file_get_contents($this->url), true);

if (isset($decoded['quoteText'], $decoded['quoteAuthor']))
return $decoded['quoteText'] . " - " . $decoded['quoteAuthor'];

return "Random quote";
}
}
111 changes: 0 additions & 111 deletions src/AliceFramework/Core.php

This file was deleted.

Loading

0 comments on commit ef13e77

Please sign in to comment.