baseDir = $baseDir; $this->cacheDir = $cacheDir; } public function compile( $template, $ignoreCache = FALSE ) { $sourceFile = $template[0] == '/' ? $template : "{$this->baseDir}/{$template}"; if ( ! $ignoreCache && ! empty( $this->cacheDir ) ) { $cacheFile = "{$this->cacheDir}/{$template}.php"; if ( file_exists( $cacheFile ) && filemtime( $cacheFile ) >= filemtime( $sourceFile ) ){ return file_get_contents( $cacheFile ); } } if ( ! file_exists( $sourceFile ) ) { return FALSE; } $code = $this->compile_string( file_get_contents( $sourceFile ) ); if ( ! empty( $cacheFile ) ) { if ( ! is_dir( dirname( $cacheFile ) ) ) { mkdir( dirname( $cacheFile ), 0775, TRUE ); } file_put_contents( $cacheFile, $code ); } return $code; } public function compile_string( $code ) { $code = str_replace( [ '' ], [ '{{', '}}' ], $code ); $arr = explode( '{{', $code ); $result = $this->handle_cdata( $arr[0] ); for ( $i = 1; $i < sizeof( $arr ); $i++ ) { $pos = strpos( $arr[ $i ], '}}' ); $result .= $this->handle_tag( substr( $arr[ $i ], 0, $pos) ) . $this->handle_cdata( substr( $arr[ $i ], $pos + strlen('}}') ) ); } return $result; } public function clear_cache() { if ( empty( $this->cacheDir ) ) { return FALSE; } $list = scandir( $this->cacheDir ); foreach ( $list as $filename ) { if ( $filename[0] != '.' ) { unlink( $this->cacheDir . '/' . $filename ); } } return TRUE; } private function handle_cdata( $code ) { if ( empty( $this->baseUrlResources ) ) { return $code; } $search = '|([\("\\\'])' . '([a-zA-Z0-9%_-][a-zA-Z0-9%_\.-]+/[a-zA-Z0-9%_/\.-]+\.[a-zA-Z0-9_]+)' . '(["\\\'\)])|'; $replace = '\\1' . $this->baseUrlResources . '/\\2\\3'; return preg_replace( $search, $replace, $code ); } private function handle_tag( $code ) { if ( $code[0] == '~' ) { return ''; } list ( $code, $formatString ) = explode( '|', $code, 2 ) + [ '', '' ]; list ( $name, $arg ) = explode( ':', $code, 2 ) + [ '', '' ]; $function = 'handle_tag_' . $name; if ( method_exists( $this, $function ) ) { return $this->$function( $arg, $formatString ); } if ( $name[0] == '?' ) { $var = $this->_arg2var( substr( $name, 1 ) ); $var = "empty( {$var} ) ? '' : {$var}"; } else { $var = $this->_arg2var( $name ); } if ( empty( $this->outputFunction ) ) { return "'; } else { return 'outputFunction}( {$var}, '{$formatString}' ); ?" . '>'; } } private function handle_tag_if( $arg ) { if ( strpos( $arg, '$' ) !== FALSE ) { return "'; } $var = $this->_arg2var( $arg ); return "'; } private function handle_tag_if_eq( $arg1, $arg2 ) { $var = $this->_arg2var( $arg1 ); return "'; } private function handle_tag_ifnot( $arg ) { if ( strpos( $arg, '$' ) !== FALSE ) { return "'; } $var = $this->_arg2var( $arg ); return "'; } private function handle_tag_endif() { return ''; } private function handle_tag_elseif( $arg ) { if ( strpos( $arg, '$' ) !== FALSE ) { return "'; } $var = $this->_arg2var( $arg ); return "'; } private function handle_tag_else() { return ''; } private function handle_tag_link( $arg ) { $arg = trim( $arg ); return "{$this->baseUrlLinks}/{$arg}"; } private function handle_tag_loop( $arg, $varname ) { $var = $this->_arg2var( $arg ); $varname = empty( $varname ) ? 'item' : trim( $varname ); if ( $this->usePdoLoops ){ return "fetch( PDO::FETCH_ASSOC ) ){ ?" . '>'; } return "data_seek(0); " . "while ( \${$varname} = {$var}->fetch_assoc() ){ ?" . '>'; } private function handle_tag_endloop() { return ''; } private function handle_tag_repeat( $arg, $varname ) { $var = $this->_arg2var( $arg ); $names = array_map( 'trim', explode( ',', $varname ) ); $keyname = 'key'; $varname = 'item'; if ( ! empty( $names[1] ) ) { $keyname = $names[0]; $varname = $names[1]; } elseif ( ! empty( $names[0] ) ) { $varname = $names[0]; } return " \${$varname} ){ ?" . '>'; } private function handle_tag_endrepeat() { return ''; } private function handle_tag_switch( $arg, $varname ) { $var = $this->_arg2var( $arg ); return "'; } private function handle_tag_case( $arg ) { return "'; } private function handle_tag_break( $arg ) { return "'; } private function handle_tag_endswitch() { return ''; } private function handle_tag_include( $arg ) { return $this->compile( trim( $arg ) ); } private function _arg2var( $arg ) { if ( strpos( $arg, '.' ) === FALSE ) { return '$' . trim( $arg ); } $parts = explode( '.', trim( $arg ) ); return '$' . array_shift( $parts ) . "['" . implode( "']['", $parts ) . "']"; } } // end of file leany.class.php