Exemple #1
0
            Dir::remove($this->temp);
        });
        it("clears the cache", function () {
            $this->interceptor->clearCache();
            expect(file_exists($this->customCachePath))->toBe(false);
        });
        it("bails out if the cache has already been cleared", function () {
            $this->interceptor->clearCache();
            $this->interceptor->clearCache();
            expect(file_exists($this->customCachePath))->toBe(false);
        });
    });
    describe("->watch()/unwatch()", function () {
        it("add some file to be watched", function () {
            $this->temp = Dir::tempnam(null, 'cache');
            touch($this->temp . DS . 'watched1.php');
            touch($this->temp . DS . 'watched2.php');
            $watched = [$this->temp . DS . 'watched1.php', $this->temp . DS . 'watched2.php'];
            $this->interceptor = Interceptor::patch(['cachePath' => $this->cachePath]);
            $this->interceptor->watch($this->temp . DS . 'watched1.php');
            expect($this->interceptor->watched())->toBe([$watched[0]]);
            $this->interceptor->watch($this->temp . DS . 'watched2.php');
            expect($this->interceptor->watched())->toBe($watched);
            $this->interceptor->unwatch($this->temp . DS . 'watched1.php');
            expect($this->interceptor->watched())->toBe([$watched[1]]);
            $this->interceptor->unwatch($this->temp . DS . 'watched2.php');
            expect($this->interceptor->watched())->toBe([]);
            Dir::remove($this->temp);
        });
    });
});
Exemple #2
0
 /**
  * Enables filter JIT patching.
  *
  * @param  array  $classes Classes to patch.
  * @param  array  $options Interceptor options.
  * @return object          The Interceptor instance.
  */
 public static function patch($classes = [], $options = [])
 {
     $loaders = spl_autoload_functions();
     $loader = reset($loaders);
     $interceptor = null;
     if (method_exists($loader[0], 'patchers')) {
         $interceptor = $loader[0];
         // When using an already existing interceptor, make sure the added patcher
         // won't need to autoload anything to prevent infinite loop.
         class_exists('Lead\\Jit\\JitException');
         class_exists('Lead\\Jit\\Node\\NodeDef');
         class_exists('Lead\\Jit\\Node\\FunctionDef');
         class_exists('Lead\\Jit\\Node\\BlockDef');
         class_exists('Lead\\Jit\\TokenStream');
         class_exists('Lead\\Jit\\Parser');
     } elseif (isset($options['loader'])) {
         $interceptor = Interceptor::patch($options);
         static::$_unpatch = true;
     } else {
         foreach ($loaders as $loader) {
             if ($loader[0] instanceof ClassLoader) {
                 $options['loader'] = [$loader[0], 'loadClass'];
                 $interceptor = Interceptor::patch($options);
                 static::$_unpatch = true;
                 break;
             }
         }
     }
     if (!$interceptor) {
         throw new Exception("Unable to find a valid autoloader to apply the JIT filter patcher.");
     }
     $filter = new Filter(['patch' => $classes]);
     $patchers = $interceptor->patchers();
     $patchers->add('filter', $filter);
     return static::$_interceptor = $interceptor;
 }