gdbots / app-bundle
Symfony应用的App包。
Requires
- php: >=8.2
- symfony/console: ^6.4 || ^7.0
- symfony/framework-bundle: ^6.4 || ^7.0
- twig/twig: ^3.8
Requires (Dev)
- phpunit/phpunit: ^10.5
README
为symfony应用提供的App包,它提供了一个基础应用内核、设备视图和观看者国家感知。
AppKernel
提供了一个AppKernel接口和必须在你自己的应用中扩展的AbstractAppKernel
类。这个类提供了一些基本方法来描述内核运行的环境(云服务提供商、区域等)以及应用的详细信息,如供应商、包、版本、构建等。
控制台命令
当一个应用部署后,我们需要执行symfony命令和/或通过curl验证部署是否成功。控制台命令console app:describe
可以返回应用详细信息。
命令输出示例
{ "symfony_version": "5.0.0", "app_vendor": "acme", "app_name": "blog", "app_version": "v0.1.0", "app_build": "1487902285", "app_deployment_id": "d-IHMA71LSM", "app_dev_branch": "master", "system_mac_address": "02:a0:57:b4:59:e9", "cloud_provider": "private", "cloud_region": "us-west-2", "cloud_zone": "us-west-2a", "cloud_instance_id": "080027a450f9", "cloud_instance_type": "vbox", "kernel_environment": "local", "kernel_debug": true, "kernel_bundles": { "FrameworkBundle": "Symfony\\Bundle\\FrameworkBundle\\FrameworkBundle", "MonologBundle": "Symfony\\Bundle\\MonologBundle\\MonologBundle", "SecurityBundle": "Symfony\\Bundle\\SecurityBundle\\SecurityBundle", "TwigBundle": "Symfony\\Bundle\\TwigBundle\\TwigBundle", "SensioFrameworkExtraBundle": "Sensio\\Bundle\\FrameworkExtraBundle\\SensioFrameworkExtraBundle", "AwsBundle": "Aws\\Symfony\\AwsBundle", "GdbotsAppBundle": "Gdbots\\Bundle\\AppBundle\\GdbotsAppBundle", "AppBundle": "AppBundle\\AppBundle", "DebugBundle": "Symfony\\Bundle\\DebugBundle\\DebugBundle" } }
在CodeDeploy ValidateService钩子中的示例使用
if (( $( curl -s --resolve ${app_domain}:8080:127.0.0.1 http://${app_domain}:8080/health-check | grep -c "APP_DEPLOYMENT_ID = '${DEPLOYMENT_ID}'" ) > 0 )) then echo "[${APP_NAME}] validate success (curl)" else echo "[${APP_NAME}] validate failure (curl), does not have [APP_DEPLOYMENT_ID = '${DEPLOYMENT_ID}']" exit 1 fi app_deployment_id=`sudo -H -u ${APP_OWNER} php ${APP_DIR}/bin/console app:describe --env=${APP_ENV} --no-debug --no-interaction | jq -r '.app_deployment_id'` if [ "${app_deployment_id}" == "${DEPLOYMENT_ID}" ]; then echo "[${APP_NAME}] validate success (console)" else echo "[${APP_NAME}] validate failure (console), app returned '${app_deployment_id}', expected '${DEPLOYMENT_ID}'" exit 1 fi
设备视图感知
如果启用了设备检测并且已对当前请求进行了评估,则将识别要发送给用户的“视图”的字符串推送到服务器的一个环境变量中。
这个字符串完全由应用开发者决定,因为给用户的视图不一定与设备的形态完全匹配。
例如,智能电视可能会显示应用的“桌面”视图。
示例:桌面、智能手机、智能电视等。https://www.scientiamobile.com/wurflCapability
此包不提供任何检测,它只是将“决策”注入请求属性中,并提供了一些使模板解析变得简单的方法。实际的检测由更适合此的工具完成,如CloudFront。例如,在一个Apache重写规则中
RewriteCond %{HTTP:CloudFront-Is-Mobile-Viewer} =true
RewriteCond %{HTTP:CloudFront-Is-Tablet-Viewer} =false
RewriteRule ^ - [E=DEVICE_VIEW:smartphone]
"device_view"现在等于"smartphone",可以作为twig变量、请求属性或环境变量使用。在控制器中的示例使用
declare(strict_types=1); namespace AppBundle\Controller; use Gdbots\Bundle\AppBundle\Controller\DeviceViewRendererTrait; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Response; final class DefaultController extends Controller { use DeviceViewRendererTrait; /** * @Route("/") * * @return Response */ public function indexAction(): Response { // if device_view is populated and the template index.smartphone.html.twig exists, it will be used // otherwise the "index.html.twig" file will be loaded. return $this->renderUsingDeviceView('@app/index%device_view%.html.twig'); } }
使用Twig动态继承,你可以使用device_view
变量在可用时提供特定于设备的布局。
{% extends ['layout.' ~ device_view ~ '.twig.html', 'layout.twig.html'] %} I'm on a {{ device_view }}.
观看者国家感知
使用与设备视图相同的策略,此值包含观看者的国家。此字符串应是一个两位ISO国家代码,全部为大写或null。
此包不提供任何检测,它只是将“决策”注入请求属性中,并提供了一些使模板解析变得简单的方法。实际的检测由更适合此的工具完成,如CloudFront。例如,在一个Apache重写规则中
RewriteCond %{HTTP:CloudFront-Viewer-Country} ([A-Z0-9]{2})
RewriteRule ^ - [E=VIEWER_COUNTRY:%1]
你可以在Symfony请求中使用$request->attributes->get('viewer_country')
或在使用twig时使用I'm in {{ viewer_country }}.