PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/C4/Core/Framework.php

https://bitbucket.org/paulscott56/c4-new
PHP | 234 lines | 165 code | 36 blank | 33 comment | 3 complexity | d3ca896cc28f7d4fd815a1cd23e51031 MD5 | raw file
  1. <?php
  2. namespace C4\Core;
  3. use Symfony\Component\Routing;
  4. use Symfony\Component\HttpKernel;
  5. use Symfony\Component\HttpFoundation\Session;
  6. use Symfony\Component\HttpFoundation\SessionStorage\SessionStorageInterface;
  7. use Symfony\Component\EventDispatcher\EventDispatcher;
  8. use Symfony\Component\Yaml\Dumper;
  9. use Symfony\Component\Yaml\Parser;
  10. use Symfony\Component\Yaml\Exception\ParseException;
  11. use Monolog\Handler\StreamHandler;
  12. use Monolog\Handler\FirePHPHandler;
  13. use Doctrine\ORM\Tools\Setup;
  14. use Doctrine\ORM\EntityManager;
  15. use Doctrine\Common\ClassLoader;
  16. use Doctrine\Common\Annotations\AnnotationReader;
  17. use Doctrine\ODM\MongoDB\DocumentManager;
  18. use Doctrine\MongoDB\Connection;
  19. use Doctrine\ODM\MongoDB\Configuration;
  20. use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver;
  21. use Assetic\Asset\AssetCollection;
  22. use Assetic\Asset\FileAsset;
  23. use Assetic\Filter\Yui\JsCompressorFilter as YuiCompressorFilter;
  24. class Framework extends HttpKernel\HttpKernel
  25. {
  26. public static $mainConfiguration;
  27. public $yamlParser;
  28. private $logger;
  29. public $entityManager;
  30. public $documentManager;
  31. public function __construct($routes, $logger)
  32. {
  33. // get the main configuration
  34. $this->parseMainConfiguration();
  35. $this->logger = $logger;
  36. $this->logger->pushHandler(new StreamHandler(__DIR__.'/../../../logging/System_Log.log', \Monolog\Logger::DEBUG));
  37. $this->logger->pushHandler(new FirePHPHandler());
  38. $this->logger->addInfo('My logger is now ready');
  39. try {
  40. $context = new Routing\RequestContext();
  41. $matcher = new Routing\Matcher\UrlMatcher($routes, $context);
  42. $resolver = new HttpKernel\Controller\ControllerResolver();
  43. $dispatcher = new EventDispatcher();
  44. $dispatcher->addSubscriber(new HttpKernel\EventListener\RouterListener($matcher));
  45. $dispatcher->addSubscriber(new HttpKernel\EventListener\ResponseListener('UTF-8'));
  46. $dispatcher->addSubscriber(new ExceptionListener(function (Request $request)
  47. {
  48. $msg = 'Something went wrong! ('.$request->get('exception')->getMessage().')';
  49. return new Response($msg, 500);
  50. }
  51. ));
  52. // get ORM
  53. $this->getORM();
  54. $this->getMongoODM();
  55. // log visitor
  56. $this->logVisitor();
  57. // wrap up the JS
  58. $this->js = new AssetCollection(array(
  59. new FileAsset(__DIR__.'/../../../assets/js/bootstrap.js'),
  60. //new FileAsset(__DIR__.'/../../../assets/css/bootstrap.css'),
  61. ), array(
  62. new YuiCompressorFilter(__DIR__.'/../../../assets/yuicompressor-2.4.7/build/yuicompressor-2.4.7.jar'),
  63. ));
  64. //header('Content-Type: application/js');
  65. //echo '<script type="text/javascript">'.$this->js->dump().'</script>';
  66. }
  67. catch(Exception $e)
  68. {
  69. echo "oops";
  70. }
  71. parent::__construct($dispatcher, $resolver);
  72. }
  73. public function parseMainConfiguration()
  74. {
  75. try {
  76. // The YAML parser object can be re-used, so we instantiate it here
  77. $this->yamlParser = new Parser();
  78. self::$mainConfiguration = $this->yamlParser->parse(file_get_contents(__DIR__.'/../../../config/systemConfig.yml'));
  79. } catch (ParseException $e) {
  80. printf("Unable to parse the YAML string: %s", $e->getMessage());
  81. die();
  82. }
  83. return $this;
  84. }
  85. public function parseGeneralConfiguration($configFile)
  86. {
  87. try {
  88. $configuration = $this->yamlParser->parse(
  89. file_get_contents(__DIR__.'/../../../config/'.$configFile.'.yml')
  90. );
  91. return $configuration;
  92. } catch (ParseException $e) {
  93. printf("Unable to parse the YAML string: %s", $e->getMessage());
  94. }
  95. }
  96. public function yamlWriter(array $data, $filename)
  97. {
  98. $dumper = new Dumper();
  99. $yaml = $dumper->dump($data);
  100. file_put_contents(__DIR__.'/../../../config/'.$filename.'.yml', $yaml);
  101. }
  102. public static function getConfiguration()
  103. {
  104. return self::$mainConfiguration;
  105. }
  106. /**
  107. * Set PHP configuration settings
  108. *
  109. * @param array $settings
  110. * @return Framework
  111. */
  112. public function setPhpSettings(array $settings)
  113. {
  114. foreach ($settings as $key => $value) {
  115. if (is_scalar($value)) {
  116. ini_set($key, $value);
  117. }
  118. }
  119. return $this;
  120. }
  121. /**
  122. * Set include path
  123. *
  124. * @param array $paths
  125. * @return Framework
  126. */
  127. public function setIncludePaths(array $paths)
  128. {
  129. $path = implode(PATH_SEPARATOR, $paths);
  130. set_include_path($path . PATH_SEPARATOR . get_include_path());
  131. return $this;
  132. }
  133. private function getMongoODM()
  134. {
  135. $config = new \Doctrine\ODM\MongoDB\Configuration();
  136. $config->setProxyDir('/var/www/c4/cache');
  137. $config->setProxyNamespace('Proxies');
  138. $config->setHydratorDir('/var/www/c4/cache');
  139. $config->setHydratorNamespace('Hydrators');
  140. $reader = new \Doctrine\Common\Annotations\AnnotationReader();
  141. $config->setMetadataDriverImpl(new AnnotationDriver($reader, __DIR__ . '/Documents'));
  142. AnnotationDriver::registerAnnotationClasses();
  143. //$reader->setDefaultAnnotationNamespace('Doctrine\ODM\MongoDB\Mapping\Annotations\\');
  144. //var_dump($reader);
  145. $dm = \Doctrine\ODM\MongoDB\DocumentManager::create(new Connection(), $config);
  146. $this->documentManager = $dm;
  147. return $this;
  148. }
  149. private function getORM()
  150. {
  151. // set up the database connection
  152. $loader = new \Doctrine\Common\ClassLoader("Doctrine");
  153. $loader->register();
  154. $dbParams = array(
  155. 'driver' => 'pdo_mysql',
  156. 'user' => 'root',
  157. 'password' => '',
  158. 'dbname' => 'c4'
  159. );
  160. $path = array(__DIR__ . '/entities');
  161. $config = Setup::createAnnotationMetadataConfiguration($path, true);
  162. $this->entityManager = EntityManager::create($dbParams, $config);
  163. return $this;
  164. }
  165. /**
  166. * Grabs the client IP address
  167. *
  168. * This function should be used to grab IP addresses, even those behind proxies, to gather data from
  169. *
  170. * @return string $ip
  171. */
  172. private function getIpAddr() {
  173. if(!empty($_SERVER['HTTP_CLIENT_IP'])) {
  174. $ip = $_SERVER['HTTP_CLIENT_IP'];
  175. }
  176. elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  177. // pass from proxy
  178. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  179. }
  180. else {
  181. $ip = $_SERVER['REMOTE_ADDR'];
  182. }
  183. return $ip;
  184. }
  185. public function logVisitor()
  186. {
  187. // test user
  188. $hitCount = new Model\VisitCounter();
  189. $hitCount->setIp($this->getIpAddr());
  190. $hitCount->setCounter(1);
  191. $hitCount->setHostname();
  192. $hitCount->incrementCounter();
  193. // persist
  194. $this->documentManager->persist($hitCount);
  195. $this->documentManager->flush();
  196. return true;
  197. }
  198. }