@@ -36,85 +36,78 @@ public function extend($filePath)
3636 }
3737
3838 // Read file
39- $ bundlesFile = $ this ->readSchema ($ filePath );
39+ $ schema = $ this ->readSchema ($ filePath , true );
40+
41+ // Abort if no bundle is specified
42+ if ($ schema ['bundle ' ] === null ) {
43+ return ;
44+ }
4045
4146 // Process bundles
42- if ( isset ( $ bundlesFile -> bundle ) ) {
43- foreach ( $ bundlesFile -> bundle as $ bundleName => $ bundle ) {
44- // Get collision setting.
45- $ collisionRule = isset ( $ bundle-> options -> sprinkle -> onCollision ) ? $ bundle -> options -> sprinkle -> onCollision : 'replace ' ;
47+ foreach ( $ schema [ ' bundle ' ] as $ bundleName => $ _ ) {
48+
49+ // Get collision setting.
50+ $ collisionRule = $ schema [ " bundle. $ bundleName . options. sprinkle. onCollision" ] ? : 'replace ' ;
4651
47- // Handle CSS bundle if specified.
48- if (isset ($ bundle ->styles )) {
49- // Attempt to add CSS bundle
50- try {
51- $ standardisedBundle = $ this ->standardiseBundle ($ bundle ->styles );
52- if (!array_key_exists ($ bundleName , $ this ->cssBundles )) {
53- $ this ->cssBundles [$ bundleName ] = $ standardisedBundle ;
54- } else {
55- switch ($ collisionRule ) {
56- case 'replace ' :
57- // Replaces the existing bundle.
58- $ this ->cssBundles [$ bundleName ] = $ standardisedBundle ;
59- break ;
60- case 'merge ' :
61- // Merge with existing bundle.
62- foreach ($ standardisedBundle as $ assetPath ) {
63- if (!in_array ($ assetPath , $ this ->cssBundles [$ bundleName ])) {
64- $ this ->cssBundles [$ bundleName ][] = $ assetPath ;
65- }
66- }
67- break ;
68- case 'ignore ' :
69- break ;
70- case 'error ' :
71- throw new \ErrorException ("The bundle ' $ bundleName' is already defined. " );
72- break ;
73- default :
74- throw new \OutOfBoundsException ("Invalid value ' $ collisionRule' provided for 'onCollision' key in bundle ' $ bundleName'. " );
75- break ;
76- }
77- }
78- } catch (\Exception $ e ) {
79- throw new InvalidBundlesFileException ("Encountered issue processing styles property for ' $ bundleName' for file ' $ filePath' " , 0 , $ e );
80- }
52+ // Handle CSS bundle
53+ $ styles = $ schema ["bundle. $ bundleName.styles " ];
54+ if ($ styles !== null ) {
55+ // Attempt to add CSS bundle
56+ try {
57+ $ this ->addWithCollisionRule ($ styles , $ bundleName , $ collisionRule , $ this ->cssBundles );
58+ } catch (\Exception $ e ) {
59+ throw new InvalidBundlesFileException ("Encountered issue processing styles property for ' $ bundleName' for file ' $ filePath' " , 0 , $ e );
8160 }
61+ }
8262
83- // Handle JS bundle if specified.
84- if (isset ($ bundle ->scripts )) {
85- // Attempt to add JS bundle
86- try {
87- $ standardisedBundle = $ this ->standardiseBundle ($ bundle ->scripts );
88- if (!array_key_exists ($ bundleName , $ this ->jsBundles )) {
89- $ this ->jsBundles [$ bundleName ] = $ standardisedBundle ;
90- } else {
91- switch ($ collisionRule ) {
92- case 'replace ' :
93- // Replaces the existing bundle.
94- $ this ->jsBundles [$ bundleName ] = $ standardisedBundle ;
95- break ;
96- case 'merge ' :
97- // Merge with existing bundle.
98- foreach ($ standardisedBundle as $ assetPath ) {
99- if (!in_array ($ assetPath , $ this ->jsBundles [$ bundleName ])) {
100- $ this ->jsBundles [$ bundleName ][] = $ assetPath ;
101- }
102- }
103- break ;
104- case 'ignore ' :
105- break ;
106- case 'error ' :
107- throw new \ErrorException ("The bundle ' $ bundleName' is already defined. " );
108- break ;
109- default :
110- throw new \OutOfBoundsException ("Invalid value ' $ collisionRule' provided for 'onCollision' key in bundle ' $ bundleName'. " );
111- break ;
112- }
63+ // Handle JS bundle
64+ $ scripts = $ schema ["bundle. $ bundleName.scripts " ];
65+ if ($ scripts !== null ) {
66+ // Attempt to add JS bundle
67+ try {
68+ $ this ->addWithCollisionRule ($ scripts , $ bundleName , $ collisionRule , $ this ->jsBundles );
69+ } catch (\Exception $ e ) {
70+ throw new InvalidBundlesFileException ("Encountered issue processing scripts property for ' $ bundleName' for file ' $ filePath' " , 0 , $ e );
71+ }
72+ }
73+ }
74+ }
75+
76+ /**
77+ * Adds provided bundle to provided bundle store with collision rule respected.
78+ * @param string|string[] $bundle Bundle to add.
79+ * @param string $name Name of bundle provided.
80+ * @param string $collisionRule Rule to apply if collision is detected.
81+ * @param string[string][] $bundleStore Place to add bundles (CSS or JS depending on provided store).
82+ * @throws \ErrorException if collision rule is 'error' and bundle is already defined.
83+ * @throws \OutOfBoundsException if an invalid collision rule is provided.
84+ */
85+ protected function addWithCollisionRule (&$ bundle , $ bundleName , $ collisionRule , &$ bundleStore ) {
86+ $ standardisedBundle = $ this ->standardiseBundle ($ bundle );
87+ if (!array_key_exists ($ bundleName , $ bundleStore )) {
88+ $ bundleStore [$ bundleName ] = $ standardisedBundle ;
89+ } else {
90+ switch ($ collisionRule ) {
91+ case 'replace ' :
92+ // Replaces the existing bundle.
93+ $ bundleStore [$ bundleName ] = $ standardisedBundle ;
94+ break ;
95+ case 'merge ' :
96+ // Merge with existing bundle.
97+ foreach ($ standardisedBundle as $ assetPath ) {
98+ if (!in_array ($ assetPath , $ bundleStore [$ bundleName ])) {
99+ $ bundleStore [$ bundleName ][] = $ assetPath ;
113100 }
114- } catch (\Exception $ e ) {
115- throw new InvalidBundlesFileException ("Encountered issue processing scripts property for ' $ bundleName' for file ' $ filePath' " , 0 , $ e );
116101 }
117- }
102+ break ;
103+ case 'ignore ' :
104+ break ;
105+ case 'error ' :
106+ throw new \ErrorException ("The bundle ' $ bundleName' is already defined. " );
107+ break ;
108+ default :
109+ throw new \OutOfBoundsException ("Invalid value ' $ collisionRule' provided for 'onCollision' key in bundle ' $ bundleName'. " );
110+ break ;
118111 }
119112 }
120113 }
0 commit comments