I'm writing a function, as a learning excercise, to combine and minify css files and php files that output generated css.
Everything is fine looping through the css files but as soon it tries to push the string returned from the output buffer from the php file the array becomes an int. var_dump() yields this:
int(5)
I've also tried concatenating the strings; it works fine again until it get's to the php file, then everything previous in the string becomes 4. Like so:
4/*
* Home: Appointment Grid
*/
.difAppointmentGrid {
margin: 2rem 0;
font-family: "Lato" !important;
box-shadow: -4px 4px 16px 4px hsla( 240, 0%, 0%, 0.1 );
}
. . .
This is an example of what I'm doing in the styles.php file:
. . .
.difAppointmentGrid header div h3 {
margin: 0;
padding: 1.5rem 0;
font-size: 2rem;
text-align: center;
color: white;
}
<?php
for ( $h3 = 1, $o = 0.40; $h3 <= 4; ++$h3, $o += 0.20 )
{
$rule = '.difAppointmentGrid header div:nth-child('.$h3.') h3 {'."\n\t".
'background-color: hsla( 223, 63%, 22%, '.$o.' );'."\n".
'}'."\n";
echo $rule;
}
?>
.dif_grid {
display: flex;
}
. . .
This is the function:
function styles_init()
{
$path = __DIR__ . '/aggregate.min.css';
if ( is_writable( $path ) )
{
$r = array();
foreach( array_filter( glob( __DIR__ . '/modules/*.*' ), 'is_file' ) as $file )
{
$fn = pathinfo( $file );
if ( $fn['extension'] == 'php' || $fn['extension'] == 'css' )
{
ob_start();
include( $file );
$x = ob_get_flush();
$r[] = $x;
}
}
$c = implode( "\n\n", $r );
//$c = str_replace( array( ' {', ': ', ', ' ), array( '{', ':', ',' ) , str_replace( array( "\r\n", "\r", "\n", "\t", ' ', ' ', ' ' ), '', preg_replace( '!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $c ) ) );
$f = fopen( $path, 'wb' );
fwrite( $f, $c );
fclose( $f );
}
}
The weirdest part is no errors are actually thrown when array_pushing/concatenating. I don't even know exactly what question to ask because I can't actually figure out what's going wrong. I've also messed around with headers, character encoding, different ob functions, and casting ob_get_flush to string out of desperation.
Solution:
function get_include_output($file)
{
ob_start();
include( $file );
return ob_get_flush();
}
function styles_init()
{
$path = __DIR__ . '/aggregate.min.css';
if ( is_writable( $path ) )
{
$r = array();
foreach( array_filter( glob( __DIR__ . '/modules/*.*' ), 'is_file' ) as $file )
{
$fn = pathinfo( $file );
if ( $fn['extension'] == 'php' || $fn['extension'] == 'css' )
{
$r[] = get_include_contents( $file );
}
}
$c = implode( "\n\n", $r );
//$c = str_replace( array( ' {', ': ', ', ' ), array( '{', ':', ',' ) , str_replace( array( "\r\n", "\r", "\n", "\t", ' ', ' ', ' ' ), '', preg_replace( '!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $c ) ) );
$f = fopen( $path, 'wb' );
fwrite( $f, $c );
fclose( $f );
}
}
$r[] = $xis better than usingarray_push()here.count($r)bit, regardless, you're doingvar_dump($r)right afterarray_push()? Have you tried withob_get_clean()?array_pushis really supposed to return new number of elements in the array