PluginProbe
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin / 1.1.8
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin v1.1.8
1.1.9 1.1.8 1.1.7 1.1.6 1.1.5 1.1.4 1.1.3 1.1.2 1.1.1 1.1.0 1.0.1 1.0.0 0.9.8 0.9.7 0.9.6 0.9.4 0.9.5 0.9.3 0.9.2 0.9.1 0.9.0 0.8.9 0.8.8 0.8.7 0.8.6 All 33 releases
desktop-mode / includes / framework / app / class-registry.php

class-registry.php in OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin 1.1.8, at includes/framework/app/class-registry.php

159 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * OpenStation App Framework — app registry and `.os.php` loader.
4 *
5 * Holds every defined `App` by id and knows how to find them on
6 * disk: an `.os.php` file is a PHP file that `return`s an `App`.
7 * Loading a directory picks up the `.os.php` files directly inside
8 * it and those one folder down, so an app can be a single file or a
9 * folder with its stylesheet and helpers beside it.
10 *
11 * @package OpenStation
12 */
13
14 namespace OpenStation\App;
15
16 use OpenStation\App;
17
18 // Direct access, unless a standalone host is booting on bare PHP.
19 if ( ! defined( 'ABSPATH' ) ) {
20 defined( 'OPENSTATION_STANDALONE' ) || exit;
21 }
22
23 /**
24 * App registry + `.os.php` loader.
25 */
26 final class Registry {
27
28 const FILE_SUFFIX = '.os.php';
29
30 /**
31 * @var array<string,App>
32 */
33 private $apps = array();
34
35 /**
36 * Real paths already included, so a file is never evaluated twice.
37 *
38 * @var array<string,true>
39 */
40 private $loaded = array();
41
42 /**
43 * Register an app. A second app with the same id replaces the first.
44 *
45 * @param App $app App definition.
46 * @return App The same app, for chaining.
47 */
48 public function add( App $app ) {
49 $this->apps[ $app->id() ] = $app;
50 return $app;
51 }
52
53 /**
54 * Forget an app.
55 *
56 * @param string $id App id.
57 * @return void
58 */
59 public function remove( $id ) {
60 unset( $this->apps[ (string) $id ] );
61 }
62
63 /**
64 * Look an app up.
65 *
66 * @param string $id App id.
67 * @return App|null
68 */
69 public function get( $id ) {
70 return isset( $this->apps[ (string) $id ] ) ? $this->apps[ (string) $id ] : null;
71 }
72
73 /**
74 * Whether an app is registered.
75 *
76 * @param string $id App id.
77 * @return bool
78 */
79 public function has( $id ) {
80 return isset( $this->apps[ (string) $id ] );
81 }
82
83 /**
84 * Every registered app, keyed by id.
85 *
86 * @return array<string,App>
87 */
88 public function all() {
89 return $this->apps;
90 }
91
92 /**
93 * Evaluate one `.os.php` file and register the app it returns.
94 *
95 * @param string $path Absolute file path.
96 * @return App|null The app, or null when the file returned none.
97 */
98 public function load_file( $path ) {
99 $real = realpath( (string) $path );
100 if ( false === $real || ! is_file( $real ) ) {
101 return null;
102 }
103 if ( isset( $this->loaded[ $real ] ) ) {
104 return $this->find_by_file( $real );
105 }
106 $this->loaded[ $real ] = true;
107
108 $result = include $real;
109 if ( ! $result instanceof App ) {
110 return null;
111 }
112 $result->located_at( dirname( $real ), $real );
113 return $this->add( $result );
114 }
115
116 /**
117 * Load every `.os.php` under a directory (one level of
118 * sub-folders deep), alphabetical.
119 *
120 * @param string $dir Absolute directory path.
121 * @return App[] Apps loaded from this call.
122 */
123 public function load_dir( $dir ) {
124 $dir = rtrim( (string) $dir, '/\\' );
125 if ( '' === $dir || ! is_dir( $dir ) ) {
126 return array();
127 }
128 $files = array_merge(
129 (array) glob( $dir . '/*' . self::FILE_SUFFIX ),
130 (array) glob( $dir . '/*/*' . self::FILE_SUFFIX )
131 );
132 sort( $files );
133
134 $apps = array();
135 foreach ( $files as $file ) {
136 $app = $this->load_file( $file );
137 if ( $app ) {
138 $apps[] = $app;
139 }
140 }
141 return $apps;
142 }
143
144 /**
145 * The app that was loaded from a given file, if any.
146 *
147 * @param string $real Real path.
148 * @return App|null
149 */
150 private function find_by_file( $real ) {
151 foreach ( $this->apps as $app ) {
152 if ( $app->file() === $real ) {
153 return $app;
154 }
155 }
156 return null;
157 }
158 }
159