@@ -3060,7 +3060,7 @@ public function addOverride($classname)
30603060 throw new Exception (Context::getContext ()->getTranslator ()->trans ('The constant %1$s in the class %2$s is already defined. ' , [$ constant , $ classname ], 'Admin.Modules.Notification ' ));
30613061 }
30623062
3063- $ module_file = preg_replace ('/(const\s)\s*(\b ' . $ constant . '\b)/ism ' , "/* \n * module: " . $ this ->name . "\n * date: " . date ('Y-m-d H:i:s ' ) . "\n * version: " . $ this ->version . "\n */ \n $1$2 " , $ module_file );
3063+ $ module_file = preg_replace ('/((?:public|private|protected)\s+)?(?:static\s+)?( const\s)\s*(\b ' . $ constant . '\b)/ism ' , "/* \n * module: " . $ this ->name . "\n * date: " . date ('Y-m-d H:i:s ' ) . "\n * version: " . $ this ->version . "\n */ \n $1$2$3 " , $ module_file );
30643064 if ($ module_file === null ) {
30653065 throw new Exception (Context::getContext ()->getTranslator ()->trans ('Failed to override constant %1$s in class %2$s. ' , [$ constant , $ classname ], 'Admin.Modules.Notification ' ));
30663066 }
@@ -3124,7 +3124,7 @@ public function addOverride($classname)
31243124
31253125 // Same loop for constants
31263126 foreach ($ module_class ->getConstants () as $ constant => $ value ) {
3127- $ module_file = preg_replace ('/(const\s)\s*(\b ' . $ constant . '\b)/ism ' , "/* \n * module: " . $ this ->name . "\n * date: " . date ('Y-m-d H:i:s ' ) . "\n * version: " . $ this ->version . "\n */ \n $1$2 " , $ module_file );
3127+ $ module_file = preg_replace ('/((?:public|private|protected)\s+)?(?:static\s+)?( const\s)\s*(\b ' . $ constant . '\b)/ism ' , "/* \n * module: " . $ this ->name . "\n * date: " . date ('Y-m-d H:i:s ' ) . "\n * version: " . $ this ->version . "\n */ \n $1$2$3 " , $ module_file );
31283128 if ($ module_file === null ) {
31293129 throw new Exception (Context::getContext ()->getTranslator ()->trans ('Failed to override constant %1$s in class %2$s. ' , [$ constant , $ classname ], 'Admin.Modules.Notification ' ));
31303130 }
@@ -3281,34 +3281,64 @@ public function removeOverride($classname)
32813281 continue ;
32823282 }
32833283
3284- // Replace the declaration line by #--remove--#
3284+ // Replace all declaration lines by #--remove--#, tracking bracket depth
3285+ // to handle multi-line property values (e.g. arrays spanning multiple lines)
3286+ $ inside_property = false ;
3287+ $ bracket_depth = 0 ;
32853288 foreach ($ override_file as $ line_number => &$ line_content ) {
3286- if (preg_match ('/(public|private|protected)\s+(static\s+)?\s*(\w+\s+)?(\$)? ' . $ property ->getName () . '/i ' , $ line_content )) {
3287- if (preg_match ('/\* module: ( ' . $ this ->name . ')/ism ' , $ override_file [$ line_number - 4 ])) {
3288- $ override_file [$ line_number - 5 ] = $ override_file [$ line_number - 4 ] = $ override_file [$ line_number - 3 ] = $ override_file [$ line_number - 2 ] = $ override_file [$ line_number - 1 ] = '#--remove--# ' ;
3289+ if (!$ inside_property ) {
3290+ if (preg_match ('/(public|private|protected)\s+(static\s+)?\s*(\w+\s+)?(\$)? ' . $ property ->getName () . '/i ' , $ line_content )) {
3291+ if (preg_match ('/\* module: ( ' . $ this ->name . ')/ism ' , $ override_file [$ line_number - 4 ])) {
3292+ $ override_file [$ line_number - 5 ] = $ override_file [$ line_number - 4 ] = $ override_file [$ line_number - 3 ] = $ override_file [$ line_number - 2 ] = $ override_file [$ line_number - 1 ] = '#--remove--# ' ;
3293+ }
3294+ $ inside_property = true ;
3295+ $ bracket_depth = 0 ;
32893296 }
3297+ }
3298+
3299+ if ($ inside_property ) {
3300+ $ bracket_depth += substr_count ($ line_content , '( ' ) - substr_count ($ line_content , ') ' );
3301+ $ bracket_depth += substr_count ($ line_content , '[ ' ) - substr_count ($ line_content , '] ' );
3302+ $ is_end = ($ bracket_depth <= 0 && false !== strpos ($ line_content , '; ' ));
32903303 $ line_content = '#--remove--# ' ;
32913304
3292- break ;
3305+ if ($ is_end ) {
3306+ break ;
3307+ }
32933308 }
32943309 }
32953310 }
32963311
3297- // Remove properties from override file
3312+ // Remove constants from override file
32983313 foreach ($ module_class ->getConstants () as $ constant => $ value ) {
32993314 if (!$ override_class ->hasConstant ($ constant )) {
33003315 continue ;
33013316 }
33023317
3303- // Replace the declaration line by #--remove--#
3318+ // Replace all declaration lines by #--remove--#, tracking bracket depth
3319+ // to handle multi-line constant values (e.g. arrays spanning multiple lines)
3320+ $ inside_constant = false ;
3321+ $ bracket_depth = 0 ;
33043322 foreach ($ override_file as $ line_number => &$ line_content ) {
3305- if (preg_match ('/(const)\s+(static\s+)?(\$)? ' . $ constant . '/i ' , $ line_content )) {
3306- if (preg_match ('/\* module: ( ' . $ this ->name . ')/ism ' , $ override_file [$ line_number - 4 ])) {
3307- $ override_file [$ line_number - 5 ] = $ override_file [$ line_number - 4 ] = $ override_file [$ line_number - 3 ] = $ override_file [$ line_number - 2 ] = $ override_file [$ line_number - 1 ] = '#--remove--# ' ;
3323+ if (!$ inside_constant ) {
3324+ if (preg_match ('/(const)\s+(static\s+)?(\$)? ' . $ constant . '/i ' , $ line_content )) {
3325+ if (preg_match ('/\* module: ( ' . $ this ->name . ')/ism ' , $ override_file [$ line_number - 4 ])) {
3326+ $ override_file [$ line_number - 5 ] = $ override_file [$ line_number - 4 ] = $ override_file [$ line_number - 3 ] = $ override_file [$ line_number - 2 ] = $ override_file [$ line_number - 1 ] = '#--remove--# ' ;
3327+ }
3328+ $ inside_constant = true ;
3329+ $ bracket_depth = 0 ;
33083330 }
3331+ }
3332+
3333+ if ($ inside_constant ) {
3334+ $ bracket_depth += substr_count ($ line_content , '( ' ) - substr_count ($ line_content , ') ' );
3335+ $ bracket_depth += substr_count ($ line_content , '[ ' ) - substr_count ($ line_content , '] ' );
3336+ $ is_end = ($ bracket_depth <= 0 && false !== strpos ($ line_content , '; ' ));
33093337 $ line_content = '#--remove--# ' ;
33103338
3311- break ;
3339+ if ($ is_end ) {
3340+ break ;
3341+ }
33123342 }
33133343 }
33143344 }
0 commit comments