pointybeard/helpers-foundation-factory
Composer 安装命令:
composer require pointybeard/helpers-foundation-factory
包简介
Provides foundation factory classes and factory design pattern functionality
README 文档
README
- Version: v1.0.4
- Date: June 05 2019
- Release notes
- GitHub repository
Provides foundation factory classes and factory design pattern functionality
Installation
This library is installed via Composer. To install, use composer require pointybeard/helpers-foundation-factory or add "pointybeard/helpers-foundation-factory": "~1.0.0" to your composer.json file.
And run composer to update your dependencies:
$ curl -s http://getcomposer.org/installer | php
$ php composer.phar update
Requirements
There are no particuar requirements for this library other than PHP 7.2 or greater.
To include all the PHP Helpers packages on your project, use composer require pointybeard/helpers
Usage
Include this library in your PHP files with use pointybeard\Helpers\Foundation\Factory and extend the Factory\AbstractFactory class like so:
<?php declare(strict_types=1); namespace MyApp; include __DIR__.'/vendor/autoload.php'; use pointybeard\Helpers\Foundation\Factory; interface VehicleInterface { public function name(): string; public function __toString(): string; } abstract class AbstractVehicle implements VehicleInterface { public function name(): string { return (new \ReflectionClass(static::class))->getShortName(); } } abstract class AbstractCar extends AbstractVehicle { public function __toString(): string { return "Hi! I'm a {$this->name()}."; } } abstract class AbstractTruck extends AbstractVehicle { public function __toString(): string { return "Hi! I'm a truck called {$this->name()}."; } } // Basic car class Volvo extends AbstractCar { } // Basic truck class Mack extends AbstractTruck { } // This adds a constructor which expects a model and vin number. They need // to be passed in when CarFactory::build() is called class Peugeot extends AbstractCar { private $model; private $vin; public function __construct(string $model, string $vin) { $this->model = $model; $this->vin = $vin; } public function __get(string $name) { return $this->$name; } public function __toString(): string { return parent::__toString()." I am a model {$this->model} with vehicle identification number {$this->vin}"; } } // a Cabbage is not a type of vehicle class Cabbage { } // Create a factory called MyApp\CarFactory Factory\create( __NAMESPACE__.'\\CarFactory', __NAMESPACE__.'\\%s', __NAMESPACE__.'\\AbstractCar' ); // Create a factory called MyApp\TruckFactory Factory\create( __NAMESPACE__.'\\TruckFactory', __NAMESPACE__.'\\%s', __NAMESPACE__.'\\AbstractTruck' ); // Create custom factory class class PeugeotFactory extends Factory\AbstractFactory { public function getTemplateNamespace(): string { return __NAMESPACE__.'\\%s'; } public function getExpectedClassType(): ?string { return __NAMESPACE__.'\\Peugeot'; } } $car = CarFactory::build('Volvo'); var_dump((string) $car); // string(16) "Hi! I'm a Volvo." $car = PeugeotFactory::build('Peugeot', '206', 'VF32HRHYF43242177'); var_dump((string) $car); // string(88) "Hi! I'm a Peugeot. I am a model 206 with vehicle identification number VF32HRHYF43242177" $truck = TruckFactory::build('Mack'); var_dump((string) $truck); // string(28) "Hi! I'm a truck called Mack." try { $car = PeugeotFactory::build('Volvo', 'XC60', 'YV1DZ8256C227123'); } catch (Factory\Exceptions\UnableToInstanciateConcreteClassException $ex) { echo 'Error: Cannot build a Volvo in the Peugeot factory!'.PHP_EOL; } // Error: Cannot build a Volvo in the Peugeot factory! try { CarFactory::build('Suzuki'); } catch (Factory\Exceptions\UnableToInstanciateConcreteClassException $ex) { echo 'Error: Unable to build a Suzuki. Returned: '.$ex->getMessage().PHP_EOL; } // Error: Unable to build a Suzuki. Returned: Class \MyApp\Suzuki does not exist try { CarFactory::build('Cabbage'); } catch (Factory\Exceptions\UnableToInstanciateConcreteClassException $ex) { echo 'Error: Unable to build a Cabbage. Returned: '.$ex->getMessage().PHP_EOL; } // Error: Unable to build a Cabbage. Returned: Class \MyApp\Cabbage is not // of expected type \MyApp\AbstractCar
Support
If you believe you have found a bug, please report it using the GitHub issue tracker, or better yet, fork the library and submit a pull request.
Contributing
We encourage you to contribute to this project. Please check out the Contributing documentation for guidelines about how to get involved.
License
"PHP Helpers: Factory Foundation Classes" is released under the MIT License.
pointybeard/helpers-foundation-factory 适用场景与选型建议
pointybeard/helpers-foundation-factory 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.15k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 05 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 pointybeard/helpers-foundation-factory 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 pointybeard/helpers-foundation-factory 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 4.15k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 13
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-05-20