Kohana.diff

Kiall Mac Innes, 08/02/2009 09:57 am

Download (18.4 kB)

 
Kohana.php (working copy)
23 23
	public static $instance;
24 24

  
25 25
	// Output buffering level
26
	private static $buffer_level;
26
	protected static $buffer_level;
27 27

  
28 28
	// Will be set to TRUE when an exception is caught
29 29
	public static $has_error = FALSE;
......
35 35
	public static $locale;
36 36

  
37 37
	// Configuration
38
	private static $configuration;
38
	protected static $configuration;
39 39

  
40 40
	// Include paths
41
	private static $include_paths;
41
	protected static $include_paths;
42 42

  
43 43
	// Cache lifetime
44
	private static $cache_lifetime;
44
	protected static $cache_lifetime;
45 45

  
46 46
	// Internal caches and write status
47
	private static $internal_cache = array();
48
	private static $write_cache;
49
	private static $internal_cache_path;
50
	private static $internal_cache_key;
51
	private static $internal_cache_encrypt;
47
	protected static $internal_cache = array();
48
	protected static $write_cache;
49
	protected static $internal_cache_path;
50
	protected static $internal_cache_key;
51
	protected static $internal_cache_encrypt;
52 52
	
53 53
	/**
54 54
	 * Sets up the PHP environment. Adds error/exception handling, output
......
84 84
		// Define database error constant
85 85
		define('E_DATABASE_ERROR', 44);
86 86

  
87
		if (self::$cache_lifetime = self::config('core.internal_cache'))
87
		if (Kohana::$cache_lifetime = Kohana::config('core.internal_cache'))
88 88
		{
89 89
			// Are we using encryption for caches?
90
			self::$internal_cache_encrypt	= self::config('core.internal_cache_encrypt');
90
			Kohana::$internal_cache_encrypt	= Kohana::config('core.internal_cache_encrypt');
91 91
			
92
			if(self::$internal_cache_encrypt===TRUE)
92
			if(Kohana::$internal_cache_encrypt===TRUE)
93 93
			{
94
				self::$internal_cache_key = self::config('core.internal_cache_key');
94
				Kohana::$internal_cache_key = Kohana::config('core.internal_cache_key');
95 95
				
96 96
				// Be sure the key is of acceptable length for the mcrypt algorithm used
97
				self::$internal_cache_key = substr(self::$internal_cache_key, 0, 24);
97
				Kohana::$internal_cache_key = substr(Kohana::$internal_cache_key, 0, 24);
98 98
			}
99 99
			
100 100
			// Set the directory to be used for the internal cache
101
			if ( ! self::$internal_cache_path = self::config('core.internal_cache_path'))
101
			if ( ! Kohana::$internal_cache_path = Kohana::config('core.internal_cache_path'))
102 102
			{
103
				self::$internal_cache_path = APPPATH.'cache/';
103
				Kohana::$internal_cache_path = APPPATH.'cache/';
104 104
			}
105 105

  
106 106
			// Load cached configuration and language files
107
			self::$internal_cache['configuration'] = self::cache('configuration', self::$cache_lifetime);
108
			self::$internal_cache['language']      = self::cache('language', self::$cache_lifetime);
107
			Kohana::$internal_cache['configuration'] = Kohana::cache('configuration', Kohana::$cache_lifetime);
108
			Kohana::$internal_cache['language']      = Kohana::cache('language', Kohana::$cache_lifetime);
109 109

  
110 110
			// Load cached file paths
111
			self::$internal_cache['find_file_paths'] = self::cache('find_file_paths', self::$cache_lifetime);
111
			Kohana::$internal_cache['find_file_paths'] = Kohana::cache('find_file_paths', Kohana::$cache_lifetime);
112 112

  
113 113
			// Enable cache saving
114 114
			Event::add('system.shutdown', array(__CLASS__, 'internal_cache_save'));
......
119 119

  
120 120
		if (function_exists('date_default_timezone_set'))
121 121
		{
122
			$timezone = self::config('locale.timezone');
122
			$timezone = Kohana::config('locale.timezone');
123 123

  
124 124
			// Set default timezone, due to increased validation of date settings
125 125
			// which cause massive amounts of E_NOTICEs to be generated in PHP 5.2+
......
133 133
		ob_start(array(__CLASS__, 'output_buffer'));
134 134

  
135 135
		// Save buffering level
136
		self::$buffer_level = ob_get_level();
136
		Kohana::$buffer_level = ob_get_level();
137 137

  
138 138
		// Set autoloader
139 139
		spl_autoload_register(array('Kohana', 'auto_load'));
......
154 154
		Kohana_PHP_Exception::enable();
155 155

  
156 156
		// Load locales
157
		$locales = self::config('locale.language');
157
		$locales = Kohana::config('locale.language');
158 158

  
159 159
		// Make first locale the defined Kohana charset
160 160
		$locales[0] .= '.'.Kohana::CHARSET;
161 161

  
162 162
		// Set locale information
163
		self::$locale = setlocale(LC_ALL, $locales);
163
		Kohana::$locale = setlocale(LC_ALL, $locales);
164 164
		
165 165
		// Set locale for the I18n system
166
		I18n::set_locale(self::$locale);
166
		I18n::set_locale(Kohana::$locale);
167 167

  
168 168
		// Enable Kohana routing
169 169
		Event::add('system.routing', array('Router', 'find_uri'));
......
178 178
		// Enable Kohana output handling
179 179
		Event::add('system.shutdown', array('Kohana', 'shutdown'));
180 180

  
181
		if (self::config('core.enable_hooks') === TRUE)
181
		if (Kohana::config('core.enable_hooks') === TRUE)
182 182
		{
183 183
			// Find all the hook files
184
			$hooks = self::list_files('hooks', TRUE);
184
			$hooks = Kohana::list_files('hooks', TRUE);
185 185

  
186 186
			foreach ($hooks as $file)
187 187
			{
......
208 208
	 */
209 209
	public static function & instance()
210 210
	{
211
		if (self::$instance === NULL)
211
		if (Kohana::$instance === NULL)
212 212
		{
213 213
			Benchmark::start(SYSTEM_BENCHMARK.'_controller_setup');
214 214

  
......
287 287
			Benchmark::stop(SYSTEM_BENCHMARK.'_controller_execution');
288 288
		}
289 289

  
290
		return self::$instance;
290
		return Kohana::$instance;
291 291
	}
292 292

  
293 293
	/**
......
302 302
		if ($process === TRUE)
303 303
		{
304 304
			// Add APPPATH as the first path
305
			self::$include_paths = array(APPPATH);
305
			Kohana::$include_paths = array(APPPATH);
306 306

  
307
			foreach (self::$configuration['core']['extensions'] as $path)
307
			foreach (Kohana::$configuration['core']['extensions'] as $path)
308 308
			{
309 309
				if ($path = str_replace('\\', '/', realpath($path)))
310 310
				{
311 311
					// Add a valid path
312
					self::$include_paths[] = $path.'/';
312
					Kohana::$include_paths[] = $path.'/';
313 313
				}
314 314
			}
315 315

  
316 316
			// Add SYSPATH as the last path
317
			self::$include_paths[] = SYSPATH;
317
			Kohana::$include_paths[] = SYSPATH;
318 318
		}
319 319

  
320
		return self::$include_paths;
320
		return Kohana::$include_paths;
321 321
	}
322 322

  
323 323
	/**
......
330 330
	 */
331 331
	public static function config($key, $slash = FALSE, $required = TRUE)
332 332
	{
333
		if (self::$configuration === NULL)
333
		if (Kohana::$configuration === NULL)
334 334
		{
335 335
			// Load core configuration
336
			self::$configuration['core'] = self::config_load('core');
336
			Kohana::$configuration['core'] = Kohana::config_load('core');
337 337

  
338 338
			// Re-parse the include paths
339
			self::include_paths(TRUE);
339
			Kohana::include_paths(TRUE);
340 340
		}
341 341

  
342 342
		// Get the group name from the key
343 343
		$group = explode('.', $key, 2);
344 344
		$group = $group[0];
345 345

  
346
		if ( ! isset(self::$configuration[$group]))
346
		if ( ! isset(Kohana::$configuration[$group]))
347 347
		{
348 348
			// Load the configuration group
349
			self::$configuration[$group] = self::config_load($group, $required);
349
			Kohana::$configuration[$group] = Kohana::config_load($group, $required);
350 350
		}
351 351

  
352 352
		// Get the value of the key string
353
		$value = self::key_string(self::$configuration, $key);
353
		$value = Kohana::key_string(Kohana::$configuration, $key);
354 354

  
355 355
		if ($slash === TRUE AND is_string($value) AND $value !== '')
356 356
		{
......
371 371
	public static function config_set($key, $value)
372 372
	{
373 373
		// Do this to make sure that the config array is already loaded
374
		self::config($key);
374
		Kohana::config($key);
375 375

  
376 376
		if (substr($key, 0, 7) === 'routes.')
377 377
		{
......
385 385
		}
386 386

  
387 387
		// Used for recursion
388
		$conf =& self::$configuration;
388
		$conf =& Kohana::$configuration;
389 389
		$last = count($keys) - 1;
390 390

  
391 391
		foreach ($keys as $i => $k)
......
403 403
		if ($key === 'core.extensions')
404 404
		{
405 405
			// Reprocess the include paths
406
			self::include_paths(TRUE);
406
			Kohana::include_paths(TRUE);
407 407
		}
408 408

  
409 409
		return TRUE;
......
432 432
			return $config;
433 433
		}
434 434

  
435
		if (isset(self::$internal_cache['configuration'][$name]))
436
			return self::$internal_cache['configuration'][$name];
435
		if (isset(Kohana::$internal_cache['configuration'][$name]))
436
			return Kohana::$internal_cache['configuration'][$name];
437 437

  
438 438
		// Load matching configs
439 439
		$configuration = array();
440 440

  
441
		if ($files = self::find_file('config', $name, $required))
441
		if ($files = Kohana::find_file('config', $name, $required))
442 442
		{
443 443
			foreach ($files as $file)
444 444
			{
......
452 452
			}
453 453
		}
454 454

  
455
		if ( ! isset(self::$write_cache['configuration']))
455
		if ( ! isset(Kohana::$write_cache['configuration']))
456 456
		{
457 457
			// Cache has changed
458
			self::$write_cache['configuration'] = TRUE;
458
			Kohana::$write_cache['configuration'] = TRUE;
459 459
		}
460 460

  
461
		return self::$internal_cache['configuration'][$name] = $configuration;
461
		return Kohana::$internal_cache['configuration'][$name] = $configuration;
462 462
	}
463 463

  
464 464
	/**
......
470 470
	public static function config_clear($group)
471 471
	{
472 472
		// Remove the group from config
473
		unset(self::$configuration[$group], self::$internal_cache['configuration'][$group]);
473
		unset(Kohana::$configuration[$group], Kohana::$internal_cache['configuration'][$group]);
474 474

  
475
		if ( ! isset(self::$write_cache['configuration']))
475
		if ( ! isset(Kohana::$write_cache['configuration']))
476 476
		{
477 477
			// Cache has changed
478
			self::$write_cache['configuration'] = TRUE;
478
			Kohana::$write_cache['configuration'] = TRUE;
479 479
		}
480 480
	}
481 481

  
......
491 491
	{
492 492
		if ($lifetime > 0)
493 493
		{
494
			$path = self::$internal_cache_path.'kohana_'.$name;
494
			$path = Kohana::$internal_cache_path.'kohana_'.$name;
495 495

  
496 496
			if (is_file($path))
497 497
			{
......
499 499
				if ((time() - filemtime($path)) < $lifetime)
500 500
				{
501 501
					// Cache is valid! Now, do we need to decrypt it?
502
					if(self::$internal_cache_encrypt===TRUE)
502
					if(Kohana::$internal_cache_encrypt===TRUE)
503 503
					{
504 504
						$data		= file_get_contents($path);
505 505
						
506 506
						$iv_size	= mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
507 507
						$iv			= mcrypt_create_iv($iv_size, MCRYPT_RAND);
508 508
						
509
						$decrypted_text	= mcrypt_decrypt(MCRYPT_RIJNDAEL_256, self::$internal_cache_key, $data, MCRYPT_MODE_ECB, $iv);
509
						$decrypted_text	= mcrypt_decrypt(MCRYPT_RIJNDAEL_256, Kohana::$internal_cache_key, $data, MCRYPT_MODE_ECB, $iv);
510 510
						
511 511
						$cache	= unserialize($decrypted_text);
512 512
						
......
548 548
		if ($lifetime < 1)
549 549
			return FALSE;
550 550

  
551
		$path = self::$internal_cache_path.'kohana_'.$name;
551
		$path = Kohana::$internal_cache_path.'kohana_'.$name;
552 552

  
553 553
		if ($data === NULL)
554 554
		{
......
558 558
		else
559 559
		{
560 560
			// Using encryption? Encrypt the data when we write it
561
			if(self::$internal_cache_encrypt===TRUE)
561
			if(Kohana::$internal_cache_encrypt===TRUE)
562 562
			{
563 563
				// Encrypt and write data to cache file
564 564
				$iv_size	= mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
565 565
				$iv			= mcrypt_create_iv($iv_size, MCRYPT_RAND);
566 566
				
567 567
				// Serialize and encrypt!
568
				$encrypted_text	= mcrypt_encrypt(MCRYPT_RIJNDAEL_256, self::$internal_cache_key, serialize($data), MCRYPT_MODE_ECB, $iv);
568
				$encrypted_text	= mcrypt_encrypt(MCRYPT_RIJNDAEL_256, Kohana::$internal_cache_key, serialize($data), MCRYPT_MODE_ECB, $iv);
569 569
				
570 570
				return (bool) file_put_contents($path, $encrypted_text);
571 571
			}
......
593 593
		}
594 594

  
595 595
		// Set final output
596
		self::$output = $output;
596
		Kohana::$output = $output;
597 597

  
598 598
		// Set and return the final output
599
		return self::$output;
599
		return Kohana::$output;
600 600
	}
601 601

  
602 602
	/**
......
608 608
	 */
609 609
	public static function close_buffers($flush = TRUE)
610 610
	{
611
		if (ob_get_level() >= self::$buffer_level)
611
		if (ob_get_level() >= Kohana::$buffer_level)
612 612
		{
613 613
			// Set the close function
614 614
			$close = ($flush === TRUE) ? 'ob_end_flush' : 'ob_end_clean';
615 615

  
616
			while (ob_get_level() > self::$buffer_level)
616
			while (ob_get_level() > Kohana::$buffer_level)
617 617
			{
618 618
				// Flush or clean the buffer
619 619
				$close();
......
632 632
	public static function shutdown()
633 633
	{
634 634
		// Close output buffers
635
		self::close_buffers(TRUE);
635
		Kohana::close_buffers(TRUE);
636 636

  
637 637
		// Run the output event
638
		Event::run('system.display', self::$output);
638
		Event::run('system.display', Kohana::$output);
639 639

  
640 640
		// Render the final output
641
		self::render(self::$output);
641
		Kohana::render(Kohana::$output);
642 642
	}
643 643

  
644 644
	/**
......
649 649
	 */
650 650
	public static function render($output)
651 651
	{
652
		if (self::config('core.render_stats') === TRUE)
652
		if (Kohana::config('core.render_stats') === TRUE)
653 653
		{
654 654
			// Fetch memory usage in MB
655 655
			$memory = function_exists('memory_get_usage') ? (memory_get_usage() / 1024 / 1024) : 0;
......
679 679
			);
680 680
		}
681 681

  
682
		if ($level = self::config('core.output_compression') AND ini_get('output_handler') !== 'ob_gzhandler' AND (int) ini_get('zlib.output_compression') === 0)
682
		if ($level = Kohana::config('core.output_compression') AND ini_get('output_handler') !== 'ob_gzhandler' AND (int) ini_get('zlib.output_compression') === 0)
683 683
		{
684 684
			if ($compress = request::preferred_encoding(array('gzip','deflate'), TRUE))
685 685
			{
......
773 773
			$file = $class;
774 774
		}
775 775

  
776
		if ($filename = self::find_file($type, $file))
776
		if ($filename = Kohana::find_file($type, $file))
777 777
		{
778 778
			// Load the class
779 779
			require $filename;
......
784 784
			return FALSE;
785 785
		}
786 786

  
787
		if ($filename = self::find_file($type, self::$configuration['core']['extension_prefix'].$class))
787
		if ($filename = Kohana::find_file($type, Kohana::$configuration['core']['extension_prefix'].$class))
788 788
		{
789 789
			// Load the class extension
790 790
			require $filename;
......
873 873
		// Search path
874 874
		$search = $directory.'/'.$filename.$ext;
875 875

  
876
		if (isset(self::$internal_cache['find_file_paths'][$search]))
877
			return self::$internal_cache['find_file_paths'][$search];
876
		if (isset(Kohana::$internal_cache['find_file_paths'][$search]))
877
			return Kohana::$internal_cache['find_file_paths'][$search];
878 878

  
879 879
		// Load include paths
880
		$paths = self::$include_paths;
880
		$paths = Kohana::$include_paths;
881 881

  
882 882
		// Nothing found, yet
883 883
		$found = NULL;
......
919 919
				$directory = 'core.'.inflector::singular($directory);
920 920

  
921 921
				// If the file is required, throw an exception
922
				throw new Kohana_Exception('The requested :resource:, :file:, could not be found', array(':resource:' => self::message($directory), ':file:' =>$filename));
922
				throw new Kohana_Exception('The requested :resource:, :file:, could not be found', array(':resource:' => Kohana::message($directory), ':file:' =>$filename));
923 923
			}
924 924
			else
925 925
			{
......
928 928
			}
929 929
		}
930 930

  
931
		if ( ! isset(self::$write_cache['find_file_paths']))
931
		if ( ! isset(Kohana::$write_cache['find_file_paths']))
932 932
		{
933 933
			// Write cache at shutdown
934
			self::$write_cache['find_file_paths'] = TRUE;
934
			Kohana::$write_cache['find_file_paths'] = TRUE;
935 935
		}
936 936

  
937
		return self::$internal_cache['find_file_paths'][$search] = $found;
937
		return Kohana::$internal_cache['find_file_paths'][$search] = $found;
938 938
	}
939 939

  
940 940
	/**
......
951 951

  
952 952
		if ($path === FALSE)
953 953
		{
954
			$paths = array_reverse(self::include_paths());
954
			$paths = array_reverse(Kohana::include_paths());
955 955

  
956 956
			foreach ($paths as $path)
957 957
			{
958 958
				// Recursively get and merge all files
959
				$files = array_merge($files, self::list_files($directory, $recursive, $path.$directory));
959
				$files = array_merge($files, Kohana::list_files($directory, $recursive, $path.$directory));
960 960
			}
961 961
		}
962 962
		else
......
980 980
							$item = pathinfo($item, PATHINFO_BASENAME);
981 981

  
982 982
							// Append sub-directory search
983
							$files = array_merge($files, self::list_files($directory, TRUE, $path.$item));
983
							$files = array_merge($files, Kohana::list_files($directory, TRUE, $path.$item));
984 984
						}
985 985
					}
986 986
				}
......
1004 1004
		$group = $group[0];
1005 1005

  
1006 1006
		// Get locale name
1007
		$locale = self::config('locale.language.0');
1007
		$locale = Kohana::config('locale.language.0');
1008 1008

  
1009
		if ( ! isset(self::$internal_cache['messages'][$group]))
1009
		if ( ! isset(Kohana::$internal_cache['messages'][$group]))
1010 1010
		{
1011 1011
			// Messages for this group
1012 1012
			$messages = array();
1013 1013

  
1014
			if ($file = self::find_file('messages', $group))
1014
			if ($file = Kohana::find_file('messages', $group))
1015 1015
			{
1016 1016
				include $file[0];
1017 1017
			}
1018 1018

  
1019
			if ( ! isset(self::$write_cache['messages']))
1019
			if ( ! isset(Kohana::$write_cache['messages']))
1020 1020
			{
1021 1021
				// Write language cache
1022
				self::$write_cache['messages'] = TRUE;
1022
				Kohana::$write_cache['messages'] = TRUE;
1023 1023
			}
1024 1024

  
1025
			self::$internal_cache['messages'][$group] = $messages;
1025
			Kohana::$internal_cache['messages'][$group] = $messages;
1026 1026
		}
1027 1027

  
1028 1028
		// Get the line from cache
1029
		$line = self::key_string(self::$internal_cache['messages'], $key);
1029
		$line = Kohana::key_string(Kohana::$internal_cache['messages'], $key);
1030 1030

  
1031 1031
		if ($line === NULL)
1032 1032
		{
......
1246 1246
							$arg = Kohana::debug_path($arg);
1247 1247
						}
1248 1248

  
1249
						$args[] = '<code>'.text::limit_chars(html::specialchars(self::debug_var($arg)), 50, '...').'</code>';
1249
						$args[] = '<code>'.text::limit_chars(html::specialchars(Kohana::debug_var($arg)), 50, '...').'</code>';
1250 1250
					}
1251 1251
				}
1252 1252

  
......
1329 1329
						$out .= ($more === TRUE ? ', ' : '').$property->getName().' => ';
1330 1330
						if ($property->isPublic())
1331 1331
						{
1332
							$out .= self::debug_var($property->getValue($var), TRUE);
1332
							$out .= Kohana::debug_var($property->getValue($var), TRUE);
1333 1333
						}
1334 1334
						elseif ($property->isPrivate())
1335 1335
						{
......
1350 1350
				{
1351 1351
					if ( ! is_int($key))
1352 1352
					{
1353
						$key = self::debug_var($key, TRUE).' => ';
1353
						$key = Kohana::debug_var($key, TRUE).' => ';
1354 1354
					}
1355 1355
					else
1356 1356
					{
1357 1357
						$key = '';
1358 1358
					}
1359
					$out .= ($more ? ', ' : '').$key.self::debug_var($val, TRUE);
1359
					$out .= ($more ? ', ' : '').$key.Kohana::debug_var($val, TRUE);
1360 1360
					$more = TRUE;
1361 1361
				}
1362 1362
				return $out.')';
......
1378 1378
	 */
1379 1379
	public static function internal_cache_save()
1380 1380
	{
1381
		if ( ! is_array(self::$write_cache))
1381
		if ( ! is_array(Kohana::$write_cache))
1382 1382
			return FALSE;
1383 1383

  
1384 1384
		// Get internal cache names
1385
		$caches = array_keys(self::$write_cache);
1385
		$caches = array_keys(Kohana::$write_cache);
1386 1386

  
1387 1387
		// Nothing written
1388 1388
		$written = FALSE;
1389 1389

  
1390 1390
		foreach ($caches as $cache)
1391 1391
		{
1392
			if (isset(self::$internal_cache[$cache]))
1392
			if (isset(Kohana::$internal_cache[$cache]))
1393 1393
			{
1394 1394
				// Write the cache file
1395
				self::cache_save($cache, self::$internal_cache[$cache], self::$configuration['core']['internal_cache']);
1395
				Kohana::cache_save($cache, Kohana::$internal_cache[$cache], Kohana::$configuration['core']['internal_cache']);
1396 1396

  
1397 1397
				// A cache has been written
1398 1398
				$written = TRUE;
......
1603 1603

  
1604 1604
				ob_start();
1605 1605

  
1606
				if ( ! self::$error_resources)
1606
				if ( ! Kohana::$error_resources)
1607 1607
				{
1608 1608
					// Include error style
1609 1609
					echo '<style type="text/css">', "\n";
......
1616 1616
					echo "\n", '</script>', "\n";
1617 1617

  
1618 1618
					// Error resources have been loaded
1619
					self::$error_resources = TRUE;
1619
					Kohana::$error_resources = TRUE;
1620 1620
				}
1621 1621

  
1622 1622
				require Kohana::find_file('views', 'kohana/error', TRUE);