Exemplo n.º 1
0
 public function testParseInTheSameFile()
 {
     $tmp = $this->getTmp();
     file_put_contents($tmp . '_foo.sbp.php', "<?\nif true\n\techo 'Hello'");
     Sbp::fileParse($tmp . '_foo.sbp.php');
     $lines = file($tmp . '_foo.sbp.php');
     unset($lines[0]);
     $contents = trim(implode('', $lines));
     $this->assertSame("if (true) {\n\techo 'Hello';\n}", $contents);
 }
Exemplo n.º 2
0
 public function __invoke(Filter $node, Compiler $compiler)
 {
     $nodes = $node->block->nodes;
     $indent = strlen($nodes[0]->value) - strlen(ltrim($nodes[0]->value));
     $sbp = '<?';
     foreach ($nodes as $line) {
         $sbp .= "\n" . substr($line->value, $indent);
     }
     $php = SbpParser::parse($sbp);
     return $php . (preg_match('`\\?>\\s*$`', $php) ? '' : '?>');
 }
Exemplo n.º 3
0
 public function testSuperMethods()
 {
     $tmp = $this->getTmp();
     Sbp::prod();
     copy(__DIR__ . '/../files/.src/return.php', $tmp . '_return_prod.sbp.php');
     file_put_contents($tmp . '__return_prod.php', '<?php return 1138;');
     touch($tmp . '__return_prod.php', time() - 3600);
     $this->assertSame(1138, sbp_include_once($tmp . '_return_prod'));
     $this->assertSame(1138, sbp($tmp . '_return_prod'));
     Sbp::dev();
     copy(__DIR__ . '/../files/.src/return.php', $tmp . '_return_dev.sbp.php');
     file_put_contents($tmp . '__return_dev.php', '<?php return 1138;');
     touch($tmp . '__return_dev.php', time() - 3600);
     $this->assertSame(42, sbp_include_once($tmp . '_return_dev'));
     $this->assertSame(42, sbp($tmp . '_return_dev'));
 }
Exemplo n.º 4
0
 public function testDefaultStorageDirectory()
 {
     ClassLoaderClone::unregister();
     $message = null;
     try {
         ClassLoaderClone::register();
     } catch (SbpException $e) {
         $message = $e->getMessage();
     }
     $app = __DIR__ . '/../../../../../../app';
     if (file_exists($app . '/storage') && is_writable($app . '/storage')) {
         $this->assertSame(realpath(dirname(Sbp::phpFile('foo'))), realpath($app . '/storage/sbp'), 'PHP files should be stored in the sbp storage directory in the Laravel app directory');
         return;
     }
     $this->assertTrue(strpos($message, 'register') !== false, 'Register method shoudl throw an exception no valid app path is found');
 }
Exemplo n.º 5
0
 /**
  * Register the given class loader on the auto-loader stack.
  *
  * @return void
  */
 public static function register($prepend = true, $callback = null, $app = null)
 {
     if (!static::$registered) {
         static::$registered = spl_autoload_register(array('\\Sbp\\Laravel\\ClassLoader', 'load'), true, $prepend);
         if (is_null($app)) {
             $app = __DIR__ . '/../../../../../../app';
         }
         if (!file_exists($app . '/storage') || !is_writable($app . '/storage')) {
             throw new SbpException("Laravel app and/or writable storage directory not found at {$app}, please specify the path with the following code:\nSbp\\Laravel\\ClassLoader::register(true, 'sha1', \$laravelAppPath)");
         }
         Sbp::writeIn(Sbp::SAME_DIR);
         Sbp::fileExists($app . '/routes');
         $storage = $app . '/storage/sbp';
         if (!file_exists($storage)) {
             if (mkdir($storage, 0777)) {
                 file_put_contents($storage . '/.gitignore', "*\n!.gitignore");
             }
         }
         Sbp::writeIn($storage, $callback);
     }
 }
Exemplo n.º 6
0
 /**
  * @expectedException Sbp\SbpException
  */
 public function testWriteInWrongCallback()
 {
     Sbp::writeIn(null, 'not_a_valid_callback');
 }
Exemplo n.º 7
0
 public function testContainer()
 {
     Sbp::execute(__DIR__ . '/../container/foo.sbp');
     $foo = new XFoo();
     $this->assertSame($foo->hello(), "Hello foo");
 }
Exemplo n.º 8
0
 /**
  * @after
  */
 public function removeTempDirectory()
 {
     $tmp = $this->getTmp();
     InitialSbp::writeIn(InitialSbp::SAME_DIR, null);
     foreach (scandir($tmp) as $file) {
         if (substr($file, 0, 1) === '_') {
             unlink($tmp . $file);
         }
     }
     rmdir($tmp);
 }
Exemplo n.º 9
0
 public function parse($sbp)
 {
     $php = SbpParser::parse($sbp);
     return $php . (preg_match('`\\?>\\s*$`', $php) ? '' : '?>');
 }
Exemplo n.º 10
0
            if (file_exists($from)) {
                $to = __DIR__ . '/tests/sbp/files/' . $name . '.php';
                SbpTestParse::fileParse($from, $to);
                echo "Compilation done in:\n";
                echo realpath($to) . "\n";
                echo filesize($to) . " bytes\n";
                continue;
            }
            echo "File not found at:\n";
            echo "{$from}\n";
            echo "To create a new test with this name, enter:\n";
            echo "{$script} " . CREATE_TEST . " {$name} \n";
        }
        break;
    case COMPILE:
        $from = argn(2);
        if (file_exists($from)) {
            $to = tempnam(sys_get_temp_dir(), 'sbp-compile');
            Sbp::fileParse($from, $to);
            readfile($to);
            unlink($to);
            break;
        }
        echo "File not found at:\n";
        echo "{$from}\n";
        break;
    default:
        echo "Usage:\n  php {$script} <command>\n\n";
        echo "Commands:\n  " . CREATE_TEST . "\n  " . COMPILE_TEST . "\n  " . COMPILE;
        break;
}