lukaswhite/manifest

用于构建Web应用程序清单的PHP库

dev-master 2018-12-23 07:15 UTC

This package is auto-updated.

Last update: 2019-12-23 09:37:20 UTC


README

这是一个简单的PHP库,用于通过编程方式生成Web应用程序清单

简单示例

$manifest = new Manifest( );
$manifest->name( 'My Awesome App' )
    ->shortName( 'AwesomeApp' )
    ->description( 'Just testing' );
    
print $manifest->toJson( );
// or
$manifest->save( 'public/manifest.json' );    

在Laravel中

Route::get( 'manifest.json', function( ) {
    $manifest = new Manifest( );
    $manifest->name( Config::get( 'app.name' ) )
        ->description( trans( 'site.meta.description' ) );
        
    return response( )->json( $manifest );
} );

更长的示例

以下示例来自Mozilla关于Web应用程序清单的文档

{
  "name": "HackerWeb",
  "short_name": "HackerWeb",
  "start_url": ".",
  "display": "standalone",
  "background_color": "#fff",
  "description": "A simply readable Hacker News app.",
  "icons": [{
    "src": "images/touch/homescreen48.png",
    "sizes": "48x48",
    "type": "image/png"
  }, {
    "src": "images/touch/homescreen72.png",
    "sizes": "72x72",
    "type": "image/png"
  }, {
    "src": "images/touch/homescreen96.png",
    "sizes": "96x96",
    "type": "image/png"
  }, {
    "src": "images/touch/homescreen144.png",
    "sizes": "144x144",
    "type": "image/png"
  }, {
    "src": "images/touch/homescreen168.png",
    "sizes": "168x168",
    "type": "image/png"
  }, {
    "src": "images/touch/homescreen192.png",
    "sizes": "192x192",
    "type": "image/png"
  }],
  "related_applications": [{
    "platform": "play",
    "url": "https://play.google.com/store/apps/details?id=cheeaun.hackerweb"
  }]
}

以下是使用此库构建该清单的方法

$manifest = new Manifest( );
$manifest
    ->name( 'HackerWeb' )
    ->shortName( 'HackerWeb' )
    ->description( 'A simply readable Hacker News app.' )
    ->startUrl( '.' )
    ->standalone( )
    ->backgroundColor( '#fff' )
    ->icons(
        [
            new Icon( 'images/touch/homescreen48.png', 48, 'image/png' ),
            new Icon( 'images/touch/homescreen72.png', 72, 'image/png' ),
            new Icon( 'images/touch/homescreen96.png', 96, 'image/png' ),
            new Icon( 'images/touch/homescreen144.png', 144, 'image/png' ),
            new Icon( 'images/touch/homescreen168.png', 168, 'image/png' ),
            new Icon( 'images/touch/homescreen192.png', 192, 'image/png' ),
        ]
    )
    ->addRelatedApplication(
        new RelatedApplication(
            'play', 
            'https://play.google.com/store/apps/details?id=cheeaun.hackerweb'
        )
    );

但是...为什么?

我创建这个库是因为我正在开发一个旨在以不同配置多次部署的应用程序;例如,应用程序名称在多个安装中可能不同,因此从代码库中控制清单内容是有意义的。

假设你正在构建一个由CMS驱动的应用程序;从元数据(添加到主屏幕时显示的名称和描述)到颜色方案,几乎所有东西都可能是由数据库驱动的;这让你可以做到这一点。

还有许多其他原因你可能想使用这种方法

  • 个人偏好;也许你喜欢手动编写JSON文件,也许你更喜欢编程方法
  • 它有助于消除重复;假设诸如应用程序名称等信息在配置或数据库中——这允许你避免重复这些信息
  • 某些属性确实属于配置;假设你需要设置Google Cloud Messaging的发送者ID。将其保留在配置中是合理的,然后你可以根据需要将其注入到清单中。
  • i18n;想将你的应用程序描述翻译成多种语言?你可以使用这种方法。
  • 你可以将其集成到部署流程中;如果你正在自动化,例如,应用程序图标生成(谁想手动进行缩放?),这种方法使自动化更容易。

后续将提供更多文档