Search code examples
restsymfonysortingservicefoscommentbundle

Symfony : private services - provide change options by using Rest -API


I am currently using FOSCommentBundle to get sample example of Rest api best practice

In this bundle there are using Sorter (or Sorting) services with different options, sorting with date asc and date desc, but this services are not injected in sorting provider array (inside the sorting class service)

Reuse of this in the way of oder page by "page number" or "date" range

parameters:
# The sorting factory class
cms_content.sorting_factory.class:  CMS\Bundle\ContentBundle\Sorting\SortingFactory
# Provide to sort by date
cms_content.sorter.date.class:      CMS\Bundle\ContentBundle\Sorting\DateSorting
# Provide to sort by page number
cms_content.sorter.page_nb.class:   CMS\Bundle\ContentBundle\Sorting\PageNbSorting

# 
sorter_sevices_aliases:
    - 'cms_content.sorter.page_nb_desc'
    - 'cms_content.sorter.page_nb_asc'
    - 'cms_content.sorter.date_desc'
    - 'cms_content.sorter.date_asc'

services:
# sort by page nb asc
cms_content.sorter.page_nb_asc:
    class: '%cms_content.sorter.page_nb.class%'
    public: false
    tags:
        - { name: cms_content.sorter, alias: page_nb_asc }
    arguments: [ASC]

# sort by page nb dsc
cms_content.sorter.page_nb_desc:
    class: '%cms_content.sorter.page_nb.class%'
    public: false
    tags:
        - { name: cms_content.sorter, alias: page_nb_desc }
    arguments: [DESC]

# sort by date asc
cms_content.sorter.date_asc:
    class: '%cms_content.sorter.date.class%'
    public: false
    tags:
        - { name: cms_content.sorter, alias: date_asc }
    arguments: [ASC]

# sort by date desc
cms_content.sorter.date_desc:
    class: '%cms_content.sorter.date.class%'
    public: false
    tags:
        - { name: cms_content.sorter, alias: date_desc }
    arguments: [DESC]

# the sorting factory (may be construct with empty array )
cms_content.sorting_factory:
    class: '%cms_content.sorting_factory.class%'
    arguments: ['%sorter_sevices_aliases%', '%cms_content.sorting_factory.default_sorter%']

I have some trouble to use services declared as private inside sorting factory as array of possible sorter provider


Solution

  • Bad practice, I have make a mistake, services shall be inject directly, without pass by array references like that:

    # the sorting factory (may be contruct with empty array )
    cms_content.sorting_factory:
        class: '%cms_content.sorting_factory.class%'
        arguments: [['@cms_content.sorter.page_nb_asc', '@cms_content.sorter.page_nb_desc', '@cms_content.sorter.date_asc', '@cms_content.sorter.date_desc'], '%cms_content.sorting_factory.default_sorter%']