<commit_before><?php namespace Bkwld\Decoy;

use App;
use Bkwld\Decoy\Observers\NotFound;
use Bkwld\Decoy\Observers\Validation;
use Bkwld\Decoy\Fields\Former\MethodDispatcher;
use Config;
use Former\Former;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Foundation\AliasLoader;
use Illuminate\Foundation\ProviderRepository;
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
use Symfony\Component\Yaml\Parser;

class ServiceProvider extends BaseServiceProvider {

	/**
	 * Bootstrap the application events.
	 *
	 * @return void
	 */
	public function boot() {
		
		// Publish config files
		$this->publishes([
			 __DIR__.'/../../config' => config_path('decoy')
		], 'config');

		// Publish public assets
		$this->publishes([
			 __DIR__.'/../../../public' => public_path('packages/bkwld/decoy')
		], 'assets');

		// Publish migrations
		$this->publishes([
			__DIR__.'/../../migrations/' => database_path('migrations')
		], 'migrations');

		// Register views
		$this->loadViewsFrom(__DIR__.'/../../views', 'decoy');

		// Define constants that Decoy uses
		if (!defined('FORMAT_DATE'))     define('FORMAT_DATE', 'm/d/y');
		if (!defined('FORMAT_DATETIME')) define('FORMAT_DATETIME', 'm/d/y g:i a T');
		if (!defined('FORMAT_TIME'))     define('FORMAT_TIME', 'g:i a T');		

		// Register the routes AFTER all the app routes using the "before" register.  
		// Unless the app is running via the CLI where we want the routes reigsterd 
		// for URL generation.
		if (App::runningInConsole()) $router->registerAll();
		else $this->app->before(array($router, 'registerAll'));

		// Do bootstrapping that only matters if user has requested an admin URL
		if ($this->app['decoy']->handling()) $this->usingAdmin();

		// Wire up model event callbacks even if request is not for admin.  Do this
		// after the usingAdmin call so that the callbacks run after models are
		// mutated by Decoy logic.  This is important, in particular, so the
		// Validation observer can alter validation rules before the onValidation
		// callback runs.
		$this->app['events']->listen('eloquent.*',     'Bkwld\Decoy\Observers\ModelCallbacks');
		$this->app['events']->listen('decoy::model.*', 'Bkwld\Decoy\Observers\ModelCallbacks');

		// If logging changes hasn't been disabled, log all model events.  This
		// should come after the callbacks in case they modify the record before
		// being saved.  And we're logging ONLY admin actions, thus the handling
		// condition.
		if ($this->app['decoy']->handling() && Config::get('decoy.site.log_changes')) {
			$this->app['events']->listen('eloquent.*', 'Bkwld\Decoy\Observers\Changes');
		}		
	}

	/**
	 * Things that happen only if the request is for the admin
	 */
	public function usingAdmin() {
		
		// Load all the composers
		require_once(__DIR__.'/../../composers/layouts._breadcrumbs.php');
		require_once(__DIR__.'/../../composers/layouts._nav.php');
		require_once(__DIR__.'/../../composers/shared.list._search.php');

		// Use Bootstrap 3
		Config::set('former.framework', 'TwitterBootstrap3');

		// Reduce the horizontal form's label width
		Config::set('former.TwitterBootstrap3.labelWidths', []);

		// Change Former's required field HTML
		Config::set('former.required_text', ' <span class="glyphicon glyphicon-exclamation-sign js-tooltip required" title="Required field"></span>');

		// Make pushed checkboxes have an empty string as their value
		Config::set('former.unchecked_value', '');

		// Add Decoy's custom Fields to Former so they can be invoked using the "Former::"
		// namespace and so we can take advantage of sublassing Former's Field class.
		$this->app->make('former.dispatcher')->addRepository('Bkwld\Decoy\Fields\\');

		// Use the Decoy paginator
		Config::set('view.pagination', 'decoy::shared.list._paginator');

		// Delegate events to Decoy observers
		$this->app['events']->listen('eloquent.saving:*',         'Bkwld\Decoy\Observers\Localize');
		$this->app['events']->listen('eloquent.saving:*',         'Bkwld\Decoy\Observers\Cropping@onSaving');
		$this->app['events']->listen('eloquent.deleted:*',        'Bkwld\Decoy\Observers\Cropping@onDeleted');
		$this->app['events']->listen('eloquent.saved:*',          'Bkwld\Decoy\Observers\ManyToManyChecklist');
		$this->app['events']->listen('eloquent.saving:*',         'Bkwld\Decoy\Observers\Encoding@onSaving');
		$this->app['events']->listen('eloquent.deleted:*',        'Bkwld\Decoy\Observers\Encoding@onDeleted');
		$this->app['events']->listen('decoy::model.validating:*', 'Bkwld\Decoy\Observers\Validation@onValidating');
	}

	/**<commit_msg>Group some logic into smaller methods<commit_after><?php namespace Bkwld\Decoy;

use App;
use Bkwld\Decoy\Observers\NotFound;
use Bkwld\Decoy\Observers\Validation;
use Bkwld\Decoy\Fields\Former\MethodDispatcher;
use Config;
use Former\Former;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Foundation\AliasLoader;
use Illuminate\Foundation\ProviderRepository;
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
use Symfony\Component\Yaml\Parser;

class ServiceProvider extends BaseServiceProvider {

	/**
	 * Bootstrap the application events.
	 *
	 * @return void
	 */
	public function boot() {
		
		// Publish config files
		$this->publishes([
			 __DIR__.'/../../config' => config_path('decoy')
		], 'config');

		// Publish public assets
		$this->publishes([
			 __DIR__.'/../../../public' => public_path('packages/bkwld/decoy')
		], 'assets');

		// Publish migrations
		$this->publishes([
			__DIR__.'/../../migrations/' => database_path('migrations')
		], 'migrations');

		// Register views
		$this->loadViewsFrom(__DIR__.'/../../views', 'decoy');

		// Define constants that Decoy uses
		if (!defined('FORMAT_DATE'))     define('FORMAT_DATE', 'm/d/y');
		if (!defined('FORMAT_DATETIME')) define('FORMAT_DATETIME', 'm/d/y g:i a T');
		if (!defined('FORMAT_TIME'))     define('FORMAT_TIME', 'g:i a T');		

		// Register the routes AFTER all the app routes using the "before" register.  
		// Unless the app is running via the CLI where we want the routes reigsterd 
		// for URL generation.
		if (App::runningInConsole()) $router->registerAll();
		else $this->app->before(array($router, 'registerAll'));

		// Do bootstrapping that only matters if user has requested an admin URL
		if ($this->app['decoy']->handling()) $this->usingAdmin();

		// Wire up model event callbacks even if request is not for admin.  Do this
		// after the usingAdmin call so that the callbacks run after models are
		// mutated by Decoy logic.  This is important, in particular, so the
		// Validation observer can alter validation rules before the onValidation
		// callback runs.
		$this->app['events']->listen('eloquent.*',     'Bkwld\Decoy\Observers\ModelCallbacks');
		$this->app['events']->listen('decoy::model.*', 'Bkwld\Decoy\Observers\ModelCallbacks');

		// If logging changes hasn't been disabled, log all model events.  This
		// should come after the callbacks in case they modify the record before
		// being saved.  And we're logging ONLY admin actions, thus the handling
		// condition.
		if ($this->app['decoy']->handling() && Config::get('decoy.site.log_changes')) {
			$this->app['events']->listen('eloquent.*', 'Bkwld\Decoy\Observers\Changes');
		}		
	}

	/**
	 * Things that happen only if the request is for the admin
	 */
	public function usingAdmin() {
		
		// Load all the composers
		require_once(__DIR__.'/../../composers/layouts._breadcrumbs.php');
		require_once(__DIR__.'/../../composers/layouts._nav.php');
		require_once(__DIR__.'/../../composers/shared.list._search.php');

		// Use the Decoy paginator
		Config::set('view.pagination', 'decoy::shared.list._paginator');

		// Config Former
		$this->configureFormer();

		// Delegate events to Decoy observers
		$this->delegateAdminObservers();
	}

	/**
	 * Config Former
	 *
	 * @return void 
	 */
	protected function configureFormer() {

		// Use Bootstrap 3
		Config::set('former.framework', 'TwitterBootstrap3');

		// Reduce the horizontal form's label width
		Config::set('former.TwitterBootstrap3.labelWidths', []);

		// Change Former's required field HTML
		Config::set('former.required_text', ' <span class="glyphicon glyphicon-exclamation-sign js-tooltip required" title="Required field"></span>');

		// Make pushed checkboxes have an empty string as their value
		Config::set('former.unchecked_value', '');

		// Add Decoy's custom Fields to Former so they can be invoked using the "Former::"
		// namespace and so we can take advantage of sublassing Former's Field class.
		$this->app->make('former.dispatcher')->addRepository('Bkwld\Decoy\Fields\\');
	}
	
	/**
	 * Delegate events to Decoy observers
	 *
	 * @return void
	 */
	protected function delegateAdminObservers() {
		$this->app['events']->listen('eloquent.saving:*',         'Bkwld\Decoy\Observers\Localize');
		$this->app['events']->listen('eloquent.saving:*',         'Bkwld\Decoy\Observers\Cropping@onSaving');
		$this->app['events']->listen('eloquent.deleted:*',        'Bkwld\Decoy\Observers\Cropping@onDeleted');
		$this->app['events']->listen('eloquent.saved:*',          'Bkwld\Decoy\Observers\ManyToManyChecklist');
		$this->app['events']->listen('eloquent.saving:*',         'Bkwld\Decoy\Observers\Encoding@onSaving');
		$this->app['events']->listen('eloquent.deleted:*',        'Bkwld\Decoy\Observers\Encoding@onDeleted');
		$this->app['events']->listen('decoy::model.validating:*', 'Bkwld\Decoy\Observers\Validation@onValidating');
	} 

	/**
{'fraction_non_alphanumeric': 0.1621846987700646, 'fraction_numerical': 0.0006253908692933083, 'mean_word_length': 4.065997888067582, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 85, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3086647', 'n_tokens_mistral': 3183, 'n_tokens_neox': 2953, 'n_words': 704}