api.manifestator.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /**
  3. * Implements a manifest generator for a web application
  4. */
  5. class Manifestator {
  6. /**
  7. * application name
  8. *
  9. * @var string
  10. */
  11. protected $name = 'AppName';
  12. /**
  13. * short application name
  14. *
  15. * @var string
  16. */
  17. protected $shortName = 'ShortName';
  18. /**
  19. * The start URL
  20. *
  21. * @var string
  22. */
  23. protected $startUrl = '.';
  24. /**
  25. * The display mode
  26. *
  27. * @var string
  28. */
  29. protected $display = 'standalone';
  30. /**
  31. * The theme color
  32. *
  33. * @var string
  34. */
  35. protected $themeColor = 'ffffff';
  36. /**
  37. * The background color
  38. *
  39. * @var string
  40. */
  41. protected $backgroundColor = 'ffffff';
  42. /**
  43. * The icons set
  44. *
  45. * @var array
  46. */
  47. protected $icons = array();
  48. /**
  49. * Contains appended custom data
  50. *
  51. * @var array
  52. */
  53. protected $appendData = array();
  54. /**
  55. * Constructor method.
  56. *
  57. * Initializes the Manifestator object and sets default icons.
  58. */
  59. public function __construct() {
  60. $this->setDefaultIcons();
  61. }
  62. /**
  63. * Sets the default icons for the web application.
  64. *
  65. * @return void
  66. */
  67. protected function setDefaultIcons() {
  68. $defaultIcons = array(
  69. 0 => array('src' => 'skins/webapp/wa192.png', 'sizes' => '192x192', 'type' => 'image/png'),
  70. 1 => array('src' => 'skins/webapp/wa512.png', 'sizes' => '512x512', 'type' => 'image/png')
  71. );
  72. $this->setIcons($defaultIcons);
  73. }
  74. /**
  75. * Sets the name of the web application.
  76. *
  77. * @param string $name The name of the web application.
  78. *
  79. * @return void
  80. */
  81. public function setName($name) {
  82. $this->name = $name;
  83. }
  84. /**
  85. * Sets the shortName of the web application.
  86. *
  87. * @param string $name The name of the web application.
  88. *
  89. * @return void
  90. */
  91. public function setShortName($name) {
  92. $this->shortName = $name;
  93. }
  94. /**
  95. * Sets the start URL of the web application.
  96. *
  97. * @param string $startUrl The start URL of the web application.
  98. *
  99. * @return void
  100. */
  101. public function setStartUrl($startUrl) {
  102. $this->startUrl = $startUrl;
  103. }
  104. /**
  105. * Sets the display mode of the web application.
  106. *
  107. * @param string $display The display mode of the web application. fullscreen|standalone|minimal-ui|browser
  108. *
  109. * @return void
  110. */
  111. public function setDisplay($display) {
  112. $this->display = $display;
  113. }
  114. /**
  115. * Sets the theme color of the web application.
  116. *
  117. * @param string $themeColor The theme color of the web application.
  118. *
  119. * @return void
  120. */
  121. public function setThemeColor($themeColor) {
  122. $this->themeColor = $themeColor;
  123. }
  124. /**
  125. * Sets the background color of the web application.
  126. *
  127. * @param string $backgroundColor The background color of the web application.
  128. *
  129. * @return void
  130. */
  131. public function setBackgroundColor($backgroundColor) {
  132. $this->backgroundColor = $backgroundColor;
  133. }
  134. /**
  135. * Sets the icons set of the web application.
  136. *
  137. * @param array $icons An array of icons for the web application.
  138. *
  139. * @return void
  140. */
  141. public function setIcons($icons) {
  142. $this->icons = $icons;
  143. }
  144. /**
  145. * Sets the custom data to be appended to the manifest.
  146. *
  147. * @param array $customData An array of custom data to be appended.
  148. *
  149. * @return void
  150. */
  151. public function setAppendData($customData = array()) {
  152. $this->appendData = $customData;
  153. }
  154. /**
  155. * Returns application manifest array
  156. *
  157. * @return array
  158. */
  159. protected function getManifest() {
  160. $result = array(
  161. 'name' => $this->name,
  162. 'short_name' => $this->shortName,
  163. 'start_url' => $this->startUrl,
  164. 'display' => $this->display,
  165. 'theme_color' => $this->themeColor,
  166. 'background_color' => $this->backgroundColor,
  167. 'icons' => $this->icons
  168. );
  169. if (!empty($this->appendData)) {
  170. $result+=$this->appendData;
  171. }
  172. return ($result);
  173. }
  174. /**
  175. * Renders the manifest data as JSON and sends it as the response.
  176. *
  177. * @return void
  178. */
  179. public function render() {
  180. $manifest = $this->getManifest();
  181. $jsonData = json_encode($manifest);
  182. header('Content-Type: application/json');
  183. die($jsonData);
  184. }
  185. }