Generics
1 year ago
Props
1 year ago
BlockStyleRender.php
1 year ago
DynamicStyles.php
1 year ago
GlobalStyleRender.php
1 year ago
MainAttribute.php
4 years ago
ParserUtils.php
1 year ago
StyleManager.php
2 years ago
StyleParser.php
1 year ago
StyleRender.php
1 year ago
Utils.php
1 year ago
StyleManager.php
88 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kubio\Core\StyleManager; |
| 4 | |
| 5 | use Kubio\Config; |
| 6 | use Kubio\Core\LodashBasic; |
| 7 | use Kubio\Core\Utils; |
| 8 | |
| 9 | use function array_merge; |
| 10 | #[\AllowDynamicProperties] |
| 11 | class StyleManager { |
| 12 | |
| 13 | private static $instance; |
| 14 | private $styles = array( |
| 15 | 'shared' => array(), |
| 16 | 'local' => array(), |
| 17 | 'dynamic' => array(), |
| 18 | 'global' => array(), |
| 19 | ); |
| 20 | |
| 21 | private $style_join = array( |
| 22 | 'new_line' => '', |
| 23 | 'tab' => '', |
| 24 | ); |
| 25 | |
| 26 | public function __construct() { |
| 27 | if ( Utils::isDebug() ) { |
| 28 | $this->style_join = array( |
| 29 | 'new_line' => "\n", |
| 30 | 'tab' => "\t", |
| 31 | ); |
| 32 | |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | static function getInstance() { |
| 37 | if ( ! self::$instance ) { |
| 38 | self::$instance = new self(); |
| 39 | } |
| 40 | |
| 41 | return self::$instance; |
| 42 | } |
| 43 | |
| 44 | function registerBlockStyle( $styleByType ) { |
| 45 | foreach ( $styleByType as $type => $styleByMedia ) { |
| 46 | foreach ( $styleByMedia as $media => $styles ) { |
| 47 | $path = array( $type, $media ); |
| 48 | LodashBasic::set( $this->styles, $path, array_merge( LodashBasic::get( $this->styles, $path, array() ), $styles ) ); |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | public function render() { |
| 54 | $renderByMedia = array(); |
| 55 | foreach ( $this->styles as $styleByMedia ) { |
| 56 | foreach ( $styleByMedia as $media => $styles ) { |
| 57 | if ( ! isset( $renderByMedia[ $media ] ) ) { |
| 58 | $renderByMedia[ $media ] = array(); |
| 59 | } |
| 60 | $renderByMedia[ $media ] = array_merge( $renderByMedia[ $media ], $styles ); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | $render = ''; |
| 65 | |
| 66 | $devices = LodashBasic::mapValues( Config::value( 'medias' ), 'id' ); |
| 67 | $mediasById = Config::mediasById(); |
| 68 | |
| 69 | $device_rules = array(); |
| 70 | |
| 71 | foreach ( $devices as $device ) { |
| 72 | if ( isset( $renderByMedia[ $device ] ) ) { |
| 73 | $query = LodashBasic::get( $mediasById, array( $device, 'query' ), false ); |
| 74 | $rules = join( $this->style_join['new_line'], $renderByMedia[ $device ] ); |
| 75 | if ( $query ) { |
| 76 | $device_rules[] = $query . "{$this->style_join['tab']}{{$this->style_join['new_line']}{$rules}{$this->style_join['new_line']}}"; |
| 77 | } else { |
| 78 | $device_rules[] = $rules; |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | $render .= join( '', $device_rules ); |
| 84 | |
| 85 | return $render; |
| 86 | } |
| 87 | } |
| 88 |