idx int64 0 60.3k | question stringlengths 101 6.21k | target stringlengths 7 803 |
|---|---|---|
51,000 | public function getServiceById ( $ id ) { if ( $ name = $ this -> getServiceNameById ( $ id ) ) { return $ this -> getService ( $ name ) ; } return null ; } | Get a service instance by its identifier . |
51,001 | public function getServiceNamesByType ( $ types , $ only_active = false ) { $ names = [ ] ; $ map = $ this -> getServiceNameTypeMap ( $ only_active ) ; if ( is_string ( $ types ) ) { $ types = array_map ( 'trim' , explode ( ',' , trim ( $ types , ',' ) ) ) ; } foreach ( $ map as $ name => $ type ) { if ( false !== array_search ( $ type , $ types ) ) { $ names [ ] = $ name ; } } return $ names ; } | Return all of the created service names by type . |
51,002 | public function getServiceNamesByGroup ( $ group , $ only_active = false ) { $ types = $ this -> getServiceTypeNames ( $ group ) ; return $ this -> getServiceNamesByType ( $ types , $ only_active ) ; } | Return all of the created service names . |
51,003 | public function purge ( $ name ) { try { if ( $ service = $ this -> getService ( $ name ) ) { if ( $ service instanceof CacheInterface ) { $ service -> flush ( ) ; } } } catch ( \ Exception $ ex ) { } unset ( $ this -> services [ $ name ] ) ; \ Cache :: forget ( 'service_mgr:' . $ name ) ; \ Cache :: forget ( 'service_mgr:id_name_map_active' ) ; \ Cache :: forget ( 'service_mgr:id_name_map' ) ; \ Cache :: forget ( 'service_mgr:name_type_map_active' ) ; \ Cache :: forget ( 'service_mgr:name_type_map' ) ; } | Disconnect from the given service and remove from local cache . |
51,004 | public function getServiceTypes ( $ group = null ) { ksort ( $ this -> types , SORT_NATURAL ) ; if ( ! empty ( $ group ) ) { if ( ! empty ( $ group ) && ! is_array ( $ group ) ) { $ group = array_map ( 'trim' , explode ( ',' , trim ( $ group , ',' ) ) ) ; } $ group = array_map ( 'strtolower' , ( array ) $ group ) ; $ types = [ ] ; foreach ( $ this -> types as $ type ) { if ( in_array ( strtolower ( $ type -> getGroup ( ) ) , $ group ) ) { $ types [ $ type -> getName ( ) ] = $ type ; } } return $ types ; } return $ this -> types ; } | Return all of the known service types . |
51,005 | public function isAccessException ( $ service , $ component , $ action ) { if ( is_string ( $ action ) ) { $ action = VerbsMask :: toNumeric ( $ action ) ; } if ( $ serviceType = $ this -> getServiceTypeByName ( $ service ) ) { if ( $ typeObj = $ this -> getServiceType ( $ serviceType ) ) { if ( $ typeObj -> isAccessException ( $ action , $ component ) ) { return true ; } } } return false ; } | Check for a service access exception . |
51,006 | protected function makeService ( $ name ) { $ config = $ this -> getConfig ( $ name ) ; if ( isset ( $ this -> extensions [ $ name ] ) ) { return call_user_func ( $ this -> extensions [ $ name ] , $ config , $ name ) ; } $ config = $ this -> getDbConfig ( $ name ) ; $ type = $ config [ 'type' ] ; if ( isset ( $ this -> types [ $ type ] ) ) { return $ this -> types [ $ type ] -> make ( $ name , $ config ) ; } throw new InvalidArgumentException ( "Unsupported service type '$type'." ) ; } | Make the service instance . |
51,007 | public function revert ( ) { switch ( $ this -> serviceGroupType ) { case ServiceTypeGroups :: DATABASE : if ( $ this -> resourceExists ( '_schema/' . $ this -> resource ) ) { $ rs = ServiceManager :: handleRequest ( $ this -> service -> getName ( ) , Verbs :: DELETE , '_schema/' . $ this -> resource ) ; if ( $ rs -> getStatusCode ( ) === HttpStatusCodes :: HTTP_OK ) { return true ; } $ content = $ rs -> getContent ( ) ; Log :: error ( 'Failed to delete table on import failure: ' . ( is_array ( $ content ) ? print_r ( $ content , true ) : $ content ) ) ; throw new InternalServerErrorException ( 'Failed to delete table on import failure. See log for details.' ) ; } break ; default : throw new InternalServerErrorException ( 'An Unexpected error occurred.' ) ; } } | Reverts partial import after failure |
51,008 | protected function setService ( $ serviceName ) { if ( empty ( $ serviceName ) ) { throw new InternalServerErrorException ( 'No target service name provided for CSV import.' ) ; } $ this -> service = ServiceManager :: getService ( $ serviceName ) ; $ this -> serviceGroupType = $ this -> service -> getServiceTypeInfo ( ) -> getGroup ( ) ; } | Sets the target service |
51,009 | protected function getHeader ( ) { ini_set ( 'auto_detect_line_endings' , true ) ; if ( ( $ handle = fopen ( $ this -> file , "r" ) ) !== false ) { $ header = fgetcsv ( $ handle , 0 , ',' ) ; static :: isHeader ( $ header ) ; return $ header ; } else { throw new InternalServerErrorException ( 'Could not open uploaded CSV file.' ) ; } } | Fetches CSV header |
51,010 | protected function createDbTableFromHeader ( $ header ) { if ( empty ( $ this -> resource ) ) { $ this -> resource = 'import_' . time ( ) ; } if ( ! $ this -> resourceExists ( '_schema/' . $ this -> resource ) ) { $ schema = $ this -> createSchemaFromHeader ( $ header ) ; $ this -> createTable ( $ schema ) ; return true ; } else { throw new BadRequestException ( 'Importing CSV data into existing DB table [' . $ this -> resource . '] is not supported.' ) ; } } | Creates DB table based on CSV header row |
51,011 | protected function createSchemaFromHeader ( $ header ) { $ schema = [ 'name' => $ this -> resource , 'label' => ucfirst ( $ this -> resource ) , 'description' => 'Table created from CSV data import' , 'plural' => ucfirst ( str_plural ( $ this -> resource ) ) , 'field' => [ ] ] ; foreach ( $ header as $ h ) { $ schema [ 'field' ] [ ] = [ 'name' => $ h , 'type' => 'string' , 'default' => null , 'required' => false , 'allow_null' => true ] ; } return $ schema ; } | Creates table schema definition based on CSV header |
51,012 | protected function resourceExists ( $ resource ) { $ rs = ServiceManager :: handleRequest ( $ this -> service -> getName ( ) , Verbs :: GET , $ resource ) ; if ( $ rs -> getStatusCode ( ) === HttpStatusCodes :: HTTP_NOT_FOUND ) { return false ; } return true ; } | Checks to see if target resource already exists or not |
51,013 | protected function createTable ( $ schema ) { $ rs = ServiceManager :: handleRequest ( $ this -> service -> getName ( ) , Verbs :: POST , '_schema' , [ ] , [ ] , ResourcesWrapper :: wrapResources ( $ schema ) ) ; if ( in_array ( $ rs -> getStatusCode ( ) , [ HttpStatusCodes :: HTTP_OK , HttpStatusCodes :: HTTP_CREATED ] ) ) { return true ; } $ content = $ rs -> getContent ( ) ; Log :: error ( 'Failed to create table for importing CSV data: ' . ( is_array ( $ content ) ? print_r ( $ content , true ) : $ content ) ) ; throw new InternalServerErrorException ( 'Failed to create table for importing CSV data. See log for details.' ) ; } | Creates import table |
51,014 | protected function insertTableData ( & $ data , $ useQueue = true ) { $ job = new DBInsert ( $ this -> service -> getName ( ) , $ this -> resource , $ data ) ; if ( $ useQueue !== true ) { $ job -> onConnection ( 'sync' ) ; } dispatch ( $ job ) ; $ data = [ ] ; } | Inserts data into table |
51,015 | public function getSchemaExtension ( $ name , ConnectionInterface $ conn ) { if ( isset ( $ this -> extensions [ $ name ] ) ) { return call_user_func ( $ this -> extensions [ $ name ] , $ conn ) ; } return null ; } | Return the schema extension object . |
51,016 | public function input ( $ key = null , $ default = null ) { return $ this -> getParameter ( $ key , $ this -> getPayloadData ( $ key , $ default ) ) ; } | Returns request input . |
51,017 | public function setSource ( $ source ) : void { if ( ! \ is_string ( $ source ) ) { throw new Grid \ GridException ( 'Source of `SqlSource` should be string with SQL query' ) ; } parent :: setSource ( $ source ) ; } | Set SQL source |
51,018 | private function applyFilters ( array $ settings ) : array { $ where = [ ] ; foreach ( $ settings as $ column => $ filters ) { foreach ( $ filters as $ filter => $ value ) { if ( $ filter === Grid \ Grid :: FILTER_LIKE ) { $ value = '%' . $ value . '%' ; } $ where [ ] = $ column . ' ' . $ this -> filters [ $ filter ] . ' ' . Proxy \ Db :: quote ( ( string ) $ value ) ; } } return $ where ; } | Apply filters to SQL query |
51,019 | private function applyOrders ( array $ settings ) : array { $ orders = [ ] ; foreach ( $ settings as $ column => $ order ) { $ column = Proxy \ Db :: quoteIdentifier ( $ column ) ; $ orders [ ] = $ column . ' ' . $ order ; } return $ orders ; } | Apply order to SQL query |
51,020 | public function setFoo ( $ arg1 , $ arg2 = 0 ) { if ( is_int ( $ arg1 ) ) { throw new \ Exception ( "First argument should be string" ) ; } if ( $ arg1 == 'good' || $ arg1 == 'fair' ) { $ this -> foo = $ arg1 ; return 1 ; } elseif ( $ arg1 == 'poor' && $ arg2 > 1 ) { $ this -> foo = 'poor' ; return 2 ; } else { return false ; } } | Registers the status of foo s universe |
51,021 | public function save ( ) { $ this -> beforeSave ( ) ; if ( ! \ count ( \ array_filter ( $ this -> getPrimaryKey ( ) ) ) ) { $ result = $ this -> doInsert ( ) ; } elseif ( \ count ( \ array_diff_assoc ( $ this -> getPrimaryKey ( ) , $ this -> clean ) ) ) { $ result = $ this -> doInsert ( ) ; } else { $ result = $ this -> doUpdate ( ) ; } $ this -> afterSave ( ) ; return $ result ; } | Saves the properties to the database . |
51,022 | protected function doInsert ( ) { $ this -> beforeInsert ( ) ; $ data = $ this -> toArray ( ) ; $ this -> assert ( $ data ) ; $ table = $ this -> getTable ( ) ; $ primaryKey = $ table :: insert ( $ data ) ; $ tempPrimaryKey = $ table -> getPrimaryKey ( ) ; $ newPrimaryKey = [ current ( $ tempPrimaryKey ) => $ primaryKey ] ; $ this -> setFromArray ( $ newPrimaryKey ) ; $ this -> afterInsert ( ) ; $ this -> clean = $ this -> toArray ( ) ; return $ newPrimaryKey ; } | Insert row to Db |
51,023 | public function delete ( ) : bool { $ this -> beforeDelete ( ) ; $ primaryKey = $ this -> getPrimaryKey ( ) ; $ table = $ this -> getTable ( ) ; $ result = $ table :: delete ( $ primaryKey ) ; $ this -> afterDelete ( ) ; $ this -> resetArray ( ) ; return $ result > 0 ; } | Delete existing row |
51,024 | protected function getPrimaryKey ( ) : array { $ primary = array_flip ( $ this -> getTable ( ) -> getPrimaryKey ( ) ) ; return array_intersect_key ( $ this -> toArray ( ) , $ primary ) ; } | Retrieves an associative array of primary keys if it exists |
51,025 | public function process ( ) : void { $ closure = include $ this -> file ; if ( ! \ is_callable ( $ closure ) ) { throw new ComponentException ( "There is no callable structure in file `{$this->file}`" ) ; } $ reflection = new \ ReflectionFunction ( $ closure ) ; $ docComment = $ reflection -> getDocComment ( ) ; if ( preg_match_all ( '/\s*\*\s*\@([a-z0-9-_]+)\s+(.*).*\s+/i' , $ docComment , $ matches ) ) { foreach ( $ matches [ 1 ] as $ i => $ key ) { $ this -> setOption ( $ key , trim ( $ matches [ 2 ] [ $ i ] ) ) ; } } $ this -> initRoute ( ) ; $ reflectionParams = $ reflection -> getParameters ( ) ; foreach ( $ reflectionParams as $ param ) { $ name = $ param -> getName ( ) ; if ( ! isset ( $ this -> params [ $ name ] ) ) { $ this -> params [ $ name ] = null ; } if ( $ param -> isOptional ( ) ) { $ this -> values [ $ name ] = $ param -> getDefaultValue ( ) ; } } } | Process to get reflection from file |
51,026 | public function params ( $ requestParams ) : array { $ params = [ ] ; foreach ( $ this -> params as $ param => $ type ) { if ( isset ( $ requestParams [ $ param ] ) ) { switch ( $ type ) { case 'bool' : case 'boolean' : $ params [ ] = ( bool ) $ requestParams [ $ param ] ; break ; case 'int' : case 'integer' : $ params [ ] = ( int ) $ requestParams [ $ param ] ; break ; case 'float' : $ params [ ] = ( float ) $ requestParams [ $ param ] ; break ; case 'string' : $ params [ ] = ( string ) $ requestParams [ $ param ] ; break ; case 'array' : $ params [ ] = ( array ) $ requestParams [ $ param ] ; break ; default : $ params [ ] = $ requestParams [ $ param ] ; break ; } } elseif ( isset ( $ this -> values [ $ param ] ) ) { $ params [ ] = $ this -> values [ $ param ] ; } else { $ params [ ] = null ; } } return $ params ; } | Process request params |
51,027 | public function setAccept ( $ accept ) : void { $ acceptMap = [ 'ANY' => Request :: TYPE_ANY , 'HTML' => Request :: TYPE_HTML , 'JSON' => Request :: TYPE_JSON ] ; $ accept = strtoupper ( $ accept ) ; if ( isset ( $ acceptMap [ $ accept ] ) ) { $ this -> accept [ ] = $ acceptMap [ $ accept ] ; } } | Set accepted types |
51,028 | public function setParam ( $ param ) : void { if ( strpos ( $ param , '$' ) === false ) { return ; } [ $ type , $ key ] = preg_split ( '/[ $]+/' , $ param ) ; $ this -> params [ $ key ] = trim ( $ type ) ; } | Set param types |
51,029 | protected function prepareRoutePattern ( $ route ) : string { $ pattern = str_replace ( '/' , '\/' , $ route ) ; foreach ( $ this -> getParams ( ) as $ param => $ type ) { switch ( $ type ) { case 'int' : case 'integer' : $ pattern = str_replace ( "{\$$param}" , "(?P<$param>[0-9]+)" , $ pattern ) ; break ; case 'float' : $ pattern = str_replace ( "{\$$param}" , "(?P<$param>[0-9.,]+)" , $ pattern ) ; break ; case 'string' : case 'module' : case 'controller' : $ pattern = str_replace ( "{\$$param}" , "(?P<$param>[a-zA-Z0-9-_.]+)" , $ pattern ) ; break ; } } return '/^' . $ pattern . '/i' ; } | Prepare Route pattern |
51,030 | public static function getHeader ( $ header , $ default = null ) { $ header = strtolower ( $ header ) ; $ headers = self :: getInstance ( ) -> getHeaders ( ) ; $ headers = array_change_key_case ( $ headers , CASE_LOWER ) ; if ( array_key_exists ( $ header , $ headers ) ) { $ value = \ is_array ( $ headers [ $ header ] ) ? implode ( ', ' , $ headers [ $ header ] ) : $ headers [ $ header ] ; return $ value ; } return $ default ; } | Search for a header value |
51,031 | public static function getParams ( ) : array { $ body = ( array ) self :: getInstance ( ) -> getParsedBody ( ) ; $ query = ( array ) self :: getInstance ( ) -> getQueryParams ( ) ; return array_merge ( [ ] , $ body , $ query ) ; } | Get all params from GET and POST or PUT |
51,032 | public static function getClientIp ( $ checkProxy = true ) { $ result = null ; if ( $ checkProxy ) { $ result = self :: getServer ( 'HTTP_CLIENT_IP' ) ?? self :: getServer ( 'HTTP_X_FORWARDED_FOR' ) ?? null ; } return $ result ?? self :: getServer ( 'REMOTE_ADDR' ) ; } | Get the client s IP address |
51,033 | public static function checkAccept ( array $ allowTypes = [ ] ) { $ accept = self :: getAccept ( ) ; if ( empty ( $ allowTypes ) ) { return current ( array_keys ( $ accept ) ) ; } $ allowTypes = array_map ( 'strtolower' , $ allowTypes ) ; foreach ( $ accept as $ mime => $ quality ) { if ( $ quality && \ in_array ( $ mime , $ allowTypes , true ) ) { return $ mime ; } } return false ; } | Check Accept header |
51,034 | public function addHelperPath ( string $ path ) : void { $ class = static :: class ; $ realPath = realpath ( $ path ) ; if ( false === $ realPath ) { throw new CommonException ( "Invalid Helper path `$path` for class `$class`" ) ; } if ( ! isset ( static :: $ helpersPath [ $ class ] ) ) { static :: $ helpersPath [ $ class ] = [ ] ; } if ( ! \ in_array ( $ realPath , static :: $ helpersPath [ $ class ] , true ) ) { static :: $ helpersPath [ $ class ] [ ] = $ realPath ; } } | Add helper path |
51,035 | private function addHelper ( string $ name , string $ path ) : void { $ class = static :: class ; if ( ! isset ( static :: $ helpers [ $ class ] ) ) { static :: $ helpers [ $ class ] = [ ] ; } $ helper = include $ path ; if ( \ is_callable ( $ helper ) ) { static :: $ helpers [ $ class ] [ $ name ] = $ helper ; } else { throw new CommonException ( "Helper `$name` not found in file `$path`" ) ; } } | Add helper callable |
51,036 | public function getIdentity ( ) : ? IdentityInterface { if ( ! $ this -> identity ) { if ( Session :: get ( 'auth:agent' ) === Request :: getServer ( 'HTTP_USER_AGENT' ) ) { $ this -> identity = Session :: get ( 'auth:identity' ) ; } else { $ this -> clearIdentity ( ) ; } } return $ this -> identity ; } | Return identity if user agent is correct |
51,037 | public function getParams ( array $ rewrite = [ ] ) : array { $ params = $ this -> params ; $ page = $ rewrite [ 'page' ] ?? 1 ; if ( $ page > 1 ) { $ params [ $ this -> prefix . 'page' ] = $ page ; } $ limit = $ rewrite [ 'limit' ] ?? $ this -> getLimit ( ) ; if ( $ limit !== $ this -> defaultLimit ) { $ params [ $ this -> prefix . 'limit' ] = $ limit ; } $ orders = $ rewrite [ 'orders' ] ?? $ this -> getOrders ( ) ; foreach ( $ orders as $ column => $ order ) { $ column = $ this -> applyAlias ( $ column ) ; $ params [ $ this -> prefix . 'order-' . $ column ] = $ order ; } $ filters = $ rewrite [ 'filters' ] ?? $ this -> getFilters ( ) ; foreach ( $ filters as $ column => $ columnFilters ) { $ column = $ this -> applyAlias ( $ column ) ; if ( \ count ( $ columnFilters ) === 1 && isset ( $ columnFilters [ self :: FILTER_EQ ] ) ) { $ params [ $ this -> prefix . 'filter-' . $ column ] = $ columnFilters [ self :: FILTER_EQ ] ; continue ; } $ columnFilter = [ ] ; foreach ( $ columnFilters as $ filterName => $ filterValue ) { $ columnFilter [ ] = $ filterName . '-' . $ filterValue ; } $ params [ $ this -> prefix . 'filter-' . $ column ] = implode ( '_' , $ columnFilter ) ; } return $ params ; } | Return params prepared for url builder |
51,038 | public function setAllowOrders ( array $ orders = [ ] ) : void { $ this -> allowOrders = [ ] ; foreach ( $ orders as $ column ) { $ this -> addAllowOrder ( $ column ) ; } } | Set allow orders |
51,039 | public function addOrder ( $ column , $ order = self :: ORDER_ASC ) : void { if ( ! $ this -> checkOrderColumn ( $ column ) ) { throw new GridException ( "Order for column `$column` is not allowed" ) ; } if ( ! $ this -> checkOrderName ( $ order ) ) { throw new GridException ( "Order name for column `$column` is incorrect" ) ; } $ this -> orders [ $ column ] = $ order ; } | Add order rule |
51,040 | public function addOrders ( array $ orders ) : void { foreach ( $ orders as $ column => $ order ) { $ this -> addOrder ( $ column , $ order ) ; } } | Add order rules |
51,041 | public function setAllowFilters ( array $ filters = [ ] ) : void { $ this -> allowFilters = [ ] ; foreach ( $ filters as $ column ) { $ this -> addAllowFilter ( $ column ) ; } } | Set allowed filters |
51,042 | protected function checkFilterColumn ( $ column ) : bool { return array_key_exists ( $ column , $ this -> getAllowFilters ( ) ) || \ in_array ( $ column , $ this -> getAllowFilters ( ) , false ) ; } | Check filter column |
51,043 | public function setDefaultLimit ( int $ limit ) : void { if ( $ limit < 1 ) { throw new GridException ( 'Wrong default limit value, should be greater than zero' ) ; } $ this -> setLimit ( $ limit ) ; $ this -> defaultLimit = $ limit ; } | Set default limit |
51,044 | public function setDefaultOrder ( $ column , $ order = self :: ORDER_ASC ) : void { $ this -> defaultOrder = [ $ column => $ order ] ; } | Set default order |
51,045 | public function setName ( $ name ) : void { if ( $ this -> sessionExists ( ) ) { throw new SessionException ( 'Cannot set session name after a session has already started' ) ; } if ( ! preg_match ( '/^[a-zA-Z0-9]+$/' , $ name ) ) { throw new SessionException ( 'Name provided contains invalid characters; must be alphanumeric only' ) ; } $ this -> name = $ name ; session_name ( $ name ) ; } | Attempt to set the session name |
51,046 | public function getName ( ) : string { if ( null === $ this -> name ) { $ this -> name = session_name ( ) ; } return $ this -> name ; } | Get session name |
51,047 | public function expireSessionCookie ( ) : void { if ( ini_get ( 'session.use_cookies' ) ) { $ params = session_get_cookie_params ( ) ; setcookie ( $ this -> getName ( ) , '' , $ _SERVER [ 'REQUEST_TIME' ] - 42000 , $ params [ 'path' ] , $ params [ 'domain' ] , $ params [ 'secure' ] , $ params [ 'httponly' ] ) ; } } | Expire the session cookie |
51,048 | protected function setSavePath ( $ savePath ) : void { if ( ! is_dir ( $ savePath ) || ! is_writable ( $ savePath ) ) { throw new ComponentException ( 'Session path is not writable' ) ; } session_save_path ( $ savePath ) ; } | Set session save path |
51,049 | public function readOne ( $ primary ) { if ( ! $ primary ) { return $ this -> getTable ( ) :: create ( ) ; } $ row = $ this -> getTable ( ) :: findRow ( $ primary ) ; if ( ! $ row ) { throw new NotFoundException ( 'Record not found' ) ; } $ row = $ this -> filterRow ( $ row ) ; return $ row ; } | Get record from Db or create new object |
51,050 | public function readSet ( $ offset = 0 , $ limit = 10 , $ params = [ ] ) { $ select = $ this -> getTable ( ) :: select ( ) ; if ( \ count ( $ this -> getFields ( ) ) ) { $ fields = $ this -> getFields ( ) ; $ name = $ this -> getTable ( ) -> getName ( ) ; $ fields = array_map ( function ( $ field ) use ( $ name ) { return $ name . '.' . $ field ; } , $ fields ) ; $ select -> select ( implode ( ', ' , $ fields ) ) ; } $ type = Proxy \ Db :: getOption ( 'connect' , 'type' ) ; switch ( $ type ) { case 'mysql' : $ selectPart = $ select -> getSelect ( ) ; $ selectPart [ 0 ] = 'SQL_CALC_FOUND_ROWS ' . $ selectPart [ 0 ] ; $ select -> select ( ... $ selectPart ) ; $ totalSQL = 'SELECT FOUND_ROWS()' ; break ; case 'pgsql' : default : $ selectTotal = clone $ select ; $ selectTotal -> select ( 'COUNT(*)' ) ; $ totalSQL = $ selectTotal -> getSql ( ) ; break ; } $ select -> setLimit ( $ limit ) ; $ select -> setOffset ( $ offset ) ; $ result = [ ] ; $ total = 0 ; Proxy \ Db :: transaction ( function ( ) use ( & $ result , & $ total , $ select , $ totalSQL ) { $ result = $ select -> execute ( ) ; $ total = Proxy \ Db :: fetchOne ( $ totalSQL ) ; } ) ; return [ $ result , $ total ] ; } | Get set of records |
51,051 | public function run ( ) : Data { if ( ! $ this -> loadData ( ) ) { $ this -> process ( ) ; $ this -> saveData ( ) ; } return $ this -> data ; } | Run controller logic |
51,052 | protected function findFile ( ) : void { $ path = Application :: getInstance ( ) -> getPath ( ) ; $ file = "$path/modules/{$this->module}/controllers/{$this->controller}.php" ; if ( ! file_exists ( $ file ) ) { throw new ControllerException ( "Controller file not found '{$this->module}/{$this->controller}'" , 404 ) ; } $ this -> file = $ file ; } | Setup controller file |
51,053 | protected function initMeta ( ) : void { $ cacheKey = "meta.{$this->module}.{$this->controller}" ; if ( ! $ meta = Cache :: get ( $ cacheKey ) ) { $ meta = new Meta ( $ this -> getFile ( ) ) ; $ meta -> process ( ) ; Cache :: set ( $ cacheKey , $ meta , Cache :: TTL_NO_EXPIRY , [ 'system' , 'meta' ] ) ; } $ this -> meta = $ meta ; } | Retrieve reflection for anonymous function |
51,054 | private function loadData ( ) : bool { $ cacheTime = $ this -> getMeta ( ) -> getCache ( ) ; if ( $ cacheTime && $ cached = Cache :: get ( $ this -> key ) ) { $ this -> data = $ cached ; return true ; } return false ; } | Load Data from cache |
51,055 | private function saveData ( ) : bool { if ( $ cacheTime = $ this -> getMeta ( ) -> getCache ( ) ) { return Cache :: set ( $ this -> key , $ this -> getData ( ) , $ cacheTime , [ 'system' , 'data' ] ) ; } return false ; } | Save Data to cache |
51,056 | public function addMap ( $ method , $ module , $ controller ) : Link { return $ this -> map [ strtoupper ( $ method ) ] = new Link ( $ module , $ controller ) ; } | Add mapping data |
51,057 | public function head ( $ module , $ controller ) : Link { return $ this -> addMap ( RequestMethod :: HEAD , $ module , $ controller ) ; } | Add mapping for HEAD method |
51,058 | public function get ( string $ module , string $ controller ) : Link { return $ this -> addMap ( RequestMethod :: GET , $ module , $ controller ) ; } | Add mapping for GET method |
51,059 | public function post ( string $ module , string $ controller ) : Link { return $ this -> addMap ( RequestMethod :: POST , $ module , $ controller ) ; } | Add mapping for POST method |
51,060 | public function patch ( string $ module , string $ controller ) : Link { return $ this -> addMap ( RequestMethod :: PATCH , $ module , $ controller ) ; } | Add mapping for PATCH method |
51,061 | public function put ( string $ module , string $ controller ) : Link { return $ this -> addMap ( RequestMethod :: PUT , $ module , $ controller ) ; } | Add mapping for PUT method |
51,062 | public function delete ( string $ module , string $ controller ) : Link { return $ this -> addMap ( RequestMethod :: DELETE , $ module , $ controller ) ; } | Add mapping for DELETE method |
51,063 | public function options ( string $ module , string $ controller ) : Link { return $ this -> addMap ( RequestMethod :: OPTIONS , $ module , $ controller ) ; } | Add mapping for OPTIONS method |
51,064 | protected function prepareRequest ( ) : void { $ method = Request :: getMethod ( ) ; $ this -> method = strtoupper ( $ method ) ; $ path = Router :: getCleanUri ( ) ; $ this -> params = explode ( '/' , rtrim ( $ path , '/' ) ) ; $ this -> module = array_shift ( $ this -> params ) ; $ this -> controller = array_shift ( $ this -> params ) ; $ data = Request :: getParams ( ) ; unset ( $ data [ '_method' ] , $ data [ '_module' ] , $ data [ '_controller' ] ) ; $ this -> data = array_merge ( $ data , $ this -> data ) ; $ primary = $ this -> crud -> getPrimaryKey ( ) ; $ this -> primary = array_intersect_key ( $ this -> data , array_flip ( $ primary ) ) ; } | Prepare request for processing |
51,065 | public function getRelation ( $ modelName ) : ? RowInterface { $ relations = $ this -> getRelations ( $ modelName ) ; return empty ( $ relations ) ? null : current ( $ relations ) ; } | Get relation by model name |
51,066 | public function getRelations ( $ modelName ) : array { if ( ! isset ( $ this -> relations [ $ modelName ] ) ) { $ this -> relations [ $ modelName ] = Relations :: findRelation ( $ this , $ modelName ) ; } return $ this -> relations [ $ modelName ] ; } | Get relations by model name |
51,067 | public function addParts ( $ parts ) : CompositeBuilder { foreach ( $ parts as $ part ) { $ this -> addPart ( $ part ) ; } return $ this ; } | Adds a set of expressions to composite expression |
51,068 | public function addPart ( $ part ) : CompositeBuilder { if ( ! empty ( $ part ) || ( $ part instanceof self && $ part -> count ( ) > 0 ) ) { $ this -> parts [ ] = $ part ; } return $ this ; } | Adds an expression to composite expression |
51,069 | private function prepareRouterData ( ) { $ routers = [ ] ; $ reverse = [ ] ; $ path = Application :: getInstance ( ) -> getPath ( ) . '/modules/*/controllers/*.php' ; foreach ( new \ GlobIterator ( $ path ) as $ file ) { $ module = $ file -> getPathInfo ( ) -> getPathInfo ( ) -> getBasename ( ) ; $ controller = $ file -> getBasename ( '.php' ) ; $ controllerInstance = new Controller ( $ module , $ controller ) ; $ meta = $ controllerInstance -> getMeta ( ) ; if ( $ routes = $ meta -> getRoute ( ) ) { foreach ( $ routes as $ route => $ pattern ) { if ( ! isset ( $ reverse [ $ module ] ) ) { $ reverse [ $ module ] = [ ] ; } $ reverse [ $ module ] [ $ controller ] = [ 'route' => $ route , 'params' => $ meta -> getParams ( ) ] ; $ rule = [ $ route => [ 'pattern' => $ pattern , 'module' => $ module , 'controller' => $ controller , 'params' => $ meta -> getParams ( ) ] ] ; if ( strpos ( $ route , '$' ) ) { $ routers [ ] = $ rule ; } else { array_unshift ( $ routers , $ rule ) ; } } } } $ routers = array_merge ( ... $ routers ) ; return [ $ routers , $ reverse ] ; } | Initial routers data from controllers |
51,070 | public function getCleanUri ( ) : string { if ( $ this -> cleanUri === null ) { $ uri = Request :: getUri ( ) -> getPath ( ) ; if ( $ this -> getBaseUrl ( ) && strpos ( $ uri , $ this -> getBaseUrl ( ) ) === 0 ) { $ uri = substr ( $ uri , \ strlen ( $ this -> getBaseUrl ( ) ) ) ; } $ this -> cleanUri = $ uri ; } return $ this -> cleanUri ; } | Get the request URI without baseUrl |
51,071 | public function getUrl ( $ module = self :: DEFAULT_MODULE , $ controller = self :: DEFAULT_CONTROLLER , array $ params = [ ] ) : string { $ module = $ module ?? Request :: getModule ( ) ; $ controller = $ controller ?? Request :: getController ( ) ; if ( isset ( $ this -> reverse [ $ module ] , $ this -> reverse [ $ module ] [ $ controller ] ) ) { return $ this -> urlCustom ( $ module , $ controller , $ params ) ; } return $ this -> urlRoute ( $ module , $ controller , $ params ) ; } | Build URL to controller |
51,072 | public function getFullUrl ( $ module = self :: DEFAULT_MODULE , $ controller = self :: DEFAULT_CONTROLLER , array $ params = [ ] ) : string { $ scheme = Request :: getUri ( ) -> getScheme ( ) . '://' ; $ host = Request :: getUri ( ) -> getHost ( ) ; $ port = Request :: getUri ( ) -> getPort ( ) ; if ( $ port && ! \ in_array ( $ port , [ 80 , 443 ] , true ) ) { $ host .= ':' . $ port ; } $ url = $ this -> getUrl ( $ module , $ controller , $ params ) ; return $ scheme . $ host . $ url ; } | Build full URL to controller |
51,073 | protected function urlCustom ( $ module , $ controller , $ params ) : string { $ url = $ this -> reverse [ $ module ] [ $ controller ] [ 'route' ] ; $ getParams = [ ] ; foreach ( $ params as $ key => $ value ) { if ( \ is_array ( $ value ) ) { $ getParams [ $ key ] = $ value ; continue ; } $ url = str_replace ( '{$' . $ key . '}' , $ value , $ url , $ replaced ) ; if ( ! $ replaced ) { $ getParams [ $ key ] = $ value ; } } $ url = preg_replace ( '/\{\$[a-z0-9-_]+\}/i' , '' , $ url ) ; $ url = preg_replace ( '/\(\.\*\)/' , '' , $ url ) ; $ url = str_replace ( '//' , '/' , $ url ) ; if ( ! empty ( $ getParams ) ) { $ url .= '?' . http_build_query ( $ getParams ) ; } return $ this -> getBaseUrl ( ) . ltrim ( $ url , '/' ) ; } | Build URL by custom route |
51,074 | protected function urlRoute ( $ module , $ controller , $ params ) : string { $ url = $ this -> getBaseUrl ( ) ; if ( empty ( $ params ) && $ controller === self :: DEFAULT_CONTROLLER ) { if ( $ module === self :: DEFAULT_MODULE ) { return $ url ; } return $ url . $ module ; } $ url .= $ module . '/' . $ controller ; $ getParams = [ ] ; foreach ( $ params as $ key => $ value ) { if ( \ is_array ( $ value ) ) { $ getParams [ $ key ] = $ value ; continue ; } $ url .= '/' . urlencode ( ( string ) $ key ) . '/' . urlencode ( ( string ) $ value ) ; } if ( ! empty ( $ getParams ) ) { $ url .= '?' . http_build_query ( $ getParams ) ; } return $ url ; } | Build URL by default route |
51,075 | protected function processCustom ( ) : bool { $ uri = '/' . $ this -> getCleanUri ( ) ; foreach ( $ this -> routers as $ router ) { if ( preg_match ( $ router [ 'pattern' ] , $ uri , $ matches ) ) { $ this -> setParam ( '_module' , $ router [ 'module' ] ) ; $ this -> setParam ( '_controller' , $ router [ 'controller' ] ) ; foreach ( $ router [ 'params' ] as $ param => $ type ) { if ( isset ( $ matches [ $ param ] ) ) { $ this -> setParam ( $ param , $ matches [ $ param ] ) ; } } return true ; } } return false ; } | Process custom router |
51,076 | protected function processRoute ( ) : bool { $ uri = $ this -> getCleanUri ( ) ; $ uri = trim ( $ uri , '/' ) ; $ raw = explode ( '/' , $ uri ) ; if ( \ count ( $ raw ) ) { $ this -> setParam ( '_module' , array_shift ( $ raw ) ) ; } if ( \ count ( $ raw ) ) { $ this -> setParam ( '_controller' , array_shift ( $ raw ) ) ; } if ( $ size = \ count ( $ raw ) ) { $ this -> rawParams = $ raw ; foreach ( $ raw as $ i => $ value ) { $ this -> setParam ( $ i , $ value ) ; } if ( $ size % 2 === 1 ) { array_pop ( $ raw ) ; $ size = \ count ( $ raw ) ; } for ( $ i = 0 ; $ i < $ size ; $ i += 2 ) { $ this -> setParam ( $ raw [ $ i ] , $ raw [ $ i + 1 ] ) ; } } return true ; } | Process router by default rules |
51,077 | public function addSelect ( string ... $ select ) : Select { $ this -> select = array_merge ( $ this -> select , $ select ) ; return $ this ; } | Adds an item that is to be returned in the query result . |
51,078 | public function andHaving ( ... $ conditions ) : Select { $ condition = $ this -> prepareCondition ( $ conditions ) ; if ( $ this -> having instanceof CompositeBuilder && $ this -> having -> getType ( ) === 'AND' ) { $ this -> having -> addPart ( $ condition ) ; } else { $ this -> having = new CompositeBuilder ( [ $ this -> having , $ condition ] ) ; } return $ this ; } | Adds a restriction over the groups of the query forming a logical conjunction with any existing having restrictions |
51,079 | public function setPage ( int $ page = 1 ) : Select { if ( ! $ this -> limit ) { throw new DbException ( 'Please setup limit for use method `setPage`' ) ; } $ this -> offset = $ this -> limit * ( $ page - 1 ) ; return $ this ; } | Setup offset like a page number start from 1 |
51,080 | public function join ( string $ fromAlias , string $ join , string $ alias , string $ condition = null ) : self { return $ this -> innerJoin ( $ fromAlias , $ join , $ alias , $ condition ) ; } | Creates and adds a join to the query |
51,081 | protected function prepareFrom ( ) : string { $ fromClauses = [ ] ; foreach ( $ this -> from as $ from ) { $ fromClause = Db :: quoteIdentifier ( $ from [ 'table' ] ) . ' AS ' . Db :: quoteIdentifier ( $ from [ 'alias' ] ) . $ this -> prepareJoins ( $ from [ 'alias' ] ) ; $ fromClauses [ $ from [ 'alias' ] ] = $ fromClause ; } return ' FROM ' . implode ( ', ' , $ fromClauses ) ; } | Prepare From query part |
51,082 | protected function prepareJoins ( $ fromAlias ) : string { if ( ! isset ( $ this -> join [ $ fromAlias ] ) ) { return '' ; } $ query = '' ; foreach ( $ this -> join [ $ fromAlias ] as $ join ) { $ query .= ' ' . strtoupper ( $ join [ 'joinType' ] ) . ' JOIN ' . Db :: quoteIdentifier ( $ join [ 'joinTable' ] ) . ' AS ' . Db :: quoteIdentifier ( $ join [ 'joinAlias' ] ) . ' ON ' . $ join [ 'joinCondition' ] ; $ query .= $ this -> prepareJoins ( $ join [ 'joinAlias' ] ) ; } return $ query ; } | Generate SQL string for JOINs |
51,083 | public function isRequired ( ) : bool { foreach ( $ this -> rules as $ rule ) { if ( $ rule instanceof RequiredRule ) { return true ; } } return false ; } | Get required flag |
51,084 | public function callback ( $ callable , $ description = null ) : ValidatorChain { $ rule = Validator :: rule ( 'callback' , [ $ callable ] ) ; if ( null !== $ description ) { $ rule -> setDescription ( $ description ) ; } $ this -> addRule ( $ rule ) ; return $ this ; } | Add Callback Rule to ValidatorChain |
51,085 | public function regexp ( $ expression , $ description = null ) : ValidatorChain { $ rule = Validator :: rule ( 'regexp' , [ $ expression ] ) ; if ( null !== $ description ) { $ rule -> setDescription ( $ description ) ; } $ this -> addRule ( $ rule ) ; return $ this ; } | Add Regexp Rule to ValidatorChain |
51,086 | public function info ( $ message , array $ context = [ ] ) : void { $ message = $ this -> interpolate ( $ message , $ context ) ; if ( ! $ this -> startTime ) { $ this -> startTime = $ this -> timer = $ _SERVER [ 'REQUEST_TIME_FLOAT' ] ?? microtime ( true ) ; } $ curTimer = microtime ( true ) ; $ curMemory = memory_get_usage ( ) ; $ key = sprintf ( '%f :: %f :: %d' , $ curTimer - $ this -> startTime , $ curTimer - $ this -> timer , $ curMemory - $ this -> memory ) ; $ this -> info [ $ key ] = $ message ; $ this -> timer = $ curTimer ; $ this -> memory = $ curMemory ; } | Log info message |
51,087 | public function getHeader ( $ header ) : string { if ( $ this -> hasHeader ( $ header ) ) { return implode ( ', ' , $ this -> headers [ $ header ] ) ; } return '' ; } | Retrieve a header by the given case - insensitive name as a string |
51,088 | public function addHeader ( $ header , $ value ) : void { if ( $ this -> hasHeader ( $ header ) ) { $ this -> headers [ $ header ] [ ] = $ value ; } else { $ this -> setHeader ( $ header , $ value ) ; } } | Appends a header value for the specified header |
51,089 | public function add ( $ name ) : ValidatorChain { $ this -> validators [ $ name ] = $ this -> validators [ $ name ] ?? Validator :: create ( ) ; return $ this -> validators [ $ name ] ; } | Add chain to form |
51,090 | protected function validateItem ( $ key , $ value ) : bool { $ result = $ this -> validators [ $ key ] -> validate ( $ value ) ; if ( ! $ result ) { $ this -> exception -> setError ( $ key , $ this -> validators [ $ key ] -> getError ( ) ) ; } return $ result ; } | Validate chain of rules for single item |
51,091 | public function setFromArray ( array $ data ) : void { foreach ( $ data as $ key => $ value ) { $ this -> container [ $ key ] = $ value ; } } | Sets all data in the row from an array |
51,092 | public function addTextDomain ( $ domain , $ path ) : void { if ( ! is_dir ( $ path ) ) { throw new ConfigurationException ( "Translator configuration path `$path` not found" ) ; } bindtextdomain ( $ domain , $ path ) ; bind_textdomain_codeset ( $ domain , 'UTF-8' ) ; } | Add text domain for gettext |
51,093 | public static function translatePlural ( string $ singular , string $ plural , $ number , ... $ text ) : string { if ( \ function_exists ( 'ngettext' ) ) { $ message = ngettext ( $ singular , $ plural , $ number ) ; } else { $ message = $ singular ; } if ( \ func_num_args ( ) > 3 ) { array_unshift ( $ text , $ number ) ; $ message = vsprintf ( $ message , $ text ) ; } return $ message ; } | Translate plural form |
51,094 | protected function filter ( $ input ) : string { $ input = parent :: filter ( ( string ) $ input ) ; return preg_replace ( '/\s/' , '' , $ input ) ; } | Filter input data |
51,095 | protected function parseRange ( $ input ) : ? array { if ( $ input === null || $ input === '*' || $ input === '*.*.*.*' || $ input === '0.0.0.0-255.255.255.255' ) { return null ; } $ range = [ 'min' => null , 'max' => null , 'mask' => null ] ; if ( strpos ( $ input , '-' ) !== false ) { [ $ range [ 'min' ] , $ range [ 'max' ] ] = explode ( '-' , $ input ) ; } elseif ( strpos ( $ input , '*' ) !== false ) { $ this -> parseRangeUsingWildcards ( $ input , $ range ) ; } elseif ( strpos ( $ input , '/' ) !== false ) { $ this -> parseRangeUsingCidr ( $ input , $ range ) ; } else { throw new ComponentException ( 'Invalid network range' ) ; } if ( ! $ this -> verifyAddress ( $ range [ 'min' ] ) ) { throw new ComponentException ( 'Invalid network range' ) ; } if ( isset ( $ range [ 'max' ] ) && ! $ this -> verifyAddress ( $ range [ 'max' ] ) ) { throw new ComponentException ( 'Invalid network range' ) ; } return $ range ; } | Parse IP range |
51,096 | protected function parseRangeUsingWildcards ( $ input , & $ range ) : void { $ this -> fillAddress ( $ input ) ; $ range [ 'min' ] = str_replace ( '*' , '0' , $ input ) ; $ range [ 'max' ] = str_replace ( '*' , '255' , $ input ) ; } | Parse range using wildcards |
51,097 | protected function verifyNetwork ( $ input ) : bool { if ( $ this -> networkRange === null ) { return true ; } if ( isset ( $ this -> networkRange [ 'mask' ] ) ) { return $ this -> belongsToSubnet ( $ input ) ; } $ input = sprintf ( '%u' , ip2long ( $ input ) ) ; $ min = sprintf ( '%u' , ip2long ( $ this -> networkRange [ 'min' ] ) ) ; $ max = sprintf ( '%u' , ip2long ( $ this -> networkRange [ 'max' ] ) ) ; return ( $ input >= $ min ) && ( $ input <= $ max ) ; } | Verify Network by mask |
51,098 | protected function initTable ( ) : void { $ tableClass = class_namespace ( static :: class ) . '\\Table' ; if ( ! class_exists ( $ tableClass ) || ! is_subclass_of ( $ tableClass , TableInterface :: class ) ) { throw new TableNotFoundException ( '`Table` class is not exists or not initialized' ) ; } $ this -> setTable ( $ tableClass :: getInstance ( ) ) ; } | Init table instance for manipulation |
51,099 | public function getPath ( ) : string { if ( ! $ this -> path ) { if ( \ defined ( 'PATH_APPLICATION' ) ) { $ this -> path = PATH_APPLICATION ; } else { $ reflection = new \ ReflectionClass ( $ this ) ; $ this -> path = \ dirname ( $ reflection -> getFileName ( ) , 3 ) ; } } return $ this -> path ; } | Get path to Application |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.