OTGS_Assets_Handles.php
1 month ago
OTGS_Assets_Store.php
1 month ago
OTGS_UI_Assets.php
1 month ago
OTGS_UI_Loader.php
1 month ago
OTGS_Assets_Store.php
70 lines
| 1 | <?php |
| 2 | |
| 3 | class OTGS_Assets_Store { |
| 4 | |
| 5 | private $assets_files_store = array(); |
| 6 | private $assets = array(); |
| 7 | |
| 8 | public function get( $type, $handle = null ) { |
| 9 | $result = array(); |
| 10 | |
| 11 | $this->parse_assets(); |
| 12 | |
| 13 | if ( array_key_exists( $type, $this->assets ) ) { |
| 14 | $result = $this->assets[ $type ]; |
| 15 | |
| 16 | if ( $handle ) { |
| 17 | if ( array_key_exists( $handle, $this->assets[ $type ] ) ) { |
| 18 | $result = $this->assets[ $type ][ $handle ]; |
| 19 | } else { |
| 20 | $result = array(); |
| 21 | } |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | return $result; |
| 26 | } |
| 27 | |
| 28 | public function add_assets_location( $path ) { |
| 29 | if ( ! in_array( $path, $this->assets, true ) ) { |
| 30 | $this->assets_files_store[] = $path; |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | private function parse_assets() { |
| 35 | if ( ! $this->assets ) { |
| 36 | foreach ( $this->assets_files_store as $assets_file ) { |
| 37 | $this->add_asset( $assets_file ); |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | private function add_asset( $assets_file ) { |
| 43 | if ( ! is_file( $assets_file ) ) { |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | $assets = file_get_contents( $assets_file ); |
| 48 | if ( ! $assets || ! is_string( $assets ) ) { |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | $assets_data = json_decode( $assets, true ); |
| 53 | |
| 54 | if ( $assets_data && array_key_exists( 'entrypoints', $assets_data ) ) { |
| 55 | foreach ( $assets_data['entrypoints'] as $handle => $resources ) { |
| 56 | $this->add_resources( $handle, $resources ); |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | private function add_resources( $handle, $resources ) { |
| 62 | foreach ( $resources as $type => $path ) { |
| 63 | if ( ! array_key_exists( $type, $this->assets ) ) { |
| 64 | $this->assets[ $type ] = array(); |
| 65 | } |
| 66 | $this->assets[ $type ][ $handle ] = $path; |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 |