idx int64 0 60.3k | question stringlengths 64 4.24k | target stringlengths 5 618 |
|---|---|---|
44,900 | public function quoteColumnName ( $ name ) { if ( strpos ( $ name , '(' ) !== false || strpos ( $ name , '[[' ) !== false || strpos ( $ name , '{{' ) !== false ) { return $ name ; } if ( ( $ pos = strrpos ( $ name , '.' ) ) !== false ) { $ prefix = $ this -> quoteIndexName ( substr ( $ name , 0 , $ pos ) ) . '.' ; $ name = substr ( $ name , $ pos + 1 ) ; } else { $ prefix = '' ; } return $ prefix . $ this -> quoteSimpleColumnName ( $ name ) ; } | Quotes a column name for use in a query . If the column name contains prefix the prefix will also be properly quoted . If the column name is already quoted or contains ( [[ or {{ then this method will do nothing . |
44,901 | protected function getColumnPhpType ( $ column ) { static $ typeMap = [ 'smallint' => 'integer' , 'integer' => 'integer' , 'bigint' => 'integer' , 'timestamp' => 'integer' , 'boolean' => 'boolean' , 'float' => 'double' , ] ; if ( isset ( $ typeMap [ $ column -> type ] ) ) { if ( $ column -> type === 'bigint' ) { return PHP_INT_SIZE == 8 ? 'integer' : 'string' ; } elseif ( $ column -> type === 'integer' || $ column -> type === 'timestamp' ) { return PHP_INT_SIZE == 4 ? 'string' : 'integer' ; } else { return $ typeMap [ $ column -> type ] ; } } else { return 'string' ; } } | Extracts the PHP type from abstract DB type . |
44,902 | protected function findColumns ( $ index ) { $ sql = 'DESCRIBE ' . $ this -> quoteSimpleIndexName ( $ index -> name ) ; try { $ columns = $ this -> db -> createCommand ( $ sql ) -> queryAll ( ) ; } catch ( \ Exception $ e ) { $ previous = $ e -> getPrevious ( ) ; if ( $ previous instanceof \ PDOException && strpos ( $ previous -> getMessage ( ) , 'SQLSTATE[42S02' ) !== false ) { return false ; } throw $ e ; } if ( empty ( $ columns [ 0 ] [ 'Agent' ] ) ) { foreach ( $ columns as $ info ) { $ column = $ this -> loadColumnSchema ( $ info ) ; if ( isset ( $ index -> columns [ $ column -> name ] ) ) { $ column = $ this -> mergeColumnSchema ( $ index -> columns [ $ column -> name ] , $ column ) ; } $ index -> columns [ $ column -> name ] = $ column ; if ( $ column -> isPrimaryKey ) { $ index -> primaryKey = $ column -> name ; } } return true ; } foreach ( $ columns as $ column ) { if ( ! empty ( $ column [ 'Type' ] ) && strcasecmp ( 'local' , $ column [ 'Type' ] ) !== 0 ) { continue ; } $ agent = $ this -> getIndexSchema ( $ column [ 'Agent' ] ) ; if ( $ agent !== null ) { $ index -> columns = $ agent -> columns ; $ index -> primaryKey = $ agent -> primaryKey ; return true ; } } $ this -> applyDefaultColumns ( $ index ) ; return true ; } | Collects the metadata of index columns . |
44,903 | protected function applyDefaultColumns ( $ index ) { $ column = $ this -> loadColumnSchema ( [ 'Field' => 'id' , 'Type' => 'bigint' , ] ) ; $ index -> columns [ $ column -> name ] = $ column ; $ index -> primaryKey = 'id' ; } | Sets up the default columns for given index . This method should be used in case there is no way to find actual columns like in some distributed indexes . |
44,904 | protected function mergeColumnSchema ( $ origin , $ override ) { $ override -> dbType .= ',' . $ override -> dbType ; if ( $ override -> isField ) { $ origin -> isField = true ; } if ( $ override -> isAttribute ) { $ origin -> isAttribute = true ; $ origin -> type = $ override -> type ; $ origin -> phpType = $ override -> phpType ; if ( $ override -> isMva ) { $ origin -> isMva = true ; } } return $ origin ; } | Merges two column schemas into a single one . |
44,905 | public function getFacet ( $ name ) { $ facets = $ this -> getFacets ( ) ; if ( ! isset ( $ facets [ $ name ] ) ) { throw new InvalidCallException ( "Facet '{$name}' does not present." ) ; } return $ facets [ $ name ] ; } | Returns results of the specified facet . |
44,906 | public function match ( $ condition , $ params = [ ] ) { $ this -> match = $ condition ; $ this -> addParams ( $ params ) ; return $ this ; } | Sets the MATCH expression . |
44,907 | public function batchReplace ( $ index , $ columns , $ rows , & $ params ) { return $ this -> generateBatchInsertReplace ( 'REPLACE' , $ index , $ columns , $ rows , $ params ) ; } | Generates a batch REPLACE SQL statement . For example |
44,908 | public function callSnippets ( $ index , $ source , $ match , $ options , & $ params ) { if ( is_array ( $ source ) ) { $ dataSqlParts = [ ] ; foreach ( $ source as $ sourceRow ) { $ phName = self :: PARAM_PREFIX . count ( $ params ) ; $ params [ $ phName ] = ( string ) $ sourceRow ; $ dataSqlParts [ ] = $ phName ; } $ dataSql = '(' . implode ( ',' , $ dataSqlParts ) . ')' ; } else { $ phName = self :: PARAM_PREFIX . count ( $ params ) ; $ params [ $ phName ] = $ source ; $ dataSql = $ phName ; } $ indexParamName = self :: PARAM_PREFIX . count ( $ params ) ; $ params [ $ indexParamName ] = $ index ; $ matchSql = $ this -> buildMatch ( $ match , $ params ) ; if ( ! empty ( $ options ) ) { $ optionParts = [ ] ; foreach ( $ options as $ name => $ value ) { if ( $ value instanceof Expression ) { $ actualValue = $ value -> expression ; } else { $ actualValue = self :: PARAM_PREFIX . count ( $ params ) ; $ params [ $ actualValue ] = $ value ; } $ optionParts [ ] = $ actualValue . ' AS ' . $ name ; } $ optionSql = ', ' . implode ( ', ' , $ optionParts ) ; } else { $ optionSql = '' ; } return 'CALL SNIPPETS(' . $ dataSql . ', ' . $ indexParamName . ', ' . $ matchSql . $ optionSql . ')' ; } | Builds a SQL statement for call snippet from provided data and query using specified index settings . |
44,909 | public function callKeywords ( $ index , $ text , $ fetchStatistic , & $ params ) { $ indexParamName = self :: PARAM_PREFIX . count ( $ params ) ; $ params [ $ indexParamName ] = $ index ; $ textParamName = self :: PARAM_PREFIX . count ( $ params ) ; $ params [ $ textParamName ] = $ text ; return 'CALL KEYWORDS(' . $ textParamName . ', ' . $ indexParamName . ( $ fetchStatistic ? ', 1' : '' ) . ')' ; } | Builds a SQL statement for returning tokenized and normalized forms of the keywords and optionally keyword statistics . |
44,910 | public function buildColumns ( $ columns ) { if ( ! is_array ( $ columns ) ) { if ( strpos ( $ columns , '(' ) !== false ) { return $ columns ; } else { $ columns = preg_split ( '/\s*,\s*/' , $ columns , - 1 , PREG_SPLIT_NO_EMPTY ) ; } } foreach ( $ columns as $ i => $ column ) { if ( $ column instanceof Expression ) { $ columns [ $ i ] = $ column -> expression ; } elseif ( strpos ( $ column , '(' ) === false ) { $ columns [ $ i ] = $ this -> db -> quoteColumnName ( $ column ) ; } } return is_array ( $ columns ) ? implode ( ', ' , $ columns ) : $ columns ; } | Processes columns and properly quote them if necessary . It will join all columns into a string with comma as separators . |
44,911 | protected function buildShowMeta ( $ showMeta , & $ params ) { if ( empty ( $ showMeta ) ) { return '' ; } $ sql = 'SHOW META' ; if ( is_bool ( $ showMeta ) ) { return $ sql ; } if ( $ showMeta instanceof Expression ) { foreach ( $ showMeta -> params as $ n => $ v ) { $ params [ $ n ] = $ v ; } $ phName = $ showMeta -> expression ; } else { $ phName = self :: PARAM_PREFIX . count ( $ params ) ; $ escape = [ '%' => '\%' , '_' => '\_' , '\\' => '\\\\' ] ; $ params [ $ phName ] = '%' . strtr ( $ showMeta , $ escape ) . '%' ; } $ sql .= " LIKE {$phName}" ; return $ sql ; } | Builds SHOW META query . |
44,912 | protected function composeColumnValue ( $ indexes , $ columnName , $ value , & $ params ) { if ( $ value === null ) { return 'NULL' ; } elseif ( $ value instanceof Expression ) { $ params = array_merge ( $ params , $ value -> params ) ; return $ value -> expression ; } foreach ( $ indexes as $ index ) { $ columnSchema = $ index -> getColumn ( $ columnName ) ; if ( $ columnSchema !== null ) { break ; } } if ( is_array ( $ value ) ) { $ lineParts = [ ] ; foreach ( $ value as $ subValue ) { if ( $ subValue instanceof Expression ) { $ params = array_merge ( $ params , $ subValue -> params ) ; $ lineParts [ ] = $ subValue -> expression ; } else { $ phName = self :: PARAM_PREFIX . count ( $ params ) ; $ lineParts [ ] = $ phName ; $ params [ $ phName ] = ( isset ( $ columnSchema ) ) ? $ columnSchema -> dbTypecast ( $ subValue ) : $ subValue ; } } return '(' . implode ( ',' , $ lineParts ) . ')' ; } else { $ phName = self :: PARAM_PREFIX . count ( $ params ) ; $ params [ $ phName ] = ( isset ( $ columnSchema ) ) ? $ columnSchema -> dbTypecast ( $ value ) : $ value ; return $ phName ; } } | Composes column value for SQL taking in account the column type . |
44,913 | public function replace ( $ index , $ columns ) { $ params = [ ] ; $ sql = $ this -> db -> getQueryBuilder ( ) -> replace ( $ index , $ columns , $ params ) ; return $ this -> setSql ( $ sql ) -> bindValues ( $ params ) ; } | Creates an REPLACE command . For example |
44,914 | public function batchReplace ( $ index , $ columns , $ rows ) { $ params = [ ] ; $ sql = $ this -> db -> getQueryBuilder ( ) -> batchReplace ( $ index , $ columns , $ rows , $ params ) ; return $ this -> setSql ( $ sql ) -> bindValues ( $ params ) ; } | Creates a batch REPLACE command . For example |
44,915 | public function truncateIndex ( $ index ) { $ sql = $ this -> db -> getQueryBuilder ( ) -> truncateIndex ( $ index ) ; return $ this -> setSql ( $ sql ) ; } | Creates a SQL command for truncating a runtime index . |
44,916 | protected function resetIndex ( ) { $ index = $ this -> getIndexSchema ( ) ; $ this -> db -> createCommand ( ) -> truncateIndex ( $ index -> name ) -> execute ( ) ; } | Truncates the specified index removing all existing data from it . This method is called before populating fixture data into the index associated with this fixture . |
44,917 | public function createCommand ( $ db = null ) { $ this -> setConnection ( $ db ) ; $ db = $ this -> getConnection ( ) ; list ( $ sql , $ params ) = $ db -> getQueryBuilder ( ) -> build ( $ this ) ; return $ db -> createCommand ( $ sql , $ params ) ; } | Creates a Sphinx command that can be used to execute this query . |
44,918 | public function addWithin ( $ columns ) { $ columns = $ this -> normalizeOrderBy ( $ columns ) ; if ( $ this -> within === null ) { $ this -> within = $ columns ; } else { $ this -> within = array_merge ( $ this -> within , $ columns ) ; } return $ this ; } | Adds additional WITHIN GROUP ORDER BY columns to the query . |
44,919 | public function addFacets ( $ facets ) { if ( is_array ( $ this -> facets ) ) { $ this -> facets = array_merge ( $ this -> facets , $ facets ) ; } else { $ this -> facets = $ facets ; } return $ this ; } | Adds additional FACET part of the query . |
44,920 | protected function callSnippetsInternal ( array $ source , $ from ) { $ connection = $ this -> getConnection ( ) ; $ match = $ this -> match ; if ( $ match === null ) { throw new InvalidCallException ( 'Unable to call snippets: "' . $ this -> className ( ) . '::match" should be specified.' ) ; } return $ connection -> createCommand ( ) -> callSnippets ( $ from , $ source , $ match , $ this -> snippetOptions ) -> queryColumn ( ) ; } | Builds a snippets from provided source data by the given index . |
44,921 | public static function create ( $ from ) { return new self ( [ 'where' => $ from -> where , 'limit' => $ from -> limit , 'offset' => $ from -> offset , 'orderBy' => $ from -> orderBy , 'indexBy' => $ from -> indexBy , 'select' => $ from -> select , 'selectOption' => $ from -> selectOption , 'distinct' => $ from -> distinct , 'from' => $ from -> from , 'groupBy' => $ from -> groupBy , 'join' => $ from -> join , 'having' => $ from -> having , 'union' => $ from -> union , 'params' => $ from -> params , 'groupLimit' => $ from -> groupLimit , 'options' => $ from -> options , 'within' => $ from -> within , 'match' => $ from -> match , 'snippetCallback' => $ from -> snippetCallback , 'snippetOptions' => $ from -> snippetOptions , ] ) ; } | Creates a new Query object and copies its property values from an existing one . The properties being copies are the ones to be used by query builders . |
44,922 | public function buildMatch ( $ match , & $ params ) { if ( empty ( $ match ) ) { return '' ; } if ( $ match instanceof Expression ) { return $ this -> buildMatchValue ( $ match , $ params ) ; } if ( ! is_array ( $ match ) ) { return $ match ; } if ( isset ( $ match [ 0 ] ) ) { $ operator = strtoupper ( $ match [ 0 ] ) ; if ( isset ( $ this -> matchBuilders [ $ operator ] ) ) { $ method = $ this -> matchBuilders [ $ operator ] ; } else { $ method = 'buildSimpleMatch' ; } array_shift ( $ match ) ; return $ this -> $ method ( $ operator , $ match , $ params ) ; } return $ this -> buildHashMatch ( $ match , $ params ) ; } | Create MATCH expression . |
44,923 | public function buildHashMatch ( $ match , & $ params ) { $ parts = [ ] ; foreach ( $ match as $ column => $ value ) { $ parts [ ] = $ this -> buildMatchColumn ( $ column ) . ' ' . $ this -> buildMatchValue ( $ value , $ params ) ; } return count ( $ parts ) === 1 ? $ parts [ 0 ] : '(' . implode ( ') (' , $ parts ) . ')' ; } | Creates a MATCH based on column - value pairs . |
44,924 | public function buildAndMatch ( $ operator , $ operands , & $ params ) { $ parts = [ ] ; foreach ( $ operands as $ operand ) { if ( is_array ( $ operand ) || is_object ( $ operand ) ) { $ operand = $ this -> buildMatch ( $ operand , $ params ) ; } if ( $ operand !== '' ) { $ parts [ ] = $ operand ; } } if ( empty ( $ parts ) ) { return '' ; } return '(' . implode ( ')' . ( $ operator === 'OR' ? ' | ' : ' ' ) . '(' , $ parts ) . ')' ; } | Connects two or more MATCH expressions with the AND or OR operator |
44,925 | public function buildMultipleMatch ( $ operator , $ operands , & $ params ) { if ( count ( $ operands ) < 3 ) { throw new InvalidParamException ( "Operator '$operator' requires three or more operands." ) ; } $ column = array_shift ( $ operands ) ; $ phNames = [ ] ; foreach ( $ operands as $ operand ) { $ phNames [ ] = $ this -> buildMatchValue ( $ operand , $ params ) ; } return $ this -> buildMatchColumn ( $ column ) . ' ' . implode ( ' ' . $ operator . ' ' , $ phNames ) ; } | Create MAYBE SENTENCE or PARAGRAPH expressions . |
44,926 | public function buildZoneMatch ( $ operator , $ operands , & $ params ) { if ( ! isset ( $ operands [ 0 ] ) ) { throw new InvalidParamException ( "Operator '$operator' requires exactly one operand." ) ; } $ zones = ( array ) $ operands [ 0 ] ; return "$operator: (" . implode ( ',' , $ zones ) . ")" ; } | Create MATCH expressions for zones . |
44,927 | public function buildProximityMatch ( $ operator , $ operands , & $ params ) { if ( ! isset ( $ operands [ 0 ] , $ operands [ 1 ] , $ operands [ 2 ] ) ) { throw new InvalidParamException ( "Operator '$operator' requires three operands." ) ; } list ( $ column , $ value , $ proximity ) = $ operands ; return $ this -> buildMatchColumn ( $ column ) . ' ' . $ this -> buildMatchValue ( $ value , $ params ) . '~' . ( int ) $ proximity ; } | Create PROXIMITY expressions |
44,928 | public function buildIgnoreMatch ( $ operator , $ operands , & $ params ) { if ( ! isset ( $ operands [ 0 ] , $ operands [ 1 ] ) ) { throw new InvalidParamException ( "Operator '$operator' requires two operands." ) ; } list ( $ column , $ value ) = $ operands ; return $ this -> buildMatchColumn ( $ column , true ) . ' ' . $ this -> buildMatchValue ( $ value , $ params ) ; } | Create ignored MATCH expressions |
44,929 | public function buildSimpleMatch ( $ operator , $ operands , & $ params ) { if ( count ( $ operands ) !== 2 ) { throw new InvalidParamException ( "Operator '$operator' requires two operands." ) ; } list ( $ column , $ value ) = $ operands ; if ( isset ( $ this -> matchOperators [ $ operator ] ) ) { $ operator = $ this -> matchOperators [ $ operator ] ; } return $ this -> buildMatchColumn ( $ column ) . $ operator . $ this -> buildMatchValue ( $ value , $ params ) ; } | Creates an Match expressions like column operator value . |
44,930 | protected function buildMatchValue ( $ value , & $ params ) { if ( empty ( $ value ) ) { return '""' ; } if ( $ value instanceof Expression ) { $ params = array_merge ( $ params , $ value -> params ) ; return $ value -> expression ; } $ parts = [ ] ; foreach ( ( array ) $ value as $ v ) { if ( $ v instanceof Expression ) { $ params = array_merge ( $ params , $ v -> params ) ; $ parts [ ] = $ v -> expression ; } else { $ phName = self :: PARAM_PREFIX . count ( $ params ) ; $ parts [ ] = $ phName ; $ params [ $ phName ] = $ v ; } } return implode ( ' | ' , $ parts ) ; } | Create placeholder for expression of MATCH |
44,931 | protected function buildMatchColumn ( $ column , $ ignored = false ) { if ( empty ( $ column ) ) { return '' ; } if ( $ column === '*' ) { return '@*' ; } return '@' . ( $ ignored ? '!' : '' ) . ( strpos ( $ column , ',' ) === false ? $ column : '(' . $ column . ')' ) ; } | Created column as string for expression of MATCH |
44,932 | protected function parseParams ( $ expression , $ params ) { if ( empty ( $ params ) ) { return $ expression ; } foreach ( $ params as $ name => $ value ) { if ( strncmp ( $ name , ':' , 1 ) !== 0 ) { $ name = ':' . $ name ; } $ pattern = "/" . preg_quote ( $ name , '/' ) . '\b/' ; $ value = '"' . $ this -> db -> escapeMatchValue ( $ value ) . '"' ; $ expression = preg_replace ( $ pattern , $ value , $ expression , - 1 , $ cnt ) ; } return $ expression ; } | Returns the actual MATCH expression by inserting parameter values into the corresponding placeholders . |
44,933 | public static function deleteAll ( $ condition = '' , $ params = [ ] ) { $ command = static :: getDb ( ) -> createCommand ( ) ; $ command -> delete ( static :: indexName ( ) , $ condition , $ params ) ; return $ command -> execute ( ) ; } | Deletes rows in the index using the provided conditions . |
44,934 | public static function getIndexSchema ( ) { $ schema = static :: getDb ( ) -> getIndexSchema ( static :: indexName ( ) ) ; if ( $ schema !== null ) { return $ schema ; } else { throw new InvalidConfigException ( "The index does not exist: " . static :: indexName ( ) ) ; } } | Returns the schema information of the Sphinx index associated with this AR class . |
44,935 | public function getSnippet ( $ match = null , $ options = [ ] ) { if ( $ match !== null ) { $ this -> _snippet = $ this -> fetchSnippet ( $ match , $ options ) ; } return $ this -> _snippet ; } | Returns current snippet value or generates new one from given match . |
44,936 | public function insert ( $ runValidation = true , $ attributes = null ) { if ( $ runValidation && ! $ this -> validate ( $ attributes ) ) { return false ; } $ db = static :: getDb ( ) ; if ( $ this -> isTransactional ( self :: OP_INSERT ) && $ db -> getTransaction ( ) === null ) { $ transaction = $ db -> beginTransaction ( ) ; try { $ result = $ this -> insertInternal ( $ attributes ) ; if ( $ result === false ) { $ transaction -> rollBack ( ) ; } else { $ transaction -> commit ( ) ; } } catch ( \ Exception $ e ) { $ transaction -> rollBack ( ) ; throw $ e ; } } else { $ result = $ this -> insertInternal ( $ attributes ) ; } return $ result ; } | Inserts a row into the associated Sphinx index using the attribute values of this record . |
44,937 | public function delete ( ) { $ db = static :: getDb ( ) ; $ transaction = $ this -> isTransactional ( self :: OP_DELETE ) && $ db -> getTransaction ( ) === null ? $ db -> beginTransaction ( ) : null ; try { $ result = false ; if ( $ this -> beforeDelete ( ) ) { $ condition = $ this -> getOldPrimaryKey ( true ) ; $ lock = $ this -> optimisticLock ( ) ; if ( $ lock !== null ) { $ condition [ $ lock ] = $ this -> $ lock ; } $ result = $ this -> deleteAll ( $ condition ) ; if ( $ lock !== null && ! $ result ) { throw new StaleObjectException ( 'The object being deleted is outdated.' ) ; } $ this -> setOldAttributes ( null ) ; $ this -> afterDelete ( ) ; } if ( $ transaction !== null ) { if ( $ result === false ) { $ transaction -> rollBack ( ) ; } else { $ transaction -> commit ( ) ; } } } catch ( \ Exception $ e ) { if ( $ transaction !== null ) { $ transaction -> rollBack ( ) ; } throw $ e ; } return $ result ; } | Deletes the index entry corresponding to this active record . |
44,938 | public function generateRules ( $ index ) { $ types = [ ] ; foreach ( $ index -> columns as $ column ) { if ( $ column -> isMva ) { $ types [ 'safe' ] [ ] = $ column -> name ; continue ; } if ( $ column -> isPrimaryKey ) { $ types [ 'required' ] [ ] = $ column -> name ; $ types [ 'unique' ] [ ] = $ column -> name ; } switch ( $ column -> type ) { case Schema :: TYPE_PK : case Schema :: TYPE_INTEGER : case Schema :: TYPE_BIGINT : $ types [ 'integer' ] [ ] = $ column -> name ; break ; case Schema :: TYPE_BOOLEAN : $ types [ 'boolean' ] [ ] = $ column -> name ; break ; case Schema :: TYPE_FLOAT : $ types [ 'number' ] [ ] = $ column -> name ; break ; case Schema :: TYPE_TIMESTAMP : $ types [ 'safe' ] [ ] = $ column -> name ; break ; default : $ types [ 'string' ] [ ] = $ column -> name ; } } $ rules = [ ] ; foreach ( $ types as $ type => $ columns ) { $ rules [ ] = "[['" . implode ( "', '" , $ columns ) . "'], '$type']" ; } return $ rules ; } | Generates validation rules for the specified index . |
44,939 | public function updateCommentsCount ( PostInterface $ post = null ) { $ commentTableName = $ this -> getObjectManager ( ) -> getClassMetadata ( $ this -> getClass ( ) ) -> table [ 'name' ] ; $ postTableName = $ this -> getObjectManager ( ) -> getClassMetadata ( $ this -> postManager -> getClass ( ) ) -> table [ 'name' ] ; $ this -> getConnection ( ) -> beginTransaction ( ) ; $ this -> getConnection ( ) -> query ( $ this -> getCommentsCountResetQuery ( $ postTableName ) ) ; $ this -> getConnection ( ) -> query ( $ this -> getCommentsCountQuery ( $ postTableName , $ commentTableName ) ) ; $ this -> getConnection ( ) -> commit ( ) ; } | Update the number of comment for a comment . |
44,940 | public function updateCommentsCount ( PostInterface $ post = null ) { $ post -> setCommentsCount ( $ post -> getCommentsCount ( ) + 1 ) ; $ this -> getDocumentManager ( ) -> persist ( $ post ) ; $ this -> getDocumentManager ( ) -> flush ( ) ; } | Update the comments count . |
44,941 | private function resolveRequest ( Request $ request = null ) { if ( null === $ request ) { return $ this -> get ( 'request_stack' ) -> getCurrentRequest ( ) ; } return $ request ; } | To keep backwards compatibility with older Sonata News code . |
44,942 | public function deletePostAction ( $ id ) { $ post = $ this -> getPost ( $ id ) ; try { $ this -> postManager -> delete ( $ post ) ; } catch ( \ Exception $ e ) { return View :: create ( [ 'error' => $ e -> getMessage ( ) ] , 400 ) ; } return [ 'deleted' => true ] ; } | Deletes a post . |
44,943 | public function getPostCommentsAction ( $ id , ParamFetcherInterface $ paramFetcher ) { $ post = $ this -> getPost ( $ id ) ; $ page = $ paramFetcher -> get ( 'page' ) ; $ count = $ paramFetcher -> get ( 'count' ) ; $ criteria = $ this -> filterCriteria ( $ paramFetcher ) ; $ criteria [ 'postId' ] = $ post -> getId ( ) ; $ pager = $ this -> commentManager -> getPager ( $ criteria , $ page , $ count ) ; return $ pager ; } | Retrieves the comments of specified post . |
44,944 | public function postPostCommentsAction ( $ id , Request $ request ) { $ post = $ this -> getPost ( $ id ) ; if ( ! $ post -> isCommentable ( ) ) { throw new HttpException ( 403 , sprintf ( 'Post (%d) not commentable' , $ id ) ) ; } $ comment = $ this -> commentManager -> create ( ) ; $ comment -> setPost ( $ post ) ; $ form = $ this -> formFactory -> createNamed ( null , 'sonata_news_api_form_comment' , $ comment , [ 'csrf_protection' => false ] ) ; $ form -> handleRequest ( $ request ) ; if ( $ form -> isValid ( ) ) { $ comment = $ form -> getData ( ) ; $ comment -> setPost ( $ post ) ; if ( ! $ comment -> getStatus ( ) ) { $ comment -> setStatus ( $ post -> getCommentsDefaultStatus ( ) ) ; } $ this -> commentManager -> save ( $ comment ) ; $ this -> mailer -> sendCommentNotification ( $ comment ) ; return $ comment ; } return $ form ; } | Adds a comment to a post . |
44,945 | public function putPostCommentsAction ( $ postId , $ commentId , Request $ request ) { $ post = $ this -> getPost ( $ postId ) ; if ( ! $ post -> isCommentable ( ) ) { throw new HttpException ( 403 , sprintf ( 'Post (%d) not commentable' , $ postId ) ) ; } $ comment = $ this -> commentManager -> find ( $ commentId ) ; if ( null === $ comment ) { throw new NotFoundHttpException ( sprintf ( 'Comment (%d) not found' , $ commentId ) ) ; } $ comment -> setPost ( $ post ) ; $ form = $ this -> formFactory -> createNamed ( null , 'sonata_news_api_form_comment' , $ comment , [ 'csrf_protection' => false , ] ) ; $ form -> handleRequest ( $ request ) ; if ( $ form -> isValid ( ) ) { $ comment = $ form -> getData ( ) ; $ this -> commentManager -> save ( $ comment ) ; return $ comment ; } return $ form ; } | Updates a comment . |
44,946 | protected function handleWritePost ( $ request , $ id = null ) { $ post = $ id ? $ this -> getPost ( $ id ) : null ; $ form = $ this -> formFactory -> createNamed ( null , 'sonata_news_api_form_post' , $ post , [ 'csrf_protection' => false , ] ) ; $ form -> handleRequest ( $ request ) ; if ( $ form -> isValid ( ) ) { $ post = $ form -> getData ( ) ; $ post -> setContent ( $ this -> formatterPool -> transform ( $ post -> getContentFormatter ( ) , $ post -> getRawContent ( ) ) ) ; $ this -> postManager -> save ( $ post ) ; $ context = new Context ( ) ; $ context -> setGroups ( [ 'sonata_api_read' ] ) ; if ( method_exists ( $ context , 'enableMaxDepth' ) ) { $ context -> enableMaxDepth ( ) ; } else { $ context -> setMaxDepth ( 10 ) ; } $ view = View :: create ( $ post ) ; $ view -> setContext ( $ context ) ; return $ view ; } return $ form ; } | Write a post this method is used by both POST and PUT action methods . |
44,947 | protected function getComment ( $ id ) { $ comment = $ this -> commentManager -> find ( $ id ) ; if ( null === $ comment ) { throw new NotFoundHttpException ( sprintf ( 'Comment (%d) not found' , $ id ) ) ; } return $ comment ; } | Returns a comment entity instance . |
44,948 | protected static function openTag ( $ tag , $ add , $ whitespace ) { $ tabs = str_pad ( '' , self :: $ tabCount ++ , "\t" ) ; if ( preg_match ( '#^<(a|label|option|textarea|h1|h2|h3|h4|h5|h6|strong|b|em|i|abbr|acronym|cite|span|sub|sup|u|s|title)(?: [^>]*|)>#' , $ tag ) ) { $ result = $ tag . $ whitespace . str_replace ( "\n" , "\n" . $ tabs , $ add ) ; } elseif ( substr ( $ tag , 0 , 9 ) == '<!DOCTYPE' ) { $ result = $ tabs . $ tag ; } else { $ result = "\n" . $ tabs . $ tag ; if ( ! empty ( $ add ) ) { $ result .= "\n" . $ tabs . "\t" . str_replace ( "\n" , "\n\t" . $ tabs , $ add ) ; } } self :: $ lastCallAdd = $ add ; return $ result ; } | returns an open tag and adds a tab into the auto indenting . |
44,949 | public function clearCache ( Core $ core , $ olderThan = - 1 ) { $ cachedFile = $ this -> getCacheFilename ( $ core ) ; return ! file_exists ( $ cachedFile ) || ( filectime ( $ cachedFile ) < ( time ( ) - $ olderThan ) && unlink ( $ cachedFile ) ) ; } | Clears the cached template if it s older than the given time . |
44,950 | public function getCompiledTemplate ( Core $ core , ICompiler $ compiler = null ) { $ compiledFile = $ this -> getCompiledFilename ( $ core ) ; if ( $ this -> compilationEnforced !== true && isset ( self :: $ cache [ 'compiled' ] [ $ this -> compileId ] ) === true ) { } elseif ( $ this -> compilationEnforced !== true && $ this -> isValidCompiledFile ( $ compiledFile ) ) { self :: $ cache [ 'compiled' ] [ $ this -> compileId ] = true ; } else { $ this -> compilationEnforced = false ; if ( $ compiler === null ) { $ compiler = $ core -> getDefaultCompilerFactory ( $ this -> getResourceName ( ) ) ; if ( $ compiler === null || $ compiler === array ( 'Dwoo\Compiler' , 'compilerFactory' ) ) { $ compiler = Compiler :: compilerFactory ( ) ; } else { $ compiler = call_user_func ( $ compiler ) ; } } $ this -> compiler = $ compiler ; $ compiler -> setCustomPlugins ( $ core -> getCustomPlugins ( ) ) ; $ compiler -> setSecurityPolicy ( $ core -> getSecurityPolicy ( ) ) ; $ this -> makeDirectory ( dirname ( $ compiledFile ) , $ core -> getCompileDir ( ) ) ; file_put_contents ( $ compiledFile , $ compiler -> compile ( $ core , $ this ) ) ; if ( $ this -> chmod !== null ) { chmod ( $ compiledFile , $ this -> chmod ) ; } if ( extension_loaded ( 'Zend OPcache' ) ) { opcache_invalidate ( $ compiledFile ) ; } elseif ( extension_loaded ( 'apc' ) && ini_get ( 'apc.enabled' ) ) { apc_delete_file ( $ compiledFile ) ; } self :: $ cache [ 'compiled' ] [ $ this -> compileId ] = true ; } return $ compiledFile ; } | Returns the compiled template file name . |
44,951 | public static function templateFactory ( Core $ core , $ resourceId , $ cacheTime = null , $ cacheId = null , $ compileId = null , ITemplate $ parentTemplate = null ) { return new self ( $ resourceId , $ cacheTime , $ cacheId , $ compileId ) ; } | Returns a new template string object with the resource id being the template source code . |
44,952 | protected function getCompiledFilename ( Core $ core ) { return $ core -> getCompileDir ( ) . hash ( 'md4' , $ this -> compileId ) . '.d' . Core :: RELEASE_TAG . '.php' ; } | Returns the full compiled file name and assigns a default value to it if required . |
44,953 | protected function getCacheFilename ( Core $ core ) { if ( $ this -> cacheId === null ) { if ( isset ( $ _SERVER [ 'REQUEST_URI' ] ) === true ) { $ cacheId = $ _SERVER [ 'REQUEST_URI' ] ; } elseif ( isset ( $ _SERVER [ 'SCRIPT_FILENAME' ] ) && isset ( $ _SERVER [ 'argv' ] ) ) { $ cacheId = $ _SERVER [ 'SCRIPT_FILENAME' ] . '-' . implode ( '-' , $ _SERVER [ 'argv' ] ) ; } else { $ cacheId = '' ; } $ this -> getCompiledFilename ( $ core ) ; $ this -> cacheId = str_replace ( '../' , '__' , $ this -> compileId . strtr ( $ cacheId , '\\%?=!:;' . PATH_SEPARATOR , '/-------' ) ) ; } return $ core -> getCacheDir ( ) . $ this -> cacheId . '.html' ; } | Returns the full cached file name and assigns a default value to it if required . |
44,954 | protected function makeDirectory ( $ path , $ baseDir = null ) { if ( is_dir ( $ path ) === true ) { return ; } if ( $ this -> chmod === null ) { $ chmod = 0777 ; } else { $ chmod = $ this -> chmod ; } $ retries = 3 ; while ( $ retries -- ) { @ mkdir ( $ path , $ chmod , true ) ; if ( is_dir ( $ path ) ) { break ; } usleep ( 20 ) ; } if ( strpos ( PHP_OS , 'WIN' ) !== 0 && $ baseDir !== null ) { $ path = strtr ( str_replace ( $ baseDir , '' , $ path ) , '\\' , '/' ) ; $ folders = explode ( '/' , trim ( $ path , '/' ) ) ; foreach ( $ folders as $ folder ) { $ baseDir .= $ folder . DIRECTORY_SEPARATOR ; if ( ! chmod ( $ baseDir , $ chmod ) ) { throw new Exception ( 'Unable to chmod ' . "$baseDir to $chmod: " . print_r ( error_get_last ( ) , true ) ) ; } } } } | Ensures the given path exists . |
44,955 | public function getResourceIdentifier ( ) { if ( $ this -> resolvedPath !== null ) { return $ this -> resolvedPath ; } elseif ( array_filter ( $ this -> getIncludePath ( ) ) == array ( ) ) { return $ this -> file ; } else { foreach ( $ this -> getIncludePath ( ) as $ path ) { $ path = rtrim ( $ path , DIRECTORY_SEPARATOR ) ; if ( file_exists ( $ path . DIRECTORY_SEPARATOR . $ this -> file ) === true ) { return $ this -> resolvedPath = $ path . DIRECTORY_SEPARATOR . $ this -> file ; } } throw new DwooException ( 'Template "' . $ this -> file . '" could not be found in any of your include path(s)' ) ; } } | Returns this template s source filename . |
44,956 | public function addGlobal ( $ name , $ value ) { if ( null === $ this -> globals ) { $ this -> initGlobals ( ) ; } $ this -> globals [ $ name ] = $ value ; return $ this ; } | Registers a Global . New globals can be added before compiling or rendering a template but after you can only update existing globals . |
44,957 | protected function initRuntimeVars ( ITemplate $ tpl ) { $ this -> runtimePlugins = array ( ) ; $ this -> scope = & $ this -> data ; $ this -> scopeTree = array ( ) ; $ this -> stack = array ( ) ; $ this -> curBlock = null ; $ this -> buffer = '' ; $ this -> returnData = array ( ) ; } | Re - initializes the runtime variables before each template run . override this method to inject data in the globals array if needed this method is called before each template execution |
44,958 | public function addFilter ( $ callback , $ autoload = false ) { if ( $ autoload ) { $ class = self :: NAMESPACE_PLUGINS_FILTERS . self :: toCamelCase ( $ callback ) ; if ( ! class_exists ( $ class ) && ! function_exists ( $ class ) ) { try { $ this -> getLoader ( ) -> loadPlugin ( $ callback ) ; } catch ( Exception $ e ) { if ( strstr ( $ callback , self :: NAMESPACE_PLUGINS_FILTERS ) ) { throw new Exception ( 'Wrong filter name : ' . $ callback . ', the "Filter" prefix should not be used, please only use "' . str_replace ( 'Filter' , '' , $ callback ) . '"' ) ; } else { throw new Exception ( 'Wrong filter name : ' . $ callback . ', when using autoload the filter must be in one of your plugin dir as "name.php" containig a class or function named "Filter<name>"' ) ; } } } if ( class_exists ( $ class ) ) { $ callback = array ( new $ class ( $ this ) , 'process' ) ; } elseif ( function_exists ( $ class ) ) { $ callback = $ class ; } else { throw new Exception ( 'Wrong filter name : ' . $ callback . ', when using autoload the filter must be in one of your plugin dir as "name.php" containig a class or function named "Filter<name>"' ) ; } $ this -> filters [ ] = $ callback ; } else { $ this -> filters [ ] = $ callback ; } } | Adds a filter to this Dwoo instance it will be used to filter the output of all the templates rendered by this instance . |
44,959 | public function removeFilter ( $ callback ) { if ( ( $ index = array_search ( self :: NAMESPACE_PLUGINS_FILTERS . 'Filter' . self :: toCamelCase ( $ callback ) , $ this -> filters , true ) ) !== false ) { unset ( $ this -> filters [ $ index ] ) ; } elseif ( ( $ index = array_search ( $ callback , $ this -> filters , true ) ) !== false ) { unset ( $ this -> filters [ $ index ] ) ; } else { $ class = self :: NAMESPACE_PLUGINS_FILTERS . 'Filter' . $ callback ; foreach ( $ this -> filters as $ index => $ filter ) { if ( is_array ( $ filter ) && $ filter [ 0 ] instanceof $ class ) { unset ( $ this -> filters [ $ index ] ) ; break ; } } } } | Removes a filter . |
44,960 | public function addResource ( $ name , $ class , $ compilerFactory = null ) { if ( strlen ( $ name ) < 2 ) { throw new Exception ( 'Resource names must be at least two-character long to avoid conflicts with Windows paths' ) ; } if ( ! class_exists ( $ class ) ) { throw new Exception ( sprintf ( 'Resource class %s does not exist' , $ class ) ) ; } $ interfaces = class_implements ( $ class ) ; if ( in_array ( 'Dwoo\ITemplate' , $ interfaces ) === false ) { throw new Exception ( 'Resource class must implement ITemplate' ) ; } $ this -> resources [ $ name ] = array ( 'class' => $ class , 'compiler' => $ compilerFactory ) ; } | Adds a resource or overrides a default one . |
44,961 | public function getLoader ( ) { if ( $ this -> loader === null ) { $ this -> loader = new Loader ( $ this -> getCompileDir ( ) ) ; } return $ this -> loader ; } | Returns the current loader object or a default one if none is currently found . |
44,962 | public function getCustomPlugin ( $ name ) { if ( isset ( $ this -> plugins [ $ name ] ) ) { return $ this -> plugins [ $ name ] [ 'callback' ] ; } return null ; } | Return a specified custom plugin loaded by his name . Used by the compiler for executing a Closure . |
44,963 | public function getCacheDir ( ) { if ( $ this -> cacheDir === null ) { $ this -> setCacheDir ( dirname ( __DIR__ ) . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR ) ; } return $ this -> cacheDir ; } | Returns the cache directory with a trailing DIRECTORY_SEPARATOR . |
44,964 | public function setCacheDir ( $ dir ) { $ this -> cacheDir = rtrim ( $ dir , '/\\' ) . DIRECTORY_SEPARATOR ; if ( ! file_exists ( $ this -> cacheDir ) ) { mkdir ( $ this -> cacheDir , 0777 , true ) ; } if ( is_writable ( $ this -> cacheDir ) === false ) { throw new Exception ( 'The cache directory must be writable, chmod "' . $ this -> cacheDir . '" to make it writable' ) ; } } | Sets the cache directory and automatically appends a DIRECTORY_SEPARATOR . |
44,965 | public function getCompileDir ( ) { if ( $ this -> compileDir === null ) { $ this -> setCompileDir ( dirname ( __DIR__ ) . DIRECTORY_SEPARATOR . 'compiled' . DIRECTORY_SEPARATOR ) ; } return $ this -> compileDir ; } | Returns the compile directory with a trailing DIRECTORY_SEPARATOR . |
44,966 | public function setCompileDir ( $ dir ) { $ this -> compileDir = rtrim ( $ dir , '/\\' ) . DIRECTORY_SEPARATOR ; if ( ! file_exists ( $ this -> compileDir ) ) { mkdir ( $ this -> compileDir , 0777 , true ) ; } if ( is_writable ( $ this -> compileDir ) === false ) { throw new Exception ( 'The compile directory must be writable, chmod "' . $ this -> compileDir . '" to make it writable' ) ; } } | Sets the compile directory and automatically appends a DIRECTORY_SEPARATOR . |
44,967 | public function setTemplateDir ( $ dir ) { $ tmpDir = rtrim ( $ dir , '/\\' ) . DIRECTORY_SEPARATOR ; if ( is_dir ( $ tmpDir ) === false ) { throw new Exception ( 'The template directory: "' . $ tmpDir . '" does not exists, create the directory or specify an other location !' ) ; } $ this -> templateDir [ ] = $ tmpDir ; } | sets the template directory and automatically appends a DIRECTORY_SEPARATOR template directory is stored in an array |
44,968 | public function clearCompiled ( ) { $ iterator = new \ RecursiveIteratorIterator ( new \ RecursiveDirectoryIterator ( $ this -> getCompileDir ( ) ) , \ RecursiveIteratorIterator :: SELF_FIRST ) ; $ count = 0 ; foreach ( $ iterator as $ file ) { if ( $ file -> isFile ( ) ) { $ count += unlink ( $ file -> __toString ( ) ) ? 1 : 0 ; } } return $ count ; } | Clear templates inside the compiled directory . |
44,969 | public function clearCache ( $ olderThan = - 1 ) { $ iterator = new \ RecursiveIteratorIterator ( new \ RecursiveDirectoryIterator ( $ this -> getCacheDir ( ) ) , \ RecursiveIteratorIterator :: SELF_FIRST ) ; $ expired = time ( ) - $ olderThan ; $ count = 0 ; foreach ( $ iterator as $ file ) { if ( $ file -> isFile ( ) && $ file -> getCTime ( ) < $ expired ) { $ count += unlink ( ( string ) $ file ) ? 1 : 0 ; } } return $ count ; } | Clears the cached templates if they are older than the given time . |
44,970 | public function templateFactory ( $ resourceName , $ resourceId , $ cacheTime = null , $ cacheId = null , $ compileId = null , ITemplate $ parentTemplate = null ) { if ( isset ( $ this -> resources [ $ resourceName ] ) ) { $ class = $ this -> resources [ $ resourceName ] [ 'class' ] ; return $ class :: templateFactory ( $ this , $ resourceId , $ cacheTime , $ cacheId , $ compileId , $ parentTemplate ) ; } throw new Exception ( 'Unknown resource type : ' . $ resourceName ) ; } | Fetches a template object of the given resource . |
44,971 | public function isArray ( $ value , $ checkIsEmpty = false ) { if ( is_array ( $ value ) === true || $ value instanceof ArrayAccess ) { if ( $ checkIsEmpty === false ) { return true ; } return $ this -> count ( $ value ) ; } return false ; } | Checks if the input is an array or arrayaccess object optionally it can also check if it s empty . |
44,972 | public function isTraversable ( $ value , $ checkIsEmpty = false ) { if ( is_array ( $ value ) === true ) { if ( $ checkIsEmpty === false ) { return true ; } else { return count ( $ value ) > 0 ; } } elseif ( $ value instanceof Traversable ) { if ( $ checkIsEmpty === false ) { return true ; } else { return $ this -> count ( $ value ) ; } } return false ; } | Checks if the input is an array or a traversable object optionally it can also check if it s empty . |
44,973 | public function triggerError ( $ message , $ level = E_USER_NOTICE ) { if ( ! ( $ tplIdentifier = $ this -> template -> getResourceIdentifier ( ) ) ) { $ tplIdentifier = $ this -> template -> getResourceName ( ) ; } trigger_error ( 'Dwoo error (in ' . $ tplIdentifier . ') : ' . $ message , $ level ) ; } | Triggers a dwoo error . |
44,974 | public function addStack ( $ blockName , array $ args = array ( ) ) { if ( isset ( $ this -> plugins [ $ blockName ] ) ) { $ class = $ this -> plugins [ $ blockName ] [ 'class' ] ; } else { $ class = current ( array_filter ( [ 'Plugin' . self :: toCamelCase ( $ blockName ) , self :: NAMESPACE_PLUGINS_BLOCKS . 'Plugin' . self :: toCamelCase ( $ blockName ) ] , 'class_exists' ) ) ; } if ( $ this -> curBlock !== null ) { $ this -> curBlock -> buffer ( ob_get_contents ( ) ) ; ob_clean ( ) ; } else { $ this -> buffer .= ob_get_contents ( ) ; ob_clean ( ) ; } $ block = new $ class ( $ this ) ; call_user_func_array ( array ( $ block , 'init' ) , $ args ) ; $ this -> stack [ ] = $ this -> curBlock = $ block ; return $ block ; } | Adds a block to the block stack . |
44,975 | public function readVarInto ( $ varstr , $ data , $ safeRead = false ) { if ( $ data === null ) { return null ; } if ( is_array ( $ varstr ) === false ) { preg_match_all ( '#(\[|->|\.)?((?:[^.[\]-]|-(?!>))+)\]?#i' , $ varstr , $ m ) ; } else { $ m = $ varstr ; } unset ( $ varstr ) ; foreach ( $ m [ 1 ] as $ k => $ sep ) { if ( $ sep === '.' || $ sep === '[' || $ sep === '' ) { $ m [ 2 ] [ $ k ] = preg_replace ( '#^(["\']?)(.*?)\1$#' , '$2' , $ m [ 2 ] [ $ k ] ) ; if ( ( is_array ( $ data ) || $ data instanceof ArrayAccess ) && ( $ safeRead === false || isset ( $ data [ $ m [ 2 ] [ $ k ] ] ) ) ) { $ data = $ data [ $ m [ 2 ] [ $ k ] ] ; } else { return null ; } } else { if ( is_object ( $ data ) && ( $ safeRead === false || isset ( $ data -> { $ m [ 2 ] [ $ k ] } ) ) ) { $ data = $ data -> { $ m [ 2 ] [ $ k ] } ; } else { return null ; } } } return $ data ; } | Reads a variable into the given data array . |
44,976 | public function assignInScope ( $ value , $ scope ) { if ( ! is_string ( $ scope ) ) { $ this -> triggerError ( 'Assignments must be done into strings, (' . gettype ( $ scope ) . ') ' . var_export ( $ scope , true ) . ' given' , E_USER_ERROR ) ; } if ( strstr ( $ scope , '.' ) === false && strstr ( $ scope , '->' ) === false ) { $ this -> scope [ $ scope ] = $ value ; } else { preg_match_all ( '#(\[|->|\.)?([^.[\]-]+)\]?#i' , $ scope , $ m ) ; $ cur = & $ this -> scope ; $ last = array ( array_pop ( $ m [ 1 ] ) , array_pop ( $ m [ 2 ] ) ) ; foreach ( $ m [ 1 ] as $ k => $ sep ) { if ( $ sep === '.' || $ sep === '[' || $ sep === '' ) { if ( is_array ( $ cur ) === false ) { $ cur = array ( ) ; } $ cur = & $ cur [ $ m [ 2 ] [ $ k ] ] ; } elseif ( $ sep === '->' ) { if ( is_object ( $ cur ) === false ) { $ cur = new stdClass ( ) ; } $ cur = & $ cur -> { $ m [ 2 ] [ $ k ] } ; } else { return false ; } } if ( $ last [ 0 ] === '.' || $ last [ 0 ] === '[' || $ last [ 0 ] === '' ) { if ( is_array ( $ cur ) === false ) { $ cur = array ( ) ; } $ cur [ $ last [ 1 ] ] = $ value ; } elseif ( $ last [ 0 ] === '->' ) { if ( is_object ( $ cur ) === false ) { $ cur = new stdClass ( ) ; } $ cur -> { $ last [ 1 ] } = $ value ; } else { return false ; } } } | Assign the value to the given variable . |
44,977 | public function assign ( $ name , $ val = null ) { if ( is_array ( $ name ) ) { reset ( $ name ) ; foreach ( $ name as $ k => $ v ) { $ this -> data [ $ k ] = $ v ; } } else { $ this -> data [ $ name ] = $ val ; } } | Assigns a value or an array of values to the data object . |
44,978 | public function append ( $ name , $ val = null , $ merge = false ) { if ( is_array ( $ name ) ) { foreach ( $ name as $ key => $ val ) { if ( isset ( $ this -> data [ $ key ] ) && ! is_array ( $ this -> data [ $ key ] ) ) { settype ( $ this -> data [ $ key ] , 'array' ) ; } if ( $ merge === true && is_array ( $ val ) ) { $ this -> data [ $ key ] = $ val + $ this -> data [ $ key ] ; } else { $ this -> data [ $ key ] [ ] = $ val ; } } } elseif ( $ val !== null ) { if ( isset ( $ this -> data [ $ name ] ) && ! is_array ( $ this -> data [ $ name ] ) ) { settype ( $ this -> data [ $ name ] , 'array' ) ; } elseif ( ! isset ( $ this -> data [ $ name ] ) ) { $ this -> data [ $ name ] = array ( ) ; } if ( $ merge === true && is_array ( $ val ) ) { $ this -> data [ $ name ] = $ val + $ this -> data [ $ name ] ; } else { $ this -> data [ $ name ] [ ] = $ val ; } } } | Appends values or an array of values to the data object . |
44,979 | public function appendByRef ( $ name , & $ val , $ merge = false ) { if ( isset ( $ this -> data [ $ name ] ) && ! is_array ( $ this -> data [ $ name ] ) ) { settype ( $ this -> data [ $ name ] , 'array' ) ; } if ( $ merge === true && is_array ( $ val ) ) { foreach ( $ val as $ key => & $ value ) { $ this -> data [ $ name ] [ $ key ] = & $ value ; } } else { $ this -> data [ $ name ] [ ] = & $ val ; } } | Appends a value by reference to the data object . |
44,980 | public function addPostProcessor ( $ callback , $ autoload = false ) { if ( $ autoload ) { $ name = str_replace ( Core :: NAMESPACE_PLUGINS_PROCESSORS , '' , $ callback ) ; $ class = Core :: NAMESPACE_PLUGINS_PROCESSORS . Core :: toCamelCase ( $ name ) ; if ( class_exists ( $ class ) ) { $ callback = array ( new $ class ( $ this ) , 'process' ) ; } elseif ( function_exists ( $ class ) ) { $ callback = $ class ; } else { $ callback = array ( 'autoload' => true , 'class' => $ class , 'name' => $ name ) ; } $ this -> processors [ 'post' ] [ ] = $ callback ; } else { $ this -> processors [ 'post' ] [ ] = $ callback ; } } | Adds a postprocessor to the compiler it will be called before the template is compiled . |
44,981 | protected function loadProcessor ( $ class , $ name ) { if ( ! class_exists ( $ class ) && ! function_exists ( $ class ) ) { try { $ this -> getCore ( ) -> getLoader ( ) -> loadPlugin ( $ name ) ; } catch ( Exception $ e ) { throw new Exception ( 'Processor ' . $ name . ' could not be found in your plugin directories, please ensure it is in a file named ' . $ name . '.php in the plugin directory' ) ; } } if ( class_exists ( $ class ) ) { return array ( new $ class ( $ this ) , 'process' ) ; } if ( function_exists ( $ class ) ) { return $ class ; } throw new Exception ( 'Wrong processor name, when using autoload the processor must be in one of your plugin dir as "name.php" containg a class or function named "Dwoo_Processor_name"' ) ; } | Internal function to autoload processors at runtime if required . |
44,982 | public function setPointer ( $ position , $ isOffset = false ) { if ( $ isOffset ) { $ this -> pointer += $ position ; } else { $ this -> pointer = $ position ; } } | Sets the pointer position . |
44,983 | public function setLine ( $ number , $ isOffset = false ) { if ( $ isOffset ) { $ this -> line += $ number ; } else { $ this -> line = $ number ; } } | Sets the line number . |
44,984 | public function setTemplateSource ( $ newSource , $ fromPointer = false ) { if ( $ fromPointer === true ) { $ this -> templateSource = substr ( $ this -> templateSource , 0 , $ this -> pointer ) . $ newSource ; } else { $ this -> templateSource = $ newSource ; } } | Overwrites the template that is being compiled . |
44,985 | public function getTemplateSource ( $ fromPointer = false ) { if ( $ fromPointer === true ) { return substr ( $ this -> templateSource , $ this -> pointer ) ; } elseif ( is_numeric ( $ fromPointer ) ) { return substr ( $ this -> templateSource , $ fromPointer ) ; } else { return $ this -> templateSource ; } } | Returns the template that is being compiled . |
44,986 | protected function resolveSubTemplateDependencies ( $ function ) { if ( $ this -> debug ) { echo 'Compiler::' . __FUNCTION__ . "\n" ; } $ body = $ this -> templatePlugins [ $ function ] [ 'body' ] ; foreach ( $ this -> templatePlugins as $ func => $ attr ) { if ( $ func !== $ function && ! isset ( $ attr [ 'called' ] ) && strpos ( $ body , Core :: NAMESPACE_PLUGINS_FUNCTIONS . 'Plugin' . Core :: toCamelCase ( $ func ) ) !== false ) { $ this -> templatePlugins [ $ func ] [ 'called' ] = true ; $ this -> resolveSubTemplateDependencies ( $ func ) ; } } $ this -> templatePlugins [ $ function ] [ 'checked' ] = true ; } | Checks what sub - templates are used in every sub - template so that we re sure they are all compiled . |
44,987 | public function push ( $ content , $ lineCount = null ) { if ( $ lineCount === null ) { $ lineCount = substr_count ( $ content , "\n" ) ; } if ( $ this -> curBlock [ 'buffer' ] === null && count ( $ this -> stack ) > 1 ) { $ this -> stack [ count ( $ this -> stack ) - 2 ] [ 'buffer' ] .= ( string ) $ content ; $ this -> curBlock [ 'buffer' ] = '' ; } else { if ( ! isset ( $ this -> curBlock [ 'buffer' ] ) ) { throw new CompilationException ( $ this , 'The template has been closed too early, you probably have an extra block-closing tag somewhere' ) ; } $ this -> curBlock [ 'buffer' ] .= ( string ) $ content ; } $ this -> line += $ lineCount ; } | Adds compiled content to the current block . |
44,988 | public function addBlock ( $ type , array $ params , $ paramtype ) { if ( $ this -> debug ) { echo 'Compiler::' . __FUNCTION__ . "\n" ; } $ class = current ( array_filter ( [ 'Plugin' . Core :: toCamelCase ( $ type ) , Core :: NAMESPACE_PLUGINS_BLOCKS . 'Plugin' . Core :: toCamelCase ( $ type ) ] , 'class_exists' ) ) ; if ( false === $ class ) { $ this -> getCore ( ) -> getLoader ( ) -> loadPlugin ( $ type ) ; } $ params = $ this -> mapParams ( $ params , array ( $ class , 'init' ) , $ paramtype ) ; $ this -> stack [ ] = array ( 'type' => $ type , 'params' => $ params , 'custom' => false , 'class' => $ class , 'buffer' => null ) ; $ this -> curBlock = & $ this -> stack [ count ( $ this -> stack ) - 1 ] ; return call_user_func ( array ( $ class , 'preProcessing' ) , $ this , $ params , '' , '' , $ type ) ; } | Adds a block to the top of the block stack . |
44,989 | public function addCustomBlock ( $ type , array $ params , $ paramtype ) { $ callback = $ this -> customPlugins [ $ type ] [ 'callback' ] ; if ( is_array ( $ callback ) ) { $ class = is_object ( $ callback [ 0 ] ) ? get_class ( $ callback [ 0 ] ) : $ callback [ 0 ] ; } else { $ class = $ callback ; } $ params = $ this -> mapParams ( $ params , array ( $ class , 'init' ) , $ paramtype ) ; $ this -> stack [ ] = array ( 'type' => $ type , 'params' => $ params , 'custom' => true , 'class' => $ class , 'buffer' => null ) ; $ this -> curBlock = & $ this -> stack [ count ( $ this -> stack ) - 1 ] ; return call_user_func ( array ( $ class , 'preProcessing' ) , $ this , $ params , '' , '' , $ type ) ; } | Adds a custom block to the top of the block stack . |
44,990 | public function removeBlock ( $ type ) { if ( $ this -> debug ) { echo 'Compiler::' . __FUNCTION__ . "\n" ; } $ output = '' ; $ pluginType = $ this -> getPluginType ( $ type ) ; if ( $ pluginType & Core :: SMARTY_BLOCK ) { $ type = 'Smartyinterface' ; } while ( true ) { while ( $ top = array_pop ( $ this -> stack ) ) { if ( $ top [ 'custom' ] ) { $ class = $ top [ 'class' ] ; } else { $ class = current ( array_filter ( [ 'Plugin' . Core :: toCamelCase ( $ top [ 'type' ] ) , Core :: NAMESPACE_PLUGINS_BLOCKS . 'Plugin' . Core :: toCamelCase ( $ top [ 'type' ] ) ] , 'class_exists' ) ) ; } if ( count ( $ this -> stack ) ) { $ this -> curBlock = & $ this -> stack [ count ( $ this -> stack ) - 1 ] ; $ this -> push ( call_user_func ( array ( $ class , 'postProcessing' ) , $ this , $ top [ 'params' ] , '' , '' , $ top [ 'buffer' ] ) , 0 ) ; } else { $ null = null ; $ this -> curBlock = & $ null ; $ output = call_user_func ( array ( $ class , 'postProcessing' ) , $ this , $ top [ 'params' ] , '' , '' , $ top [ 'buffer' ] ) ; } if ( $ top [ 'type' ] === $ type ) { break 2 ; } } throw new CompilationException ( $ this , 'Syntax malformation, a block of type "' . $ type . '" was closed but was not opened' ) ; } return $ output ; } | Removes the closest - to - top block of the given type and all other blocks encountered while going down the block stack . |
44,991 | public function getParamTokens ( array $ params ) { foreach ( $ params as $ k => $ p ) { if ( is_array ( $ p ) ) { $ params [ $ k ] = isset ( $ p [ 2 ] ) ? $ p [ 2 ] : 0 ; } } return $ params ; } | Returns the token of each parameter out of the given parameter array . |
44,992 | protected function parseConstKey ( $ key , $ curBlock ) { $ key = str_replace ( '\\\\' , '\\' , $ key ) ; if ( $ this -> securityPolicy !== null && $ this -> securityPolicy -> getConstantHandling ( ) === SecurityPolicy :: CONST_DISALLOW ) { return 'null' ; } if ( $ curBlock !== 'root' ) { return '(defined("' . $ key . '") ? ' . $ key . ' : null)' ; } return $ key ; } | Parses a constant . |
44,993 | protected function rebuildClassPathCache ( $ path , $ cacheFile ) { $ tmp = array ( ) ; if ( $ cacheFile !== false ) { $ tmp = $ this -> classPath ; $ this -> classPath = array ( ) ; } foreach ( new \ DirectoryIterator ( $ path ) as $ fileInfo ) { if ( ! $ fileInfo -> isDot ( ) ) { if ( $ fileInfo -> isDir ( ) ) { $ this -> rebuildClassPathCache ( $ fileInfo -> getPathname ( ) , false ) ; } else { $ this -> classPath [ $ fileInfo -> getBasename ( '.php' ) ] = $ fileInfo -> getPathname ( ) ; } } } if ( $ cacheFile !== false ) { if ( ! file_put_contents ( $ cacheFile , serialize ( $ this -> classPath ) , LOCK_EX ) ) { throw new Exception ( 'Could not write into ' . $ cacheFile . ', either because the folder is not there (create it) or because of the chmod configuration (please ensure this directory is writable by php), alternatively you can change the directory used with $dwoo->setCompileDir() or provide a custom loader object with $dwoo->setLoader()' ) ; } $ this -> classPath += $ tmp ; } } | Rebuilds class paths scans the given directory recursively and saves all paths in the given file . |
44,994 | public function loadPlugin ( $ class , $ forceRehash = true ) { if ( ( ! isset ( $ this -> classPath [ $ class ] ) || ! is_readable ( $ this -> classPath [ $ class ] ) ) || ( ! isset ( $ this -> classPath [ $ class . 'Compile' ] ) || ! is_readable ( $ this -> classPath [ $ class . 'Compile' ] ) ) ) { if ( $ forceRehash ) { $ this -> rebuildClassPathCache ( $ this -> corePluginDir , $ this -> cacheDir . 'classpath.cache.d' . Core :: RELEASE_TAG . '.php' ) ; foreach ( $ this -> paths as $ path => $ file ) { $ this -> rebuildClassPathCache ( $ path , $ file ) ; } if ( isset ( $ this -> classPath [ $ class ] ) ) { include_once $ this -> classPath [ $ class ] ; } elseif ( isset ( $ this -> classPath [ $ class . 'Compile' ] ) ) { include_once $ this -> classPath [ $ class . 'Compile' ] ; } else { throw new Exception ( 'Plugin "' . $ class . '" can not be found, maybe you forgot to bind it if it\'s a custom plugin ?' , E_USER_NOTICE ) ; } } else { throw new Exception ( 'Plugin "' . $ class . '" can not be found, maybe you forgot to bind it if it\'s a custom plugin ?' , E_USER_NOTICE ) ; } } } | Loads a plugin file . |
44,995 | public function disallowPhpFunction ( $ func ) { if ( is_array ( $ func ) ) { foreach ( $ func as $ fname ) { unset ( $ this -> allowedPhpFunctions [ strtolower ( $ fname ) ] ) ; } } else { unset ( $ this -> allowedPhpFunctions [ strtolower ( $ func ) ] ) ; } } | Removes a php function from the allowed list . |
44,996 | public function allowMethod ( $ class , $ method = null ) { if ( is_array ( $ class ) ) { foreach ( $ class as $ elem ) { $ this -> allowedMethods [ strtolower ( $ elem [ 0 ] ) ] [ strtolower ( $ elem [ 1 ] ) ] = true ; } } else { $ this -> allowedMethods [ strtolower ( $ class ) ] [ strtolower ( $ method ) ] = true ; } } | Adds a class method to the allowed list this must be used for both static and non static method by providing the class name and method name to use . |
44,997 | public function disallowMethod ( $ class , $ method = null ) { if ( is_array ( $ class ) ) { foreach ( $ class as $ elem ) { unset ( $ this -> allowedMethods [ strtolower ( $ elem [ 0 ] ) ] [ strtolower ( $ elem [ 1 ] ) ] ) ; } } else { unset ( $ this -> allowedMethods [ strtolower ( $ class ) ] [ strtolower ( $ method ) ] ) ; } } | Removes a class method from the allowed list . |
44,998 | public function callMethod ( Core $ dwoo , $ obj , $ method , $ args ) { foreach ( $ this -> allowedMethods as $ class => $ methods ) { if ( ! isset ( $ methods [ $ method ] ) ) { continue ; } if ( $ obj instanceof $ class ) { return call_user_func_array ( array ( $ obj , $ method ) , $ args ) ; } } $ dwoo -> triggerError ( 'The current security policy prevents you from calling ' . get_class ( $ obj ) . '::' . $ method . '()' ) ; return null ; } | This is used at run time to check whether method calls are allowed or not . |
44,999 | public function isMethodAllowed ( $ class , $ method = null ) { if ( is_array ( $ class ) ) { list ( $ class , $ method ) = $ class ; } foreach ( $ this -> allowedMethods as $ allowedClass => $ methods ) { if ( ! isset ( $ methods [ $ method ] ) ) { continue ; } if ( $ class === $ allowedClass || is_subclass_of ( $ class , $ allowedClass ) ) { return true ; } } return false ; } | This is used at compile time to check whether static method calls are allowed or not . |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.