Example of usage use Bluz\Proxy\Config; if (!Config::getData('db')) { throw new Exception('Configuration for db is missed'); }
See also: Instance::getData()
See also: Instance::getModuleData()
Author: Anton Shevchuk
Inheritance: use trait ProxyTrait
Esempio n. 1
0
 /**
  * SetUp
  */
 protected function setUp()
 {
     $this->path1 = Proxy\Config::getData('temp', 'image1');
     $this->path2 = Proxy\Config::getData('temp', 'image2');
     $_SERVER['REQUEST_METHOD'] = 'POST';
     $_SERVER['CONTENT_LENGTH'] = 0;
 }
Esempio n. 2
0
 /**
  * get CLI Response
  *
  * @return Cli\Response
  */
 public function initResponse()
 {
     $response = new Cli\Response();
     if ($config = Config::getData('response')) {
         $response->setOptions($config);
     }
     Response::setInstance($response);
 }
Esempio n. 3
0
 /**
  * Init instance
  *
  * @return Instance|Nil
  */
 protected static function initInstance()
 {
     if (Config::getData('logger')) {
         return new Instance();
     } else {
         return new Nil();
     }
 }
Esempio n. 4
0
 /**
  * Init instance
  *
  * @return Instance
  */
 protected static function initInstance()
 {
     $instance = new Instance();
     if ($data = Config::getData('registry')) {
         $instance->setFromArray($data);
     }
     return $instance;
 }
Esempio n. 5
0
 /**
  * Init instance
  *
  * @return Instance
  */
 protected static function initInstance()
 {
     $config = Config::getData('cache');
     if (!$config || !isset($config['enabled']) || !$config['enabled']) {
         return new Nil();
     } else {
         $instance = new Instance();
         $instance->setOptions($config);
         return $instance;
     }
 }
Esempio n. 6
0
 /**
  * Process
  *
  * @param  array $settings
  * @return \Bluz\Grid\Data
  */
 public function process(array $settings = [])
 {
     // process filters
     $where = [];
     if (!empty($settings['filters'])) {
         foreach ($settings['filters'] as $column => $filters) {
             foreach ($filters as $filter => $value) {
                 if ($filter == Grid\Grid::FILTER_LIKE) {
                     $value = '%' . $value . '%';
                 }
                 $where[] = $column . ' ' . $this->filters[$filter] . ' ' . Proxy\Db::quote($value);
             }
         }
     }
     // process orders
     $orders = [];
     if (!empty($settings['orders'])) {
         // Obtain a list of columns
         foreach ($settings['orders'] as $column => $order) {
             $column = Proxy\Db::quoteIdentifier($column);
             $orders[] = $column . ' ' . $order;
         }
     }
     // process pages
     $limit = ' LIMIT ' . ($settings['page'] - 1) * $settings['limit'] . ', ' . $settings['limit'];
     // prepare query
     $connect = Proxy\Config::getData('db', 'connect');
     if (strtolower($connect['type']) == 'mysql') {
         // MySQL
         $dataSql = preg_replace('/SELECT\\s(.*?)\\sFROM/is', 'SELECT SQL_CALC_FOUND_ROWS $1 FROM', $this->source, 1);
         $countSql = 'SELECT FOUND_ROWS()';
     } else {
         // other
         $dataSql = $this->source;
         $countSql = preg_replace('/SELECT\\s(.*?)\\sFROM/is', 'SELECT COUNT(*) FROM', $this->source, 1);
         if (sizeof($where)) {
             $countSql .= ' WHERE ' . join(' AND ', $where);
         }
     }
     if (sizeof($where)) {
         $dataSql .= ' WHERE ' . join(' AND ', $where);
     }
     if (sizeof($orders)) {
         $dataSql .= ' ORDER BY ' . join(', ', $orders);
     }
     $dataSql .= $limit;
     // run queries
     $data = Proxy\Db::fetchAll($dataSql);
     $total = Proxy\Db::fetchOne($countSql);
     $gridData = new Grid\Data($data);
     $gridData->setTotal($total);
     return $gridData;
 }
Esempio n. 7
0
 /**
  * Test upload file
  */
 public function testUploadFile()
 {
     // get path from config
     $path = Config::getData('temp', 'path');
     if (empty($path)) {
         throw new Exception('Temporary path is not configured');
     }
     $_FILES = array('file' => array('name' => 'test.jpg', 'size' => filesize($path), 'type' => 'image/jpeg', 'tmp_name' => $path, 'error' => 0));
     Request::setFileUpload(new TestFileUpload());
     $this->dispatchUri('media/crud', ['title' => 'test', 'file' => $_FILES['file']], 'POST');
     $this->assertQueryCount('input[name="title"]', 1);
     $this->assertOk();
 }
Esempio n. 8
0
 /**
  * Process
  *
  * @param  array $settings
  * @return \Bluz\Grid\Data
  */
 public function process(array $settings = [])
 {
     // process filters
     if (!empty($settings['filters'])) {
         foreach ($settings['filters'] as $column => $filters) {
             foreach ($filters as $filter => $value) {
                 if ($filter == Grid\Grid::FILTER_LIKE) {
                     $value = '%' . $value . '%';
                 }
                 $this->source->andWhere($column . ' ' . $this->filters[$filter] . ' ?', $value);
             }
         }
     }
     // process orders
     if (!empty($settings['orders'])) {
         // Obtain a list of columns
         foreach ($settings['orders'] as $column => $order) {
             $this->source->addOrderBy($column, $order);
         }
     }
     // process pages
     $this->source->setLimit($settings['limit']);
     $this->source->setPage($settings['page']);
     // prepare query
     $connect = Proxy\Config::getData('db', 'connect');
     if (strtolower($connect['type']) == 'mysql') {
         // MySQL
         $select = $this->source->getQueryPart('select');
         $this->source->select('SQL_CALC_FOUND_ROWS ' . current($select));
         // run queries
         $data = $this->source->execute();
         $total = Proxy\Db::fetchOne('SELECT FOUND_ROWS()');
     } else {
         // other
         $totalSource = clone $this->source;
         $totalSource->select('COUNT(*)');
         // run queries
         $data = $this->source->execute();
         $total = $totalSource->execute();
     }
     $gridData = new Grid\Data($data);
     $gridData->setTotal($total);
     return $gridData;
 }
Esempio n. 9
0
 /**
  * prepareRequest
  *
  * @param string $uri in format "module/controllers"
  * @param array $params of request
  * @param string $method HTTP
  * @param bool $ajax
  * @return Http\Request
  */
 private function prepareRequest($uri, array $params = null, $method = Http\Request::METHOD_GET, $ajax = false)
 {
     Request::setRequestUri($uri);
     Request::setOptions(Config::getData('request'));
     Request::setMethod($method);
     // process $_GET params
     if ($query = stristr($uri, '?')) {
         $query = substr($query, 1);
         // remove `?` sign
         parse_str($query, $_GET);
         // fill $_GET
     }
     // process custom params
     if ($params) {
         Request::setParams($params);
     }
     if ($ajax) {
         $_SERVER['HTTP_ACCEPT'] = 'application/json';
         $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
     } else {
         $_SERVER['HTTP_ACCEPT'] = 'text/html';
     }
 }
Esempio n. 10
0
 public function createOne()
 {
     //get saved data
     $existFilesData = Session::get('files');
     $files = unserialize($existFilesData);
     //get paths to upload directory
     $path = Config::getModuleData('menu', 'full_path');
     $relativePath = Config::getModuleData('menu', 'relative_path');
     //get new file,that saved in /tmp directory
     $newFileData = Request::getFileUpload()->getFile('files');
     $editor = new Manager($newFileData, $path);
     //validate file name
     $editor->renameFileName();
     //merge new and exist files data
     if ($existFilesData) {
         $fileObjects = $files;
         $fileObjects[uniqid()] = $editor->getFile();
     } else {
         $fileObjects = [uniqid() => $editor->getFile()];
     }
     Session::set('files', serialize($fileObjects));
     $file = $editor->getFile();
     return array('id' => array_search($editor->getFile(), $fileObjects), 'path' => $relativePath . $file->getName() . '.' . $file->getExtension());
 }
Esempio n. 11
0
 *          required = true,
 *          type="File"
 *      ),
 *      @SWG\ResponseMessage(code=400, message="Invalid file type"),
 *      @SWG\ResponseMessage(code=200, message="File was uploaded")
 *  )
 * )
 */
return function () {
    /**
     * @var Bootstrap $this
     */
    /** @var \Bluz\Http\File $file */
    //if (Request::getFileUpload()->getFile('files')->getType() == 'image') {
    if (Request::getInstance()->isPost()) {
        $upload = new Musician\Upload();
        $path = Config::getModuleData('musician', 'upload_path');
        if (empty($path)) {
            throw new Exception('Upload_path is not configured');
        }
        $upload->setUploadDir($path . "/musician");
        $file = $upload->upload();
        $url = "/uploads/musician/" . $file->getFullName();
        return ['fullName' => $file->getFullName(), 'url' => $url];
    } else {
        return [];
    }
    //  } else {
    //      throw new Exception('Invalid file type');
    //   }
};
Esempio n. 12
0
 /**
  * Init instance
  *
  * @return Instance
  */
 protected static function initInstance()
 {
     $instance = new Instance();
     $instance->setOptions(Config::getData('session'));
     return $instance;
 }
Esempio n. 13
0
 public function readOne($primary)
 {
     $filesArray = unserialize(Session::get('files'));
     $path = Config::getModuleData('menu', 'full_path');
     if ($filesArray) {
         foreach ($filesArray as $file) {
             $filename = $path . $file->getFullName();
             if (is_file($filename)) {
                 unlink($filename);
             }
         }
     }
     Session::delete('files');
     return parent::readOne($primary);
 }
Esempio n. 14
0
 /**
  * Initial Response instance
  * @return void
  */
 protected function initResponse()
 {
     $response = new Http\Response();
     $response->setOptions(Config::getData('response'));
     Response::setInstance($response);
 }
Esempio n. 15
0
 /**
  * SetUp
  */
 protected function setUp()
 {
     $this->path = Proxy\Config::getData('temp', 'image1');
     $file = array('name' => 'test.jpeg', 'size' => filesize($this->path), 'type' => 'image/jpeg', 'tmp_name' => $this->path, 'error' => 0);
     $this->httpFile = new File($file);
 }
Esempio n. 16
0
 // save original name
 $original = $file->getName();
 // rename file to date/time stamp
 $file->setName(date('Ymd_Hi'));
 // switch to JSON response
 $this->useJson();
 if (!$this->user()) {
     throw new Exception('User not found');
 }
 $userId = $this->user()->id;
 // directory structure:
 //   uploads/
 //     %userId%/
 //       %module%/
 //         filename.ext
 $path = Config::getModuleData('media', 'upload_path');
 if (empty($path)) {
     throw new ConfigException('Upload path is not configured');
 }
 $file->moveTo($path . '/' . $userId . '/media');
 // save media data
 $media = new Media\Row();
 $media->userId = $userId;
 $media->module = 'media';
 $media->type = $file->getMimeType();
 $media->title = $original;
 $media->file = 'uploads/' . $userId . '/media/' . $file->getFullName();
 $media->preview = $media->file;
 $media->save();
 // displaying file
 return array('filelink' => $media->file);
Esempio n. 17
0
 /**
  * @return array
  */
 public function getAvailableProviders()
 {
     return array_keys(Config::getData('hybridauth')['providers']);
 }
Esempio n. 18
0
 /**
  * setUp
  */
 public function setUp()
 {
     $this->db = new Db\Db();
     $this->db->setOptions(Proxy\Config::getData('db'));
 }
Esempio n. 19
0
 /**
  * Generates cookie for authentication
  *
  * @throws \Bluz\Db\Exception\DbException
  */
 public function generateCookie()
 {
     $hash = hash('md5', microtime(true));
     $ttl = Config::getModuleData('users', 'rememberMe');
     $this->delete(['userId' => Auth::getIdentity()->id, 'foreignKey' => Auth::getIdentity()->login, 'provider' => self::PROVIDER_COOKIE, 'tokenType' => self::TYPE_ACCESS]);
     $row = new Row();
     $row->userId = Auth::getIdentity()->id;
     $row->foreignKey = Auth::getIdentity()->login;
     $row->provider = self::PROVIDER_COOKIE;
     $row->tokenType = self::TYPE_ACCESS;
     $row->expired = gmdate('Y-m-d H:i:s', time() + $ttl);
     $row->tokenSecret = $this->generateSecret(Auth::getIdentity()->id);
     $row->token = hash('md5', $row->tokenSecret . $hash);
     $row->save();
     Response::setCookie('rToken', $hash, time() + $ttl, '/');
     Response::setCookie('rId', Auth::getIdentity()->id, time() + $ttl, '/');
 }
Esempio n. 20
0
 /**
  * Test Registry configuration setup
  */
 public function testRegistry()
 {
     $this->assertEquals(["moo" => "baz"], Proxy\Config::getData("registry"));
     $this->assertEquals("baz", Proxy\Config::getData("registry", "moo"));
     $this->assertEquals("baz", Proxy\Registry::get('moo'));
 }
Esempio n. 21
0
 /**
  * Drop photo after the test
  */
 public static function tearDownAfterClass()
 {
     Db::delete('media')->where('userId', [1])->execute();
     $path = Config::getModuleData('media', 'upload_path') . '/1';
     Tools\Cleaner::delete($path);
 }
Esempio n. 22
0
 /**
  * Initial Router instance
  *
  * @return void
  */
 protected function initRouter()
 {
     $router = new \Bluz\Router\Router();
     $router->setOptions(Config::getData('router'));
     Router::setInstance($router);
 }