PHP code example of autumn / autumn-framework

1. Go to this page and download the library: Download autumn/autumn-framework library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

autumn / autumn-framework example snippets




Autumn\Framework\Boot\AutumnApplication::run($argc, $argv));

namespace Market;

class Car
{
    public $id;
}

namespace Market;

use \Autumn\Framework\Annotation\RestController;
use \Autumn\Framework\Annotation\RequestMapping;

/**
 * @RestController
 */
class CarController
{
    /**
     * @RequestMapping(value="/cars", method="GET")
     */
    public function list()
    {
        $cars = [];
        for ($i=1; $i<=10; ++$i) {
            $car = new Car();
            $car->id = $i;
            $cars[] = $car;
        }

        return $cars;
    }
}

use \Autumn\Framework\Context\Annotation\Configuration;
use \Autumn\Framework\Context\Annotation\Bean;

/**
 * Main Configuration
 * 
 * @Configuration
 */
class DaTrieConfiguration
{
    /** @Bean */
    public function daTrieService()
    {
        // Implementation of interface DaTrieServer
        return new DaTrieServiceImpl();
    }
}

use \Autumn\Framework\Annotation\RestController;
use \Autumn\Framework\Annotation\RequestMapping;
use \Autumn\Framework\Context\Annotation\Autowired;

/**
 * @RestController
 */
class SearchInController
{
    /** @Autowired(value=DaTrieService::class) */
    private $daTrieService;
}

use \Autumn\Framework\Annotation\RestController;
use \Autumn\Framework\Annotation\RequestMapping;
use \Autumn\Framework\Context\Annotation\Autowired;

/**
 * @RestController
 */
class SearchInController
{
    private $daTrieService;
    
    /**
     * @Autowired
     */
    public function setDaTrieService(DaTrieService $daTrieService)
    {
        $this->daTrieService = $daTrieService;
    }
}

namespace Market;

use \Autumn\Framework\Annotation\RestController;
use \Autumn\Framework\Annotation\RequestMapping;
use \Autumn\Framework\Web\Bind\Annotation\RequestBody;

/**
 * @RestController
 */
class CarController
{
    /**
     * @RequestMapping(value="/comments", method="POST")
     * @RequestBody(value="carId")
     */
    public function create(string $carId)
    {
        // ...
    }
}

namespace Market;

use \Autumn\Framework\Context\Listener\ContextRefreshedEventApplicationListener;
use \Autumn\Framework\Context\Event\ContextRefreshedEvent;

class LoadCommentsApplicationListener implements ContextRefreshedEventApplicationListener
{
    public function onApplicationEvent(ContextRefreshedEvent $event)
    {
        // Load comments
    }
}

use \Autumn\Framework\Context\Annotation\Configuration;
use \Autumn\Framework\Context\Annotation\Bean;
use \Autumn\Framework\Swoole\Coroutine\MySql\MySqlTemplate;

/**
 * Main Configuration
 * 
 * @Configuration
 */
class MainConfiguration
{
    /** @Bean */
    public function mySqlOperations()
    {
        return new MySqlTemplate([
            'host' => 'localhost',
            'port' => 3306,
            'user' => 'root',
            'password' => '',
            'database' => 'test',
        ]);
    }
}

namespace Market;

use \Autumn\Framework\Swoole\Coroutine\MySql\MySqlOperations;

class CarService
{
    /** @Autowired(value=MySqlOperations::class) */
    private $mySqlOperations;
    
    const LIST_ALL_SQL = "SELECT * FROM `cars` WHERE `id`>?";
    
    public function listCars()
    {
        $generator = $this->mySqlOperations->queryAll(self::LIST_ALL_SQL, function($row) {
            $car = new Car();
            $car->id = $row['id'];
            return $car;
        }, 'id');
        
        $cars = [];
        foreach ($generator as $car) {
            // ...
            $cars[] = $car;
        }      
        
        return $cars;
    }
}

php index.php