1

I'm using PHP's CLI for creating a program. There's a folder named /compile which holds all of the compiled files after it's CLI call, this folder gets the constant __COMPILE__. I'm using this function and using the constant to use as the location: (Location: C:\Users\Jeklin\Documents\GitHub\spitfire-engine\compiled):

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 65488 bytes) in C:\Users\Jeklin\Documents\GitHub\spitfire-engine\sys\compiler\Launcher.compiler.php on line 66

This is the function I'm using:

/*59*/    public function p_sweep_compile_dir()
/*60*/      { $dir = __COMPILE__;
/*61*/        if (is_dir($dir)) {
/*62*/          $objects = scandir($dir);
/*63*/          foreach ($objects as $object) {
/*64*/            if ($object != "." && $object != "..") {
/*65*/              if (filetype($dir."/".$object) == "dir")
/*66*/                 $this->p_sweep_compile_dir($dir."/".$object);
/*67*/              else unlink   ($dir."/".$object);
/*68*/            }
/*69*/          }
/*70*/          reset($objects);
/*71*/          rmdir($dir);
/*72*/        } else die('tried to delete a non-folder from p_sweep_compile_dir');
/*73*/      }

I can see that on line 66 that includes the recursion method to try to get deeper into the file structure, although the structure is very shallow, this is the __COMPILE__ directories setup:

|--compiled
     |---app
     |---public
     |---sys
     |index.html
Jack Hales
  • 1,574
  • 23
  • 51
  • 1
    `public function p_sweep_compile_dir(no_args!?) {...` each time you call the function, you reset dir to `__COMPILE__` – Zimmi Mar 29 '17 at 20:01

1 Answers1

1

Not sure who wrote that code but this would make more sense:

/*59*/    public function p_sweep_compile_dir($dir = null) // default $dir to null unless something is passed into it
/*60*/      { $dir = ($dir === null ? __COMPILE__ : $dir); // If $dir is null then use __COMPILE__ otherwise use the $dir value
/*61*/        if (is_dir($dir)) {
/*62*/          $objects = scandir($dir);
/*63*/          foreach ($objects as $object) {
/*64*/            if ($object != "." && $object != "..") {
/*65*/              if (filetype($dir."/".$object) == "dir")
/*66*/                 $this->p_sweep_compile_dir($dir."/".$object); // recurse self and provide deeper dir
/*67*/              else unlink   ($dir."/".$object);
/*68*/            }
/*69*/          }
/*70*/          reset($objects);
/*71*/          rmdir($dir);
/*72*/        } else die('tried to delete a non-folder from p_sweep_compile_dir');
/*73*/      }

I have not tested this but in theory it should work.

See line #s 59, 60, and 66 for comments.


Side-note:

Assuming you have full control of this source code, for the love of all that is good in this world, please use opening and closing curly braces, ALWAYS. Some poor programmer is going to curse you for hours when they work on this after you stop working on it.


How I would write it:

public function p_sweep_compile_dir($dir = null) // default $dir to null unless something is passed into it
{
    $dir = ($dir === null ? __COMPILE__ : $dir); // If $dir is null then use __COMPILE__ otherwise use the $dir value

    if(is_dir($dir))
    {
        $objects = array_diff(scandir($dir), array('..', '.'));
        foreach ($objects as $object)
        {
            if(is_dir($dir."/".$object))
            {
                $this->p_sweep_compile_dir($dir."/".$object); // recurse self and provide deeper dir
            }
            else
            {
                unlink($dir."/".$object);
            }
        }
        unset($object, $objects); // forced memory management
        rmdir($dir);
    }
    else
    {
        die('tried to delete a non-folder from p_sweep_compile_dir');
    }

    unset($dir);
}
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77