PHP code example of amp-framework / ampf

1. Go to this page and download the library: Download amp-framework/ampf 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/ */

    

amp-framework / ampf example snippets


use ampf\Bean\BeanFactory;
use ampf\Bootstrap\ApplicationContext;
use ampf\Bootstrap\ErrorSettings;
use ampf\Bootstrap\TraceSettings;
use ampf\Request\HttpRequestInterface;
use ampf\Router\HttpRouterInterface;

duction)
ErrorSettings::applyDefaults();
TraceSettings::apply();
date_default_timezone_set('UTC');
mb_internal_encoding('UTF-8');

// 2. the configuration: an ordered list of PHP files, each returning an array, merged into one
$config = ApplicationContext::boot([
    __DIR__ . '/../vendor/amp-framework/ampf/config/default.php', // the framework's defaults
    __DIR__ . '/../vendor/amp-framework/ampf/config/http.php',    // the framework's web beans
    __DIR__ . '/../config/default.php',                           // the application's beans and settings
    __DIR__ . '/../config/http.php',                              // the application's controllers and routes
    __DIR__ . '/../config/local.php',                             // machine settings (database, keys): not in Git
]);
ErrorSettings::apply($config, dirname(__DIR__));                  // the `errors` block

// 3. the bean factory over it
$beanFactory = new BeanFactory($config);

// 4. route the request
$router = $beanFactory->get('Router');
assert($router instanceof HttpRouterInterface);
$request = $beanFactory->get('Request');
assert($request instanceof HttpRequestInterface);
$router->route($request);

// 5. send the status code, the headers and the body (or the redirect)
$request->flush();

return [
    'beans' => [
        ReportServiceInterface::class => ['class' => ReportService::class],
        SessionServiceInterface::class => ['class' => MySessionService::class], // replaces the framework's
        'ReportController' => ['class' => ReportController::class],             // controllers: named by the routes
        'View' => ['class' => MyHttpView::class, 'scope' => 'prototype'],
    ],
];

trait ReportServiceAccess
{
    use AbstractAccess;

    protected ?ReportServiceInterface $__reportService = null;

    public function getReportService(): ReportServiceInterface
    {
        if ($this->__reportService === null) {
            $object = $this->getBeanFactory()->get(ReportServiceInterface::class);
            assert($object instanceof ReportServiceInterface);
            $this->setReportService($object);
        }

        assert($this->__reportService instanceof ReportServiceInterface);

        return $this->__reportService;
    }

    public function setReportService(ReportServiceInterface $object): void
    {
        $this->__reportService = $object;
    }
}

// config/cli.php of the application
return [
    'routes' => [
        'beanAccess/generate' => ['pattern' => 'beanAccess/generate', 'controller' => 'BeanAccessGeneratorController'],
    ],
    'beanAccessGenerator' => [
        'projectRoot' => dirname(__DIR__),
        'namespace' => 'App',             // the application's namespace, in src/ (PSR-4)
        'handWritten' => ['Request/'],    // traits under src/BeanAccess/ written by hand: a file, or a directory
        // optional: 'sourceDirectory' => 'src', 'accessNamespace' => 'BeanAccess',
        //           'entityNamespace' => 'Doctrine\Entity' (null: none), 'lineLength' => 120 (PHPCS's)
    ],
];

'routes' => [
    'report/view' => [
        'pattern' => 'report/(?P<reportId>[1-9][0-9]*)',
        'controller' => 'ReportController',
    ],
],

// config/default.php: the folder the web server serves, and — when it is not `assets` — the address it serves it under
'assets' => ['directory' => dirname(__DIR__) . '/public/assets'],

// In a template or a view: <link rel="stylesheet" href="<?= $this->escape($assets->link($request, 'css/app.css')); 

class NotesTest extends ApplicationTestCase
{
    protected static function projectRoot(): string
    {
        return dirname(__DIR__, 2);
    }

    public function testANoteIsKept(): void
    {
        $this->post('notes', ['text' => 'Buy milk'])->assertRedirect('/notes');
        self::assertStringContainsString('Buy milk', $this->get('notes')->
sh
sh docker/ci                 # everything: dependencies, lint, PHPCS, PHP-CS-Fixer, PHPStan, the tests with the coverage,
                             # the mutation testing
sh docker/ci static          # all but the tests and the mutation testing
sh docker/phpcs              # PHP_CodeSniffer (phpcs.xml.dist, the standard applications extend); docker/phpcbf fixes
sh docker/php-cs-fixer       # PHP-CS-Fixer, dry run (sh docker/php-cs-fixer fix applies it)
sh docker/phpstan            # PHPStan at the maximum level
sh docker/phpunit            # PHPUnit; arguments go through (--testsuite unit, --filter …)
sh docker/infection          # Infection, the mutation testing (src/View/HttpView.php for one file)
sh docker/composer update    # Composer, with the network
sh docker/run php -v         # anything else in the container