/**
 * Common file strategies for PHP
 *
 * @author Eric Bouwers
 */
module php/strategy/files

strategies
 /**
  * Simple inclusion of files. Only matches the direct strings
  * and includes the files it finds. Fills a newly created
  * environment with the newly included files.
  *
  */
  include-files-simple =
     where(?doc ; init-php-environment(|doc))
   ; procedural-topdown(try(process-include-and-require-files(fill-and-include)))
    
   fill-and-include = 
    where( ?doc ; fill-php-environment(|doc))
  ; procedural-topdown( try( process-include-and-require-files(fill-and-include)))



strategies
  /**
  * Complex inclusion of files. Uses the simple evaluation strategy
  * to get as many files as possible. Does not fail when a file-name cannot
  * be found. Fills a newly created environment with the newly included files.
  */
  include-files-complex =
     where( ?doc ; init-php-environment(|doc))
   ; main-constant-propogation 

strategies
  /**
   * Printing of included files. This will print all included files in the
   * same directory with a certain post-fix. This post-fix must be given.
   *
   * @param post-fix The string that is to be added after every filename
   */
   print-included-files(s|post-fix) =
       log(|Debug(), "Start printing included files.")
     ; env := <get-php-environment>
     ; get-all-inclusion-ids
     ; ?[ "main-file" | <id>] /*main AST should be first */
     ; map(print-included-file(s|env,post-fix))
     ; log(|Debug(), "End printing included files.")

   print-included-file(s|env,post-fix) =
      ?iid
    ;  log(|Debug(), "Printing included file, id: ", iid)
    ; <?Inclusionid(path,_)> iid
    ; ast   := <get-inclusion-file(|iid)> env
    ; path' := <conc-strings> (path,post-fix)
    ; ast'  := <s ; box2text-string(|80)> ast
    ;  log(|Debug(), "Opening file: ", path')
    ; file  := <fopen> (path', "w")
    ; <fputs> (ast',file)
    ; <fclose> file
    ;  log(|Debug(), "Closing file: ", path')