Skip to content

Commit

Permalink
Add service and entry script docs
Browse files Browse the repository at this point in the history
  • Loading branch information
samdark committed Oct 26, 2019
1 parent 7f0b375 commit 6e5a3e6
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 9 deletions.
18 changes: 9 additions & 9 deletions guide/en/README.md
Expand Up @@ -26,17 +26,17 @@ Application Structure
---------------------

* [Application Structure Overview](structure/overview.md)
* [Entry Scripts](structure/entry-scripts.md)
* [Applications](structure/applications.md)
* [Services](structure/services.md)
* [Controllers](structure/controllers.md)
* [Entry Scripts](structure/entry-script.md)
* [Applications](structure/application.md)
* [Service components](structure/service.md)
* [Actions](structure/action.md)
* [Domain](structure/domain.md)
* [Views](structure/views.md)
* [Modules](structure/modules.md)
* [Views](structure/view.md)
* [Modules](structure/module.md)
* [Middleware](structure/middleware.md)
* [Widgets](structure/widgets.md)
* [Assets](structure/assets.md)
* [Extensions](structure/extensions.md)
* [Widgets](structure/widget.md)
* [Assets](structure/asset.md)
* [Extensions](structure/extension.md)


Handling Requests
Expand Down
93 changes: 93 additions & 0 deletions guide/en/structure/entry-script.md
@@ -0,0 +1,93 @@
# Entry Scripts

Entry scripts are the first step in the application bootstrapping process. An application (either
Web application or console application) has a single entry script. End users make requests to
entry scripts which instantiate application instances and forward the requests to them.

Entry scripts for Web applications must be stored under Web accessible directories so that they
can be accessed by end users. They are often named as `index.php`, but can also use any other names,
provided Web servers can locate them.

Entry script for console application is `./vendor/bin/yii`.

Entry scripts mainly do the following work:


* Register [Composer autoloader](https://getcomposer.org/doc/01-basic-usage.md#autoloading);
* Obtain configuration;
* Use configuration to initialize dependency injection container;
* Call `Application::run()` to process the incoming request.


## Web Applications <span id="web-applications"></span>

The following is the code in the entry script for the application template:

```php
<?php
use hiqdev\composer\config\Builder;
use Yiisoft\Di\Container;
use Yiisoft\Yii\Web\Application;
use Yiisoft\Yii\Web\ServerRequestFactory;

require_once dirname(__DIR__) . '/vendor/autoload.php';

// Don't do it in production, assembling takes it's time
Builder::rebuild();

$container = new Container(require Builder::path('web'));

$request = $container->get(ServerRequestFactory::class)->createFromGlobals();
$container->get(Application::class)->handle($request);
```


## Console Applications <span id="console-applications"></span>

Similarly, the following is the code for the entry script of a console application:

```php
#!/usr/bin/env php
<?php

use hiqdev\composer\config\Builder;
use Yiisoft\Di\Container;
use Yiisoft\Yii\Console\Application;

(static function () {
$cwd = getcwd();

$possibleAutoloadPaths = [
// running from project root
$cwd . '/vendor/autoload.php',
// running from project bin
dirname($cwd) . '/autoload.php',
// local dev repository
dirname(__DIR__) . '/vendor/autoload.php',
// dependency
dirname(__DIR__, 4) . '/vendor/autoload.php',
];
$autoloadPath = null;
foreach ($possibleAutoloadPaths as $possibleAutoloadPath) {
if (file_exists($possibleAutoloadPath)) {
$autoloadPath = $possibleAutoloadPath;
break;
}
}

if ($autoloadPath === null) {
$message = "Unable to find vendor/autoload.php in the following paths:\n\n";
$message .= '- ' . implode("\n- ", $possibleAutoloadPaths) . "\n\n";
$message .= "Possible fixes:\n";
$message .= "- Install yiisoft/console via Composer.\n";
$message .= "- Run ./yii either from project root or from vendor/bin.\n";
fwrite(STDERR, $message);
exit(1);
}
require_once $autoloadPath;

$container = new Container(require Builder::path('console'));

$container->get(Application::class)->run();
})();
```
28 changes: 28 additions & 0 deletions guide/en/structure/service.md
@@ -0,0 +1,28 @@
# Service components

Application may get complicated so it makes sense to extract focused parts of business logic
or infrastructure into service components. These components are typically instantiated
once and put into dependency injection container.

These components are typically accessed either from other components or from controller.
It is typically done via autowiring:

```php
public function actionIndex(ServerRequestInterface $request, MyService $myService): ResponseInterface
{
$id = $request->getAttribute('id');

// ...
$extraData = $myService->getExtraData($id);

// ...
}
```

Note that in many cases it makes sense to choose more specific class to place your code into.
Check:

- Repository
- Widget
- Middleware
- Entity

0 comments on commit 6e5a3e6

Please sign in to comment.