Example #1
0
 public function initialize()
 {
     parent::initialize();
     $this->addPageTitle(__(Config::get('view.admin.title_suffix')));
     $this->view->setLayout(Config::get('view.admin.layout'));
     $this->view->isAdmin(true);
 }
Example #2
0
    public static function register(Di $di)
    {
        $environment = isset($_SERVER['PHWOOLCON_ENV']) ? $_SERVER['PHWOOLCON_ENV'] : 'production';
        // @codeCoverageIgnoreStart
        if (is_file($cacheFile = storagePath('cache/config-' . $environment . '.php'))) {
            static::$config = (include $cacheFile);
            Config::get('app.cache_config') or static::clearCache();
            return;
        }
        // @codeCoverageIgnoreEnd
        $defaultFiles = glob($_SERVER['PHWOOLCON_CONFIG_PATH'] . '/*.php');
        $environmentFiles = glob($_SERVER['PHWOOLCON_CONFIG_PATH'] . '/' . $environment . '/*.php');
        $config = new PhalconConfig(static::loadFiles($defaultFiles));
        $environmentSettings = static::loadFiles($environmentFiles);
        $environmentSettings['environment'] = $environment;
        $environmentConfig = new PhalconConfig($environmentSettings);
        $config->merge($environmentConfig);
        $di->remove('config');
        $di->setShared('config', $config);
        static::$config = $config->toArray();
        Config::get('database.default') and static::loadDb($config);
        // @codeCoverageIgnoreStart
        if (Config::get('app.cache_config')) {
            is_dir($cacheDir = dirname($cacheFile)) or mkdir($cacheDir, 0777, true);
            fileSaveArray($cacheFile, static::$config, function ($content) {
                $replacement = <<<'EOF'
$_SERVER['PHWOOLCON_ROOT_PATH'] . '
EOF;
                return str_replace("'{$_SERVER['PHWOOLCON_ROOT_PATH']}", $replacement, $content);
            });
        }
        // @codeCoverageIgnoreEnd
    }
Example #3
0
 public static function register(Di $di)
 {
     static::$di = $di;
     static::$config = Config::get('auth');
     $di->setShared('auth', function () {
         $di = static::$di;
         $config = static::$config;
         $class = $config['adapter'];
         $options = $config['options'];
         strpos($class, '\\') === false and $class = 'Phwoolcon\\Auth\\Adapter\\' . $class;
         if ($di->has($class)) {
             $class = $di->getRaw($class);
         }
         if (!class_exists($class)) {
             throw new Exception('Admin auth adapter class should implement ' . AdapterInterface::class);
         }
         /* @var Security $hasher */
         $hasher = static::$di->getShared('security');
         $hasher->setDefaultHash($options['security']['default_hash']);
         $hasher->setWorkFactor($options['security']['work_factor']);
         $adapter = new $class($options, $hasher, $di);
         if (!$adapter instanceof AdapterInterface) {
             throw new Exception('Auth adapter class should implement ' . AdapterInterface::class);
         }
         return $adapter;
     });
     static::addPhwoolconJsOptions();
 }
Example #4
0
 public static function register(Di $di)
 {
     $app = new Application(Config::get('app.name'), Config::get('app.version'));
     foreach (Config::get('commands') as $name => $class) {
         $app->add(new $class($name, $di));
     }
     return $app;
 }
Example #5
0
 public static function register(Di $di)
 {
     if ($aliases = Config::get('app.class_aliases')) {
         foreach ($aliases as $alias => $class) {
             class_exists($alias, false) or class_alias($class, $alias);
         }
     }
 }
Example #6
0
 public static function register(Di $di)
 {
     static::$di = $di;
     $di->remove('payment');
     static::$instance = null;
     $di->setShared('payment', function () {
         return new static(Config::get('payment'));
     });
 }
Example #7
0
 protected function keyList()
 {
     $keys = [];
     foreach (Config::get() as $key => $value) {
         if (is_array(fnGet($value, '_black_list')) || is_array(fnGet($value, '_white_list'))) {
             $keys[$key] = static::getKeyLabel($key);
         }
     }
     ksort($keys);
     return $keys;
 }
Example #8
0
 public function __construct($options)
 {
     parent::__construct($options);
     $cacheType = Config::get('cache.default');
     if ($cacheType == 'redis' || $cacheType == 'memcached') {
         $realAdapter = Cache::class;
         $this->realOptions = Config::get('counter.drivers.cache.options');
     } else {
         $realAdapter = Rds::class;
         $this->realOptions = Config::get('counter.drivers.rds.options');
     }
     // @codeCoverageIgnoreEnd
     $this->realCounter = new $realAdapter($this->realOptions);
 }
Example #9
0
 public static function register(Di $di)
 {
     static::$di = $di;
     $di->remove('counter');
     static::$adapter = null;
     $di->setShared('counter', function () {
         $default = Config::get('counter.default');
         $config = Config::get('counter.drivers.' . $default);
         $class = $config['adapter'];
         $options = $config['options'];
         strpos($class, '\\') === false and $class = 'Phwoolcon\\Util\\Counter\\' . $class;
         return new $class($options);
     });
 }
Example #10
0
 public function testUrl()
 {
     $baseUrl = Config::get('app.url');
     $uri = 'test/done';
     $expected = rtrim($baseUrl, '/') . '/' . ltrim($uri, '/');
     $this->assertEquals($expected, url($uri), 'Bad url() result');
     $expected = rtrim($baseUrl, '/') . '/' . ltrim($uri, '/') . '?k=v';
     $this->assertEquals($expected, url($uri, ['k' => 'v']), 'Bad url() result on queries');
     Config::set('app.enable_https', true);
     $expected = str_replace('http:', 'https:', rtrim($baseUrl, '/')) . '/' . ltrim($uri, '/') . '?k=v';
     $this->assertEquals($expected, url($uri, ['k' => 'v'], true), 'Bad url() result on https');
     $expected = $uri = 'http://test.com';
     $this->assertEquals($expected, url($uri), 'Bad url() result on external links');
 }
Example #11
0
 public function __construct()
 {
     parent::__construct(false);
     static::$runningUnitTest = Config::runningUnitTest();
     // @codeCoverageIgnoreStart
     if ($this->_sitePathPrefix = Config::get('app.site_path')) {
         $this->_uriSource = self::URI_SOURCE_GET_URL;
         $this->_sitePathLength = strlen($this->_sitePathPrefix);
     }
     // @codeCoverageIgnoreEnd
     $this->removeExtraSlashes(true);
     $routes = is_file($file = $_SERVER['PHWOOLCON_ROOT_PATH'] . '/app/routes.php') ? include $file : [];
     is_array($routes) and $this->addRoutes($routes);
     $this->cookies = static::$di->getShared('cookies');
     $this->response = static::$di->getShared('response');
     $this->response->setStatusCode(200);
 }
Example #12
0
 public static function register(Di $di)
 {
     static::$hostname = gethostname();
     $di->remove('log');
     static::$logger = null;
     $di->setShared('log', function () {
         $filePath = storagePath('logs');
         is_dir($filePath) or mkdir($filePath, 0777, true);
         $filePath .= '/' . Config::get('app.log.file', 'phwoolcon.log');
         $logger = new File($filePath);
         $formatter = $logger->getFormatter();
         if ($formatter instanceof Line) {
             $formatter->setDateFormat('Y-m-d H:i:s');
             $formatter->setFormat('[%date%]{host}[%type%] {request} %message%');
         }
         return $logger;
     });
 }
Example #13
0
 public static function register(Di $di)
 {
     static::$di = $di;
     $di->remove('cache');
     static::$cache = null;
     $di->setShared('cache', function () {
         $frontend = new Data(['lifetime' => static::TTL_ONE_DAY]);
         $default = Config::get('cache.default');
         $config = Config::get('cache.drivers.' . $default);
         $class = $config['adapter'];
         $options = $config['options'];
         strpos($class, '\\') === false and $class = 'Phalcon\\Cache\\Backend\\' . $class;
         isset($options['cacheDir']) and $options['cacheDir'] = storagePath($options['cacheDir']) . '/';
         /* @var Backend $backend */
         $backend = new $class($frontend, $options);
         return $backend;
     });
 }
Example #14
0
 public static function register(Di $di)
 {
     static::$di = $di;
     $di->set('Phalcon\\Http\\Cookie', 'Phwoolcon\\Http\\Cookie');
     static::$cookies = static::$di->getShared('cookies');
     static::$cookies->reset();
     static::$options = $options = Config::get('cookies');
     static::$cookies->useEncryption($encrypt = $options['encrypt']);
     $encrypt and static::$di->getShared('crypt')->setKey($options['encrypt_key'])->setPadding(Crypt::PADDING_ZERO);
     /* @var \Phalcon\Http\Response $response */
     if ($response = $di->getShared('response')) {
         $response->setCookies(static::$cookies);
     }
     Events::attach('view:generatePhwoolconJsOptions', function (Event $event) {
         $options = $event->getData() ?: [];
         $options['cookies'] = ['domain' => static::$options['domain'], 'path' => static::$options['path']];
         $event->setData($options);
         return $options;
     });
 }
Example #15
0
 public function testGetCurrentConfig()
 {
     $key = 'white_listed';
     $currentConfig = $this->configTrait->getCurrentConfig($key);
     // foo should be visible because it is in white list
     $this->assertEquals(fnGet($currentConfig, 'foo'), Config::get($key . '.foo'));
     // hello should be invisible because it is not in white list
     $this->assertNotEquals(fnGet($currentConfig, 'hello'), Config::get($key . '.hello'));
     $key = 'black_listed';
     $currentConfig = $this->configTrait->getCurrentConfig($key);
     // foo should be invisible because it is in black list
     $this->assertNotEquals(fnGet($currentConfig, 'foo'), Config::get($key . '.foo'));
     // hello should be visible because it is not in black list
     $this->assertEquals(fnGet($currentConfig, 'hello'), Config::get($key . '.hello'));
     $e = false;
     $key = 'protected';
     try {
         $this->configTrait->getCurrentConfig($key);
     } catch (Exception $e) {
     }
     $this->assertInstanceOf(ValidationException::class, $e);
 }
Example #16
0
 public static function register(Di $di)
 {
     static::$di = $di;
     $di->remove('modelsMetadata');
     $di->setShared('modelsMetadata', function () {
         return new InCache();
     });
     $di->setShared('dbManager', function () {
         return new static(Config::get('database'));
     });
     $di->setShared('db', function () {
         return static::connection();
     });
 }
Example #17
0
 public static function register(Di $di)
 {
     static::$di = $di;
     ini_set('session.use_cookies', 0);
     ini_set('session.cache_limiter', '');
     $di->remove('session');
     static::$session = null;
     $di->setShared('session', function () {
         $default = Config::get('session.default');
         $config = Config::get('session.drivers.' . $default);
         $class = $config['adapter'];
         $options = $config['options'];
         $options += Config::get('session.options');
         $options['cookies'] += Config::get('cookies');
         session_name($options['cookies']['name']);
         strpos($class, '\\') === false and $class = 'Phwoolcon\\Session\\Adapter\\' . $class;
         $session = new $class($options);
         // @codeCoverageIgnoreStart
         if (!$session instanceof AdapterInterface) {
             throw new SessionException('Session class should implement ' . AdapterInterface::class);
         }
         // @codeCoverageIgnoreEnd
         return $session;
     });
 }
Example #18
0
 public static function register(Di $di)
 {
     static::$di = $di;
     $di->remove('service');
     $di->setShared('service', function () {
         return new static(Config::get('service'));
     });
 }
Example #19
0
 public function __construct(array $options = [])
 {
     $options = array_merge(Config::get('cache.drivers.redis.options'), $options);
     parent::__construct($options);
     $this->_redis = new RedisCache(new FrontendNone(), $options);
 }
Example #20
0
function url($path, $queries = [], $secure = null)
{
    if (isHttpUrl($path)) {
        return $path;
    }
    $path = trim($path, '/');
    if (Config::get('app.enable_https')) {
        Text::startsWith($path, 'admin', false) and $secure = true;
        $secure === null && null !== ($configValue = Config::get('app.secure_routes.' . $path)) and $secure = $configValue;
        // TODO Detection https via proxy
        $secure === null and $secure = Di::getDefault()['request']->getScheme() === 'https';
    } else {
        $secure = false;
    }
    $protocol = $secure ? 'https://' : 'http://';
    $host = fnGet($_SERVER, 'HTTP_HOST') ?: parse_url(Config::get('app.url'), PHP_URL_HOST);
    $base = $_SERVER['SCRIPT_NAME'];
    $base = trim(dirname($base), '/');
    $base and $base .= '/';
    $url = $protocol . $host . '/' . $base;
    $url .= $path;
    if ($queries && is_array($queries)) {
        $queries = http_build_query($queries);
    }
    $queries && is_string($queries) and $url .= '?' . str_replace('?', '', $queries);
    return $url;
}
Example #21
0
use Phwoolcon\Aliases;
use Phwoolcon\Cache;
use Phwoolcon\Config;
use Phwoolcon\Cookies;
use Phwoolcon\Db;
use Phwoolcon\DiFix;
use Phwoolcon\Events;
use Phwoolcon\I18n;
use Phwoolcon\Log;
use Phwoolcon\Queue;
use Phwoolcon\Router;
use Phwoolcon\Session;
use Phwoolcon\Util\Counter;
use Phwoolcon\View;
$_SERVER['PHWOOLCON_PHALCON_VERSION'] = Version::getId();
Events::register($di);
DiFix::register($di);
Db::register($di);
Cache::register($di);
Log::register($di);
Config::register($di);
Counter::register($di);
Aliases::register($di);
Router::register($di);
I18n::register($di);
Cookies::register($di);
Session::register($di);
View::register($di);
Queue::register($di);
$loader->registerNamespaces(Config::get('app.autoload.namespaces', []), true);
Example #22
0
 public function testAvailableLocales()
 {
     $this->assertEquals(Config::get('i18n.available_locales'), I18n::getAvailableLocales(), 'Bad locales list');
 }
Example #23
0
 public static function register(Di $di)
 {
     static::$di = $di;
     static::$runningUnitTest = Config::runningUnitTest();
     $di->setShared('view', function () {
         return new static(Config::get('view'));
     });
 }
Example #24
0
 public function __construct(array $options = [])
 {
     $options = array_merge(Config::get('cache.drivers.memcached.options'), $options);
     parent::__construct($options);
 }
Example #25
0
 public function initialize()
 {
     $this->pageTitles = [__(Config::get('view.title_suffix'))];
     isset($this->view) and $this->view->reset();
 }
Example #26
0
 public static function register(Di $di)
 {
     static::$di = $di;
     $di->setShared('i18n', function () {
         return new static(Config::get('i18n'));
     });
     Events::attach('view:generatePhwoolconJsOptions', function (Event $event) {
         $options = $event->getData() ?: [];
         $options['locale'] = static::getCurrentLocale();
         $event->setData($options);
         return $options;
     });
 }