Exemplo n.º 1
0
 /**
  * @return Cache
  * @throws ApplicationPartMissingException
  */
 public static function getCache()
 {
     if (!self::$cache instanceof Cache) {
         if (Configuration::get('phpframework', 'cache.enable')) {
             $cacheFolder = Configuration::get('phpframework', 'cache.folder', './cache');
             $cacheFolderPath = realpath($cacheFolder);
             if (!is_dir($cacheFolderPath)) {
                 if (!@mkdir($cacheFolder, 0777, TRUE)) {
                     throw new ApplicationPartMissingException('Cache folder "' . $cacheFolder . '" is missing and could not be created.', 1410537983);
                 }
                 $cacheFolderPath = realpath($cacheFolder);
             }
             $testfile = $cacheFolderPath . '/L7NxnrqsICAtxg0qxDWPUSA';
             @touch($testfile);
             if (file_exists($testfile)) {
                 unlink($testfile);
             } else {
                 throw new ApplicationPartMissingException('Cache folder "' . $cacheFolder . '" is not writable', 1410537933);
             }
             $storage = new FileStorage($cacheFolderPath, new FileJournal($cacheFolderPath));
         } else {
             $storage = new DevNullStorage();
         }
         self::$cache = new Cache($storage, Configuration::get('application', 'application'));
     }
     return self::$cache;
 }
 /**
  *
  */
 protected function encodeCryptCookie()
 {
     $sSecretKey = Configuration::get('phpframework', 'authentication.cookie.encrypt_key');
     $sDecrypted = json_encode($this->store);
     $data = trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $sSecretKey, $sDecrypted, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))));
     setcookie($this->cookieName, $data, time() + 31 * 86400, '/');
 }
Exemplo n.º 3
0
 /**
  * @test
  * @expectedException \AppZap\PHPFramework\Mvc\ApplicationPartMissingException
  * @expectedExceptionCode 1410537933
  */
 public function cacheFolderNotWritable()
 {
     Configuration::reset();
     Configuration::set('phpframework', 'cache.enable', TRUE);
     Configuration::set('phpframework', 'cache.folder', '/');
     $cache = CacheFactory::getCache();
     $this->assertTrue($cache->getStorage() instanceof FileStorage);
 }
Exemplo n.º 4
0
 /**
  * @test
  */
 public function applicationFolderBothFiles()
 {
     DefaultConfiguration::initialize('_both');
     IniParser::initialize();
     $this->assertSame('b', Configuration::get('unittest', 'foo'));
     $this->assertSame('42', Configuration::get('unittest', 'bar'));
     $this->assertNull(Configuration::get('unittest', 'baz'));
 }
Exemplo n.º 5
0
 /**
  * @param AbstractView $response
  */
 public static function contentTypeHeader(AbstractView $response)
 {
     $contentType = 'text/html';
     $charset = Configuration::get('phpframework', 'output_charset', 'utf-8');
     if ($charset) {
         $contentType .= '; charset=' . trim($charset);
     }
     $response->header('Content-Type', $contentType);
 }
Exemplo n.º 6
0
 /**
  * @param string $file
  */
 protected static function parseFile($file)
 {
     $config = parse_ini_file($file, TRUE);
     foreach ($config as $section => $sectionConfiguration) {
         foreach ($sectionConfiguration as $key => $value) {
             Configuration::set($section, $key, $value);
         }
     }
 }
Exemplo n.º 7
0
 /**
  * @test
  */
 public function maximumDev()
 {
     Configuration::set('phpframework', 'version', '2.0-dev');
     $this->assertTrue(Version::maximum(2, 0));
     $this->assertTrue(Version::maximum(2, 5));
     $this->assertFalse(Version::maximum(1, 99));
     Configuration::set('phpframework', 'version', '12.5-dev');
     $this->assertTrue(Version::maximum(12, 5));
     $this->assertTrue(Version::maximum(12, 99));
     $this->assertFalse(Version::maximum(12, 0));
 }
 /**
  * @return bool
  */
 protected function isAuthenticated()
 {
     if ($this->name === NULL || $this->password === NULL) {
         return FALSE;
     }
     $httpAuthentication = Configuration::getSection('phpframework', 'authentication.http');
     if (!array_key_exists($this->name, $httpAuthentication)) {
         return FALSE;
     }
     return sha1($this->password) === $httpAuthentication[$this->name];
 }
Exemplo n.º 9
0
 /**
  * Connects to the MySQL server, sets the charset for the connection and
  * selects the database
  */
 public function connect()
 {
     if (!$this->connection instanceof \PDO) {
         $dbConfiguration = Configuration::getSection('phpframework', 'db');
         $dsn = 'mysql:host=' . $dbConfiguration['mysql.host'] . ';dbname=' . $dbConfiguration['mysql.database'];
         $this->connection = new \PDO($dsn, $dbConfiguration['mysql.user'], $dbConfiguration['mysql.password']);
         if (isset($dbConfiguration['charset'])) {
             $this->setCharset($dbConfiguration['charset']);
         }
     }
 }
 /**
  *
  */
 public static function initialize()
 {
     $airbrake_ini = Configuration::getSection('phpframework-airbrake');
     if (!is_null($airbrake_ini) && Configuration::get('phpframework-airbrake', 'enable', FALSE)) {
         $apiKey = $airbrake_ini['api_key'];
         $airbrake_environment = isset($airbrake_ini['environment']) ? $airbrake_ini['environment'] : 'NO_ENVIRONMENT_SET';
         $options = ['secure' => TRUE, 'host' => $airbrake_ini['host'], 'resource' => $airbrake_ini['resource'], 'timeout' => 10, 'environmentName' => Configuration::get('application', 'application') . ' / ' . $airbrake_environment];
         \Airbrake\EventHandler::start($apiKey, FALSE, $options);
         $config = new \Airbrake\Configuration($apiKey, $options);
         self::$client = new \Airbrake\Client($config);
     }
 }
Exemplo n.º 11
0
 /**
  * @param int $major
  * @param int $minor
  * @return bool
  */
 public static function maximum($major, $minor)
 {
     $version = Configuration::get('phpframework', 'version', '0.0');
     $versionParts = explode('.', $version, 2);
     $actualMajor = (int) $versionParts[0];
     $actualMinor = (int) $versionParts[1];
     if ($actualMajor < $major || $actualMajor === $major && $actualMinor <= $minor) {
         return true;
     } else {
         return false;
     }
 }
Exemplo n.º 12
0
 /**
  * @throws DatabaseMigratorException
  */
 public function __construct()
 {
     $this->db = StaticDatabaseConnection::getInstance();
     $this->migrationDirectory = Configuration::get('phpframework', 'db.migrator.directory');
     if (!$this->migrationDirectory) {
         throw new DatabaseMigratorException('Migration directory was not configured.', 1415085595);
     }
     if (!is_dir($this->migrationDirectory)) {
         throw new DatabaseMigratorException('Migration directory "' . $this->migrationDirectory . '" does not exist or is not a directory.', 1415085126);
     }
     $this->lastExecutedVersion = $this->getLastExecutedVersion();
 }
 public function setUp()
 {
     $database = 'phpunit_tests';
     $host = '127.0.0.1';
     $password = '';
     $user = '******';
     Configuration::set('phpframework', 'db.mysql.database', $database);
     Configuration::set('phpframework', 'db.mysql.host', $host);
     Configuration::set('phpframework', 'db.mysql.password', $password);
     Configuration::set('phpframework', 'db.mysql.user', $user);
     $this->repository = ItemRepository::getInstance();
 }
Exemplo n.º 14
0
 /**
  * @return \Twig_Environment
  * @throws ApplicationPartMissingException
  */
 protected function getRenderingEngine()
 {
     if (!isset($this->renderingEngine)) {
         if (!is_dir(Configuration::get('application', 'templates_directory'))) {
             throw new ApplicationPartMissingException('Template directory "' . Configuration::get('application', 'templates_directory') . '" does not exist.');
         }
         $loader = new \Twig_Loader_Filesystem(Configuration::get('application', 'templates_directory'));
         $options = [];
         if (Configuration::get('phpframework', 'cache.enable')) {
             $options['cache'] = Configuration::get('phpframework', 'cache.twig_folder', './cache/twig/');
         }
         $this->renderingEngine = new \Twig_Environment($loader, $options);
     }
     return $this->renderingEngine;
 }
Exemplo n.º 15
0
 /**
  *
  */
 public function __construct()
 {
     $sessionClass = Configuration::get('phpframework', 'authentication.sessionclass', 'BasePHPSession');
     if (!class_exists($sessionClass)) {
         $sessionClass = $this->defaultSessionClassNamespace . '\\' . $sessionClass;
     }
     if (class_exists($sessionClass)) {
         $this->session = new $sessionClass();
         if (!$this->session instanceof BaseSessionInterface) {
             unset($this->session);
             throw new BaseSessionException($sessionClass . ' is not an instance of AppZap\\PHPFramework\\Authentication\\BaseSessionInterface', 1409732473);
         }
     } else {
         throw new BaseSessionException('Session class ' . $sessionClass . ' not found', 1409732479);
     }
 }
Exemplo n.º 16
0
 /**
  * @param $application
  * @throws ApplicationPartMissingException
  */
 public static function initialize($application)
 {
     $projectRoot = self::getProjectRoot();
     $applicationDirectoryPath = $projectRoot . $application;
     $applicationDirectory = realpath($applicationDirectoryPath);
     if (!is_dir($applicationDirectory)) {
         throw new ApplicationPartMissingException('Application folder ' . htmlspecialchars($applicationDirectoryPath) . ' not found', 1410538265);
     }
     $applicationDirectory .= '/';
     Configuration::set('phpframework', 'db.migrator.directory', $applicationDirectory . '_sql/');
     Configuration::set('phpframework', 'project_root', $projectRoot);
     Configuration::set('application', 'application', $application);
     Configuration::set('application', 'application_directory', $applicationDirectory);
     Configuration::set('application', 'routes_file', $applicationDirectory . 'routes.php');
     Configuration::set('application', 'templates_directory', $applicationDirectory . 'templates/');
     Configuration::set('phpframework', 'version', '1.4');
 }
Exemplo n.º 17
0
 /**
  * @return \Swift_Transport
  */
 protected function createTransport()
 {
     $mailConfiguration = Configuration::getSection('phpframework', 'mail', ['smtp_encryption' => 'ssl', 'smtp_host' => 'localhost', 'smtp_password' => '', 'smtp_port' => FALSE, 'smtp_user' => FALSE]);
     if (!$mailConfiguration['smtp_user']) {
         return \Swift_MailTransport::newInstance();
     }
     if ($mailConfiguration['smtp_encryption'] === 'none') {
         $mailConfiguration['smtp_encryption'] = NULL;
     }
     if (!$mailConfiguration['smtp_port']) {
         if ($mailConfiguration['smtp_encryption'] === 'ssl') {
             $mailConfiguration['smtp_port'] = '465';
         } else {
             $mailConfiguration['smtp_port'] = '587';
         }
     }
     $transport = \Swift_SmtpTransport::newInstance($mailConfiguration['smtp_host'], $mailConfiguration['smtp_port'], $mailConfiguration['smtp_encryption']);
     $transport->setUsername($mailConfiguration['smtp_user']);
     $transport->setPassword($mailConfiguration['smtp_password']);
     return $transport;
 }
Exemplo n.º 18
0
 /**
  * @param string $resource
  * @throws ApplicationPartMissingException
  * @throws InvalidHttpResponderException
  */
 public function __construct($resource)
 {
     $routesFile = Configuration::get('application', 'routes_file');
     if (!is_readable($routesFile)) {
         throw new ApplicationPartMissingException('Routes file "' . $routesFile . '" does not exist.', 1415134009);
     }
     $routes = (include $routesFile);
     if (!is_array($routes)) {
         throw new InvalidHttpResponderException('The routes file did not return an array with routes', 1415135585);
     }
     $this->route($routes, $resource);
     // check if the responder is valid
     if (is_string($this->responder)) {
         if (!class_exists($this->responder)) {
             throw new InvalidHttpResponderException('Controller ' . $this->responder . ' for uri "' . $resource . '" not found!', 1415129223);
         }
     } elseif (!isset($this->responder)) {
         throw new InvalidHttpResponderException('Route ' . $resource . ' could not be routed.', 1415136995);
     } elseif (!is_callable($this->responder)) {
         throw new InvalidHttpResponderException('The responder must either be a class string, a callable or an array of subpaths', 1415129333);
     }
 }
Exemplo n.º 19
0
 /**
  * @test
  */
 public function getSectionDefaultValuesNamespace()
 {
     Configuration::reset();
     Configuration::set('test', 'ns1.key1', 1);
     Configuration::set('test', 'ns2.key2', FALSE);
     Configuration::set('test', 'ns1.key3', 3);
     $configuration = Configuration::getSection('test', 'ns1', ['key1' => FALSE, 'key2' => 2]);
     $this->assertSame(1, $configuration['key1']);
     $this->assertSame(2, $configuration['key2']);
     $this->assertSame(3, $configuration['key3']);
 }
Exemplo n.º 20
0
 /**
  * @test
  */
 public function setCharset()
 {
     Configuration::set('phpframework', 'db.charset', 'utf8');
     $this->fixture->connect();
 }
Exemplo n.º 21
0
 public function setUp()
 {
     Configuration::set('application', 'templates_directory', dirname(__FILE__) . '/../_templates');
     Configuration::set('phpframework', 'cache.enable', TRUE);
     $this->reponse = new TwigView();
 }
Exemplo n.º 22
0
 /**
  *
  */
 protected static function invokeDispatcher()
 {
     $dispatcher = new Dispatcher();
     if ($dispatcher->getRequestMethod() === 'cli') {
         $cliArguments = $_SERVER['argv'];
         array_shift($cliArguments);
         $resource = '/' . join('/', $cliArguments);
     } else {
         $resource = $_SERVER['REQUEST_URI'];
         $uriPathPrefix = '/' . trim(Configuration::get('phpframework', 'uri_path_prefix'), '/');
         if ($uriPathPrefix && strpos($resource, $uriPathPrefix) === 0) {
             $resource = substr($resource, strlen($uriPathPrefix));
         }
     }
     return $dispatcher->dispatch($resource);
 }
Exemplo n.º 23
0
 /**
  * @param string $template_name
  * @return \Twig_TemplateInterface
  */
 protected function getTemplateEnvironment($template_name = NULL)
 {
     if (is_null($template_name)) {
         $template_name = $this->templateName;
     }
     $template_file_extension = Configuration::get('phpframework', 'template_file_extension', $this->defaultTemplateFileExtension);
     $template = $this->getRenderingEngine()->loadTemplate($template_name . '.' . $template_file_extension);
     return $template;
 }
Exemplo n.º 24
0
 /**
  * @param string $filename
  */
 protected function loadRoutesFile($filename)
 {
     $routes_file = dirname(__FILE__) . '/_routesfiles/' . $filename . '.php';
     Configuration::set('application', 'routes_file', $routes_file);
 }
 /**
  * @test
  * @expectedException \AppZap\PHPFramework\Authentication\BaseSessionException
  * @expectedExceptionCode 1409732479
  */
 public function constructWithNotExistingSessionClass()
 {
     Configuration::set('phpframework', 'authentication.sessionclass', '\\AppZap\\PHPFramework\\Tests\\Unit\\Authentication\\NotExisting');
     new AuthenticationService();
 }
 public function setUp()
 {
     $_ENV['AppZap\\PHPFramework\\ProjectRoot'] = __DIR__;
     Configuration::reset();
 }
Exemplo n.º 27
0
 /**
  * @test
  * @expectedException \AppZap\PHPFramework\Persistence\DatabaseMigratorException
  * @expectedExceptionCode 1415089456
  */
 public function rollbackOnError()
 {
     Configuration::set('phpframework', 'db.migrator.directory', $this->basePath . '/_migrator/_error/');
     try {
         (new DatabaseMigrator())->migrate();
     } catch (\Exception $e) {
         $this->assertSame(1, count($this->db->query("SHOW TABLES LIKE 'migrator_test_error'")));
         $this->assertSame(1, $this->db->count('migrator_test_error', ['title' => 'test1']));
         $this->assertSame(1, $this->db->count('migrator_test_error'));
         throw $e;
     }
 }