Component entry

Each swewt component must have a component autoloader class AutoLoader

Notice: The component's Autoloader must inherit from Swoft\SwoftComponent in order to be correctly identified by swowt as a component.

AutoLoader

 <?php declare(strict_types=1);

namespace Your\Component;

use Swoft\Helper\ComposerJSON;
use Swoft\SwoftComponent;
use function dirname;

/**
 * class AutoLoader
 * @since 2.0
 */
final class AutoLoader extends SwoftComponent
{
    /**
     * @return bool
     */
    public function enable(): bool
    {
        return true;
    }

    /**
     * Get namespace and dirs
     *
     * @return array
     */
    public function getPrefixDirs(): array
    {
        return [
            __NAMESPACE__ => __DIR__,
        ];
    }

    /**
     * Metadata information for the component
     *
     * @return array
     */
    public function metadata(): array
    {
        $jsonFile = dirname(__DIR__) . '/composer.json';

        return ComposerJSON::open($jsonFile)->getMetadata();
    }

    /**
     * {@inheritDoc}
     */
    public function beans(): array
    {
        return [
            'myBean'    => [
                'class' => MySome::class,
            ],
        ];
    }
} 

Method description

  • enable(): bool Whether enable(): bool enables this component
  • getPrefixDirs(): array This component you need to scan those directories, you can specify to scan only part of the directory.
  • metadata(): array Lists the basic information of the component. You can return the information in composer.json directly in the above way.
  • beans(): array you can configure to add custom beans if needed

Tips: Our application project swoft/swoft structure is similar, can be seen as a top-level component, loaded configuration is app/AutoLoader.php .

/docs/2.x/en/component/entry.html
progress-bar