New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modern bootstrap #97

Merged
merged 2 commits into from Feb 13, 2019

Conversation

Projects
None yet
8 participants
@dg
Copy link
Member

dg commented Feb 1, 2019

Encapsulation of bootstrap.php into class Bootstrap.

Benefits:

  • code completition:

image

  • removed unusual $container = require ...

  • Possibility to setup environment without creating container Bootstrap::boot()

  • Possibility to parameterize bootstrap Bootstrap::boot('project-name') or container Bootstrap::createContainer('project-name')

  • etc…

@SendiMyrkr

This comment has been minimized.

Copy link

SendiMyrkr commented Feb 1, 2019

I don't see any advantage when you use this static-way. You just take code and put it to another file with no benefits.

@SendiMyrkr

This comment has been minimized.

Copy link

SendiMyrkr commented Feb 1, 2019

And other risk is that someone take it bad way a wil be seduced to use it as global access to container

@dg

This comment has been minimized.

Copy link
Member Author

dg commented Feb 1, 2019

And other risk is that someone take it bad way a wil be seduced to use it as global access to container

Whole Nette is one big risk that someone uses it bad way. But what you write cannot be done.

@f3l1x

This comment has been minimized.

Copy link
Member

f3l1x commented Feb 1, 2019

I like it. Actually I have created sth similar to our latest project.

@f3l1x

This comment has been minimized.

Copy link
Member

f3l1x commented Feb 1, 2019

API

return ConfiguratorBuilder::create()
	->setJsonError500()
	->addParameters(['scope' => 'api'])
	->addEnvironmentConfig()
	->addConfig(__DIR__ . '/config/entrypoint/api.neon')
	->addConfig(__DIR__ . '/config/config.local.neon')
	->addCustomConfig()
	->createContainer();

web

return ConfiguratorBuilder::create()
	->setWebError500()
	->addParameters(['scope' => 'web'])
	->addEnvironmentConfig()
	->addConfig(__DIR__ . '/config/entrypoint/web.neon')
	->addConfig(__DIR__ . '/config/config.local.neon')
	->addCustomConfig()
	->createContainer();
@bckp

This comment has been minimized.

Copy link

bckp commented Feb 1, 2019

Same here, we have class Bootstrap, that do exactly same thing... so 👍 for me

@milo

This comment has been minimized.

Copy link
Member

milo commented Feb 1, 2019

@dg To better imagine new concept... how would look like following code?

require __DIR__ . '/../vendor/autoload.php';

$configurator = new Nette\Configurator;

# REST API
if (strpos($_SERVER['HTTP_ACCEPT'] ?? '', 'application/json') !== false) {
	$configurator->setDebugMode(false);

# CLI
} elseif (PHP_SAPI === 'cli') {
	$configurator->setDebugMode(true);

# Web
} else {
	$configurator->setDebugMode([
		'192.168.0.2', '192.168.1.2', 
	]);
}
$configurator->enableTracy(__DIR__ . '/../log', 'miloslav.hula@...');

$configurator->setTimeZone('Europe/Prague');
$configurator->setTempDirectory(__DIR__ . '/../temp');

$configurator->createRobotLoader()
	->addDirectory(__DIR__)
	->addDirectory(__DIR__ . '/../lib')
	->register();

array_push(Tracy\Debugger::getBlueScreen()->keysToHide, 'clientSecret', 'secretKey', 'signatureKey');

$configurator->addConfig(__DIR__ . '/config/config.neon');
$configurator->addConfig(__DIR__ . '/config/config.local.neon');

$container = $configurator->createContainer();

return $container;

@dg dg force-pushed the dg:pull-bootstrap branch from fdcefb9 to 7fec571 Feb 1, 2019

@dg

This comment has been minimized.

Copy link
Member Author

dg commented Feb 1, 2019

I pushed an alternative and a slightly easier solution.

@dg

This comment has been minimized.

Copy link
Member Author

dg commented Feb 1, 2019

@milo

	public static function boot(): Nette\Configurator
	{
		$configurator = new Nette\Configurator;

		# REST API
		if (strpos($_SERVER['HTTP_ACCEPT'] ?? '', 'application/json') !== false) {
			$configurator->setDebugMode(false);

		# CLI
		} elseif (PHP_SAPI === 'cli') {
			$configurator->setDebugMode(true);

		# Web
		} else {
			$configurator->setDebugMode([
				'192.168.0.2', '192.168.1.2', 
			]);
		}
		$configurator->enableTracy(__DIR__ . '/../log', 'miloslav.hula@...');

		$configurator->setTimeZone('Europe/Prague');
		$configurator->setTempDirectory(__DIR__ . '/../temp');

		$configurator->createRobotLoader()
			->addDirectory(__DIR__)
			->addDirectory(__DIR__ . '/../lib')
			->register();

		array_push(Tracy\Debugger::getBlueScreen()->keysToHide, 'clientSecret', 'secretKey', 'signatureKey');

		$configurator->addConfig(__DIR__ . '/config/config.neon');
		$configurator->addConfig(__DIR__ . '/config/config.local.neon');

		return $configurator; 
	}

@dg dg force-pushed the dg:pull-bootstrap branch 3 times, most recently from d9e3de3 to 913e584 Feb 1, 2019

@milo

This comment has been minimized.

Copy link
Member

milo commented Feb 1, 2019

@dg I see 😄 Don't know why, I thought that the Bootstrap class will be part of Nette\Application namespace.

👍 from me

@dg

This comment has been minimized.

Copy link
Member Author

dg commented Feb 1, 2019

@dg dg force-pushed the dg:pull-bootstrap branch 2 times, most recently from 00b5976 to 7c67812 Feb 1, 2019

@dg

This comment has been minimized.

Copy link
Member Author

dg commented Feb 1, 2019

Another suggestion is to rename config files to common.neon & local.neon (and maybe separate services.neon)

@fprochazka

This comment has been minimized.

Copy link
Contributor

fprochazka commented Feb 1, 2019

This might really shine if you make it easier to reuse the bootstrap between app and tests... Which do did... So 👍

$container->getByType(Nette\Application\Application::class)
App\Application::boot()
->createContainer()
->getByType(Nette\Application\Application::class)

This comment has been minimized.

@JanTvrdik

JanTvrdik Feb 1, 2019

Contributor

using two different classes named Application in one file si a terrible idea.

This comment has been minimized.

@dg

dg Feb 1, 2019

Author Member

Suggest better name.

This comment has been minimized.

@enumag

@dg dg force-pushed the dg:pull-bootstrap branch from 7c67812 to c663eb7 Feb 1, 2019

@dg dg force-pushed the nette:master branch from c5ae051 to 2875863 Feb 10, 2019

@dg dg force-pushed the dg:pull-bootstrap branch from c663eb7 to 7442594 Feb 13, 2019

@dg dg force-pushed the dg:pull-bootstrap branch from 7442594 to 3983802 Feb 13, 2019

@dg dg merged commit 8ba13ad into nette:master Feb 13, 2019

@dg dg deleted the dg:pull-bootstrap branch Feb 13, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment