diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs index 3294f91d..19d5f878 100644 --- a/.git-blame-ignore-revs +++ b/.git-blame-ignore-revs @@ -3,3 +3,6 @@ # Rename SnapshotTest to SnapshotsTest 45b422424a03c24ad2aed0af2bd0648ba4eb4e17 + +# Move dotenv file snapshot test +a2f33e083680337ba53704e4daf8c2d9ed2ac2f8 diff --git a/jigsaw b/jigsaw index 1c235969..e54d8c1a 100755 --- a/jigsaw +++ b/jigsaw @@ -1,16 +1,25 @@ #!/usr/bin/env php add($container[InitCommand::class]); -$app->add(new BuildCommand($container)); -$app->add(new ServeCommand($container)); -Jigsaw::addUserCommands($app, $container); -$app->run(); +define('JIGSAW_START', microtime(true)); + +if (file_exists(__DIR__ . '/vendor/autoload.php')) { + require __DIR__ . '/vendor/autoload.php'; +} + +if (file_exists(getcwd() . '/vendor/autoload.php')) { + require getcwd() . '/vendor/autoload.php'; +} + +$app = new TightenCo\Jigsaw\Container; + +$app->bootstrapWith([]); + +$application = new Symfony\Component\Console\Application('Jigsaw', '1.4.0'); +$application->add($app[TightenCo\Jigsaw\Console\InitCommand::class]); +$application->add(new TightenCo\Jigsaw\Console\BuildCommand($app)); +$application->add(new TightenCo\Jigsaw\Console\ServeCommand($app)); + +TightenCo\Jigsaw\Jigsaw::addUserCommands($application, $app); + +$application->run(); diff --git a/jigsaw-core.php b/jigsaw-core.php deleted file mode 100644 index 6018c356..00000000 --- a/jigsaw-core.php +++ /dev/null @@ -1,218 +0,0 @@ -setInstance($container); - -$container->instance('cwd', getcwd()); - -if (file_exists($envPath = $container['cwd'] . '/.env')) { - (Dotenv::createImmutable($container['cwd']))->load(); -} - -$cachePath = $container['cwd'] . '/cache'; -$bootstrapFile = $container['cwd'] . '/bootstrap.php'; - -$container->instance('buildPath', [ - 'source' => $container['cwd'] . '/source', - 'destination' => $container['cwd'] . '/build_{env}', -]); - -$container->bind('config', function ($c) use ($cachePath) { - $config = (new ConfigFile($c['cwd'] . '/config.php', $c['cwd'] . '/helpers.php'))->config; - $config->put('view.compiled', $cachePath); - return $config; -}); - -$container->singleton('consoleOutput', function ($c) { - return new ConsoleOutput(); -}); - -$container->bind('outputPathResolver', function ($c) { - return new BasicOutputPathResolver; -}); - -$container->bind(YAMLParser::class, SymfonyYAMLParser::class); - -$container->singleton('markdownParser', function ($c) { - return new MarkdownParser; -}); - -$container->bind(FrontYAMLMarkdownParser::class, function ($c) { - return $c['markdownParser']; -}); - -$container->bind(Parser::class, function ($c) { - return new Parser($c[YAMLParser::class], $c[FrontYAMLMarkdownParser::class]); -}); - -$container->bind(FrontMatterParser::class, function ($c) { - return new FrontMatterParser($c[Parser::class]); -}); - -$bladeCompiler = new BladeCompiler(new Filesystem, $cachePath); - -$container->bind('bladeCompiler', function ($c) use ($bladeCompiler) { - return $bladeCompiler; -}); - -$container->singleton(Factory::class, function ($c) use ($cachePath, $bladeCompiler) { - $resolver = new EngineResolver; - - $compilerEngine = new CompilerEngine($bladeCompiler, new Filesystem); - - $resolver->register('blade', function () use ($compilerEngine) { - return $compilerEngine; - }); - - $resolver->register('php', function () { - return new PhpEngine(new Filesystem); - }); - - $resolver->register('markdown', function () use ($c) { - return new MarkdownEngine($c[FrontMatterParser::class], new Filesystem, $c['buildPath']['views']); - }); - - $resolver->register('blade-markdown', function () use ($c, $compilerEngine) { - return new BladeMarkdownEngine($compilerEngine, $c[FrontMatterParser::class]); - }); - - (new BladeDirectivesFile($c['cwd'] . '/blade.php', $bladeCompiler))->register(); - - $finder = new FileViewFinder(new Filesystem, [$cachePath, $c['buildPath']['views']]); - - $factory = new Factory($resolver, $finder, new FakeDispatcher()); - $factory->setContainer($c); - - return $factory; -}); - -$container->bind('view', function ($c) { - return $c[Factory::class]; -}); - -$container->bind(ViewRenderer::class, function ($c) use ($bladeCompiler) { - return new ViewRenderer($c[Factory::class], $bladeCompiler, $c['config']); -}); - -$container->bind(TemporaryFilesystem::class, function ($c) use ($cachePath) { - return new TemporaryFilesystem($cachePath); -}); - -$container->bind(BladeHandler::class, function ($c) { - return new BladeHandler($c[TemporaryFilesystem::class], $c[FrontMatterParser::class], $c[ViewRenderer::class]); -}); - -$container->bind(MarkdownHandler::class, function ($c) { - return new MarkdownHandler($c[TemporaryFilesystem::class], $c[FrontMatterParser::class], $c[ViewRenderer::class]); -}); - -$container->bind(CollectionPathResolver::class, function ($c ) { - return new CollectionPathResolver($c['outputPathResolver'], $c[ViewRenderer::class]); -}); - -$container->bind(CollectionDataLoader::class, function ($c) { - return new CollectionDataLoader(new Filesystem, $c['consoleOutput'], $c[CollectionPathResolver::class], [ - $c[MarkdownCollectionItemHandler::class], - $c[BladeCollectionItemHandler::class], - ]); -}); - -$container->bind(DataLoader::class, function ($c) { - return new DataLoader($c[CollectionDataLoader::class]); -}); - -$container->bind(CollectionItemHandler::class, function ($c) { - return new CollectionItemHandler($c['config'], [ - $c[MarkdownHandler::class], - $c[BladeHandler::class], - ]); -}); - -$container->bind(CollectionPaginator::class, function ($c) { - return new CollectionPaginator($c['outputPathResolver']); -}); - -$container->bind(PaginatedPageHandler::class, function ($c) { - return new PaginatedPageHandler($c[CollectionPaginator::class], $c[FrontMatterParser::class], $c[TemporaryFilesystem::class], $c[ViewRenderer::class]); -}); - -$container->bind(SiteBuilder::class, function ($c) use ($cachePath) { - return new SiteBuilder(new Filesystem, $cachePath, $c['outputPathResolver'], $c['consoleOutput'], [ - $c[CollectionItemHandler::class], - new IgnoredHandler, - $c[PaginatedPageHandler::class], - $c[MarkdownHandler::class], - $c[BladeHandler::class], - $c[DefaultHandler::class], - ]); -}); - -$container->bind(CollectionRemoteItemLoader::class, function ($c) { - return new CollectionRemoteItemLoader($c['config'], new Filesystem); -}); - -$container->singleton('events', function ($c) { - return new EventBus(); -}); - -$container->bind(Jigsaw::class, function ($c) { - return new Jigsaw($c, $c[DataLoader::class], $c[CollectionRemoteItemLoader::class], $c[SiteBuilder::class]); -}); - -if (file_exists($bootstrapFile)) { - $events = $container->events; - include $bootstrapFile; -} diff --git a/src/Container.php b/src/Container.php new file mode 100644 index 00000000..1e18dbc1 --- /dev/null +++ b/src/Container.php @@ -0,0 +1,192 @@ +path = getcwd(); + + static::setInstance($this); + $this->instance('app', $this); + + $this->registerCoreProviders(); + $this->registerCoreAliases(); + } + + public function bootstrapWith(array $bootstrappers): void + { + $this->bootstrapped = true; + + $this->loadEnvironmentVariables(); + $this->loadConfiguration(); + + foreach ($bootstrappers as $bootstrapper) { + $this->make($bootstrapper)->bootstrap($this); + } + + $this->registerConfiguredProviders(); + $this->boot(); + } + + public function path(string ...$path): string + { + return implode('/', array_filter([$this->path, ...$path])); + } + + public function cachePath(string ...$path): string + { + return $this->path('cache', ...$path); + } + + public function isBooted(): bool + { + return $this->booted; + } + + public function booting(callable $callback): void + { + $this->bootingCallbacks[] = $callback; + } + + public function booted(callable $callback): void + { + $this->bootedCallbacks[] = $callback; + + if ($this->isBooted()) { + $callback($this); + } + } + + private function loadEnvironmentVariables(): void + { + try { + Dotenv::create(Env::getRepository(), $this->path)->safeLoad(); + } catch (InvalidFileException $e) { + $output = (new ConsoleOutput)->getErrorOutput(); + + $output->writeln('The environment file is invalid!'); + $output->writeln($e->getMessage()); + + exit(1); + } + } + + private function loadConfiguration(): void + { + $config = collect(); + + $files = array_filter([ + $this->path('config.php'), + $this->path('helpers.php'), + ], 'file_exists'); + + foreach ($files as $path) { + $config = $config->merge(require $path); + } + + if ($config->get('collections')) { + $config->put('collections', collect($config->get('collections'))->flatMap( + fn ($value, $key) => is_array($value) ? [$key => $value] : [$value => []] + )); + } + + $this->instance('buildPath', [ + 'source' => $this->path('source'), + 'destination' => $this->path('build_{env}'), + ]); + + $config->put('view.compiled', $this->cachePath()); + + $this->instance('config', $config); + + setlocale(LC_ALL, 'en_US.UTF8'); + } + + private function boot(): void + { + $this->fireAppCallbacks($this->bootingCallbacks); + + array_walk($this->providers, function ($provider) { + if (method_exists($provider, 'boot')) { + $this->call([$provider, 'boot']); + } + }); + + $this->booted = true; + + $this->fireAppCallbacks($this->bootedCallbacks); + } + + /** @param callable[] $callbacks */ + private function fireAppCallbacks(array &$callbacks): void + { + $index = 0; + + while ($index < count($callbacks)) { + $callbacks[$index]($this); + + $index++; + } + } + + private function registerCoreProviders(): void + { + foreach ([ + Providers\EventServiceProvider::class, + ] as $provider) { + ($provider = new $provider($this))->register(); + + $this->providers[] = $provider; + } + } + + private function registerConfiguredProviders(): void + { + foreach ([ + Providers\FilesystemServiceProvider::class, + Providers\MarkdownServiceProvider::class, + Providers\ViewServiceProvider::class, + Providers\CollectionServiceProvider::class, + Providers\CompatibilityServiceProvider::class, + Providers\BootstrapFileServiceProvider::class, + ] as $provider) { + ($provider = new $provider($this))->register(); + + $this->providers[] = $provider; + } + } + + private function registerCoreAliases(): void + { + foreach ([ + 'app' => [self::class, \Illuminate\Contracts\Container\Container::class, \Psr\Container\ContainerInterface::class], + 'view' => [\Illuminate\View\Factory::class, \Illuminate\Contracts\View\Factory::class], + ] as $key => $aliases) { + foreach ($aliases as $alias) { + $this->alias($key, $alias); + } + } + } +} diff --git a/src/Events/FakeDispatcher.php b/src/Events/FakeDispatcher.php deleted file mode 100644 index 9782d7cc..00000000 --- a/src/Events/FakeDispatcher.php +++ /dev/null @@ -1,44 +0,0 @@ -app->path('bootstrap.php'))) { + $events = $this->app->events; + $container = $this->app; + $cachePath = $this->app->cachePath(); + $envPath = $this->app->path('.env'); + $bladeCompiler = $this->app['blade.compiler']; + + include $bootstrapFile; + } + } +} diff --git a/src/Providers/CollectionServiceProvider.php b/src/Providers/CollectionServiceProvider.php new file mode 100644 index 00000000..9be29d79 --- /dev/null +++ b/src/Providers/CollectionServiceProvider.php @@ -0,0 +1,111 @@ +app->bind('outputPathResolver', fn () => new BasicOutputPathResolver); + + $this->registerHandlers(); + $this->registerPathResolver(); + $this->registerLoaders(); + $this->registerPaginator(); + $this->registerSiteBuilder(); + + $this->app->bind(Jigsaw::class, function (Container $app) { + return new Jigsaw($app, $app[DataLoader::class], $app[CollectionRemoteItemLoader::class], $app[SiteBuilder::class]); + }); + } + + private function registerHandlers(): void + { + $this->app->bind(BladeHandler::class, function (Container $app) { + return new BladeHandler($app[TemporaryFilesystem::class], $app[FrontMatterParser::class], $app[ViewRenderer::class]); + }); + + $this->app->bind(MarkdownHandler::class, function (Container $app) { + return new MarkdownHandler($app[TemporaryFilesystem::class], $app[FrontMatterParser::class], $app[ViewRenderer::class]); + }); + + $this->app->bind(CollectionItemHandler::class, function (Container $app) { + return new CollectionItemHandler($app['config'], [ + $app[MarkdownHandler::class], + $app[BladeHandler::class], + ]); + }); + } + + private function registerPathResolver(): void + { + $this->app->bind(CollectionPathResolver::class, function (Container $app) { + return new CollectionPathResolver($app['outputPathResolver'], $app[ViewRenderer::class]); + }); + } + + private function registerLoaders(): void + { + $this->app->bind(CollectionDataLoader::class, function (Container $app) { + return new CollectionDataLoader($app['files'], $app['consoleOutput'], $app[CollectionPathResolver::class], [ + $app[MarkdownCollectionItemHandler::class], + $app[BladeCollectionItemHandler::class], + ]); + }); + + $this->app->bind(DataLoader::class, function (Container $app) { + return new DataLoader($app[CollectionDataLoader::class]); + }); + + $this->app->bind(CollectionRemoteItemLoader::class, function (Container $app) { + return new CollectionRemoteItemLoader($app['config'], $app['files']); + }); + } + + private function registerPaginator(): void + { + $this->app->bind(CollectionPaginator::class, function (Container $app) { + return new CollectionPaginator($app['outputPathResolver']); + }); + + $this->app->bind(PaginatedPageHandler::class, function (Container $app) { + return new PaginatedPageHandler($app[CollectionPaginator::class], $app[FrontMatterParser::class], $app[TemporaryFilesystem::class], $app[ViewRenderer::class]); + }); + } + + private function registerSiteBuilder(): void + { + $this->app->bind(SiteBuilder::class, function (Container $app) { + return new SiteBuilder($app['files'], $app->cachePath(), $app['outputPathResolver'], $app['consoleOutput'], [ + $app[CollectionItemHandler::class], + new IgnoredHandler, + $app[PaginatedPageHandler::class], + $app[MarkdownHandler::class], + $app[BladeHandler::class], + $app[DefaultHandler::class], + ]); + }); + } +} diff --git a/src/Providers/CompatibilityServiceProvider.php b/src/Providers/CompatibilityServiceProvider.php new file mode 100644 index 00000000..7983500a --- /dev/null +++ b/src/Providers/CompatibilityServiceProvider.php @@ -0,0 +1,16 @@ +app->instance('cwd', $this->app->path()); + + $this->app->singleton('consoleOutput', fn () => new ConsoleOutput); + } +} diff --git a/src/Providers/EventServiceProvider.php b/src/Providers/EventServiceProvider.php new file mode 100644 index 00000000..b2fbc252 --- /dev/null +++ b/src/Providers/EventServiceProvider.php @@ -0,0 +1,18 @@ +app->singleton('dispatcher', fn (Container $app) => new Dispatcher($app)); + + $this->app->singleton('events', fn (Container $app) => new EventBus); + } +} diff --git a/src/Providers/FilesystemServiceProvider.php b/src/Providers/FilesystemServiceProvider.php new file mode 100644 index 00000000..2048d1bc --- /dev/null +++ b/src/Providers/FilesystemServiceProvider.php @@ -0,0 +1,14 @@ +app->singleton('files', fn () => new Filesystem); + } +} diff --git a/src/Providers/MarkdownServiceProvider.php b/src/Providers/MarkdownServiceProvider.php new file mode 100644 index 00000000..914c8ccb --- /dev/null +++ b/src/Providers/MarkdownServiceProvider.php @@ -0,0 +1,33 @@ +app->bind(YAMLParser::class, SymfonyYAMLParser::class); + + $this->app->singleton('markdownParser', fn (Container $app) => new MarkdownParser); + + // Make the FrontYAML package use our own Markdown parser internally + $this->app->bind(FrontYAMLMarkdownParser::class, fn (Container $app) => $app['markdownParser']); + + $this->app->bind(Parser::class, function (Container $app) { + return new Parser($app[YAMLParser::class], $app[FrontYAMLMarkdownParser::class]); + }); + + $this->app->bind(FrontMatterParser::class, function (Container $app) { + return new FrontMatterParser($app[Parser::class]); + }); + } +} diff --git a/src/Providers/ViewServiceProvider.php b/src/Providers/ViewServiceProvider.php new file mode 100644 index 00000000..52959247 --- /dev/null +++ b/src/Providers/ViewServiceProvider.php @@ -0,0 +1,113 @@ +registerFactory(); + $this->registerViewFinder(); + $this->registerBladeCompiler(); + $this->registerEngineResolvers(); + + (new BladeDirectivesFile($this->app->path('blade.php'), $this->app['blade.compiler']))->register(); + $this->app->bind(ViewRenderer::class, fn () => new ViewRenderer); + $this->app->bind(TemporaryFilesystem::class, fn (Container $app) => new TemporaryFilesystem($app->cachePath())); + + // TODO + // $this->registerExtensionEngines(); + // $this->registerConfiguredHintPaths(); + } + + private function registerFactory(): void + { + $this->app->singleton('view', function (Container $app) { + $factory = new Factory($app['view.engine.resolver'], $app['view.finder'], $app['dispatcher']); + + $factory->setContainer($app); + // TODO provide a magic `$app` variable to all views? + // $factory->share('app', $app); + + return $factory; + }); + } + + private function registerViewFinder(): void + { + $this->app->bind('view.finder', function (Container $app) { + // TODO $app['config']['view.paths'] + return new FileViewFinder($app['files'], [$app->cachePath(), $app['buildPath']['views']]); + }); + } + + private function registerBladeCompiler(): void + { + $this->app->singleton('blade.compiler', function (Container $app) { + // TODO $app['config']['view.compiled'] + return tap(new BladeCompiler($app['files'], $app->cachePath()), function ($blade) { + $blade->component('dynamic-component', DynamicComponent::class); + }); + }); + + // v1 binding is 'bladeCompiler' + $this->app->alias('blade.compiler', 'bladeCompiler'); + } + + private function registerEngineResolvers(): void + { + $this->app->singleton('view.engine.resolver', function (Container $app) { + $resolver = new EngineResolver; + $compilerEngine = new CompilerEngine($app['blade.compiler'], $app['files']); + + // Same as Laravel + $resolver->register('file', fn () => new FileEngine($app['files'])); + $resolver->register('php', fn () => new PhpEngine($app['files'])); + $resolver->register('blade', fn () => $compilerEngine); + + // Specific to Jigsaw + // TODO $app['config']['view.paths'] + $resolver->register('markdown', fn () => new MarkdownEngine($app[FrontMatterParser::class], $app['files'], $app['buildPath']['views'])); + $resolver->register('blade-markdown', fn () => new BladeMarkdownEngine($compilerEngine, $this->app[FrontMatterParser::class])); + + return $resolver; + }); + } + + private function registerExtensionEngines(): void + { + foreach (['md', 'markdown', 'mdown'] as $extension) { + $this->app['view']->addExtension($extension, 'markdown'); + $this->app['view']->addExtension("blade.{$extension}", 'blade-markdown'); + } + + foreach (['js', 'json', 'xml', 'yaml', 'yml', 'rss', 'atom', 'txt', 'text', 'html'] as $extension) { + $this->app['view']->addExtension($extension, 'php'); + $this->app['view']->addExtension("blade.{$extension}", 'blade'); + } + } + + private function registerConfiguredHintPaths(): void + { + foreach ($this->app['config']->get('viewHintPaths', []) as $hint => $path) { + $this->app['view']->addNamespace($hint, $path); + } + } +} diff --git a/src/Support/ServiceProvider.php b/src/Support/ServiceProvider.php new file mode 100644 index 00000000..16052f78 --- /dev/null +++ b/src/Support/ServiceProvider.php @@ -0,0 +1,17 @@ +make($abstract, $parameters); + } +} + function leftTrimPath($path) { return ltrim($path, ' \\/'); diff --git a/src/View/ViewRenderer.php b/src/View/ViewRenderer.php index 12ab9c40..6b380fe6 100644 --- a/src/View/ViewRenderer.php +++ b/src/View/ViewRenderer.php @@ -2,74 +2,51 @@ namespace TightenCo\Jigsaw\View; -use Illuminate\Support\Collection; -use Illuminate\View\Compilers\BladeCompiler; -use Illuminate\View\Factory; - +/** + * For now some of this only works if we do it in this file, not in the ViewServiceProvider, + * because other code (BuildCommand, TestCase) updates the config or buildPaths after the + * ViewServiceProvider is registered, so if we do it there it'll be overwritten. + */ class ViewRenderer { - private $config; - private $viewFactory; - private $bladeCompiler; - private $finder; - private $extensionEngines = [ - 'md' => 'markdown', - 'markdown' => 'markdown', - 'mdown' => 'markdown', - 'blade.md' => 'blade-markdown', - 'blade.mdown' => 'blade-markdown', - 'blade.markdown' => 'blade-markdown', - ]; - private $bladeExtensions = [ - 'js', 'json', 'xml', 'yaml', 'yml', 'rss', 'atom', 'txt', 'text', 'html', - ]; - - public function __construct(Factory $viewFactory, BladeCompiler $bladeCompiler, Collection $config = null) + public function __construct() { - $this->config = $config ?? collect(); - $this->viewFactory = $viewFactory; - $this->bladeCompiler = $bladeCompiler; - $this->finder = $this->viewFactory->getFinder(); $this->addExtensions(); $this->addHintpaths(); } public function getExtension($bladeViewPath) { - return strtolower(pathinfo($this->finder->find($bladeViewPath), PATHINFO_EXTENSION)); + return strtolower(pathinfo(app('view')->getFinder()->find($bladeViewPath), PATHINFO_EXTENSION)); } public function render($path, $data) { - return $this->viewFactory->file($path, $data->all())->render(); + return app('view')->file($path, $data->all())->render(); } public function renderString($string) { - return $this->bladeCompiler->compileString($string); + return app('blade.compiler')->compileString($string); } private function addHintpaths() { - collect($this->config->get('viewHintPaths'))->each(function ($path, $hint) { - $this->addHintpath($hint, $path); - }); - } - - private function addHintPath($hint, $path) - { - $this->viewFactory->addNamespace($hint, $path); + foreach (app('config')->get('viewHintPaths', []) as $hint => $path) { + app('view')->addNamespace($hint, $path); + } } private function addExtensions() { - collect($this->extensionEngines)->each(function ($engine, $extension) { - $this->viewFactory->addExtension($extension, $engine); - }); - - collect($this->bladeExtensions)->each(function ($extension) { - $this->viewFactory->addExtension($extension, 'php'); - $this->viewFactory->addExtension('blade.' . $extension, 'blade'); - }); + foreach (['md', 'markdown', 'mdown'] as $extension) { + app('view')->addExtension($extension, 'markdown'); + app('view')->addExtension("blade.{$extension}", 'blade-markdown'); + } + + foreach (['js', 'json', 'xml', 'yaml', 'yml', 'rss', 'atom', 'txt', 'text', 'html'] as $extension) { + app('view')->addExtension($extension, 'php'); + app('view')->addExtension("blade.{$extension}", 'blade'); + } } } diff --git a/tests/SnapshotsTest.php b/tests/SnapshotsTest.php index 26d5d431..c55cedac 100644 --- a/tests/SnapshotsTest.php +++ b/tests/SnapshotsTest.php @@ -56,7 +56,7 @@ public function build(string $name) // Delete the contents of the output directory in the source to clean up previous builds $this->filesystem->deleteDirectory($this->output($name), true); - $jigsaw = realpath(implode(DIRECTORY_SEPARATOR, array_filter([__DIR__, '..', 'jigsaw']))); + $jigsaw = realpath(implode('/', array_filter([__DIR__, '..', 'jigsaw']))); $arguments = static::$arguments[$name] ?? []; $build = new Process(array_merge(['php', $jigsaw, 'build'], $arguments, ['-vvv']), $this->source($name)); diff --git a/tests/TestCase.php b/tests/TestCase.php index ce25353c..bc7baca1 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -3,9 +3,11 @@ namespace Tests; use Illuminate\Support\Str; +use Illuminate\View\Component; use Mockery; use org\bovigo\vfs\vfsStream; use PHPUnit\Framework\TestCase as BaseTestCase; +use TightenCo\Jigsaw\Container; use TightenCo\Jigsaw\File\Filesystem; use TightenCo\Jigsaw\File\InputFile; use TightenCo\Jigsaw\Jigsaw; @@ -23,15 +25,17 @@ class TestCase extends BaseTestCase protected function setUp(): void { parent::setUp(); - require 'jigsaw-core.php'; - $this->app = $container; + + $this->app = new Container; + $this->app->bootstrapWith([]); + $this->app->buildPath = [ 'source' => $this->sourcePath, 'views' => $this->sourcePath, 'destination' => $this->destinationPath, ]; $this->filesystem = new Filesystem(); - $this->tempPath = $cachePath; + $this->tempPath = $this->app->cachePath(); $this->prepareTempDirectory(); } @@ -39,6 +43,13 @@ protected function tearDown(): void { $this->cleanupTempDirectory(); Mockery::close(); + + if (method_exists(Component::class, 'flushCache')) { + Component::flushCache(); + Component::forgetComponentsResolver(); + Component::forgetFactory(); + } + parent::tearDown(); } diff --git a/tests/snapshots/bootstrap-file/bootstrap.php b/tests/snapshots/bootstrap-file/bootstrap.php new file mode 100644 index 00000000..369f0404 --- /dev/null +++ b/tests/snapshots/bootstrap-file/bootstrap.php @@ -0,0 +1,3 @@ +put('config_value', 'bar'); diff --git a/tests/snapshots/bootstrap-file/config.php b/tests/snapshots/bootstrap-file/config.php new file mode 100644 index 00000000..893af32f --- /dev/null +++ b/tests/snapshots/bootstrap-file/config.php @@ -0,0 +1,5 @@ + 'foo', +]; diff --git a/tests/snapshots/bootstrap-file/source/index.blade.php b/tests/snapshots/bootstrap-file/source/index.blade.php new file mode 100644 index 00000000..a13d3224 --- /dev/null +++ b/tests/snapshots/bootstrap-file/source/index.blade.php @@ -0,0 +1 @@ +
{{ $page->config_value }}
diff --git a/tests/snapshots/bootstrap-file_snapshot/index.html b/tests/snapshots/bootstrap-file_snapshot/index.html new file mode 100644 index 00000000..3f3570ea --- /dev/null +++ b/tests/snapshots/bootstrap-file_snapshot/index.html @@ -0,0 +1 @@ +
bar
diff --git a/tests/snapshots/load-dotenv-file/.env b/tests/snapshots/dotenv-file/.env similarity index 100% rename from tests/snapshots/load-dotenv-file/.env rename to tests/snapshots/dotenv-file/.env diff --git a/tests/snapshots/load-dotenv-file/config.php b/tests/snapshots/dotenv-file/config.php similarity index 100% rename from tests/snapshots/load-dotenv-file/config.php rename to tests/snapshots/dotenv-file/config.php diff --git a/tests/snapshots/load-dotenv-file/source/index.blade.php b/tests/snapshots/dotenv-file/source/index.blade.php similarity index 100% rename from tests/snapshots/load-dotenv-file/source/index.blade.php rename to tests/snapshots/dotenv-file/source/index.blade.php diff --git a/tests/snapshots/load-dotenv-file_snapshot/index.html b/tests/snapshots/dotenv-file_snapshot/index.html similarity index 100% rename from tests/snapshots/load-dotenv-file_snapshot/index.html rename to tests/snapshots/dotenv-file_snapshot/index.html