Documentation ¶
Overview ¶
Package store implements the handling of websites, groups and stores.
The following shows a hierarchical diagram of the structure:
+---------------------+ | Website | | ID <-----------------+---+ | Code | | | +----+ Default Group ID | | | | | Is Default | | | | +---------------------+ | | | | | | +----------------------+ | | | | Group | | | +------> ID | | | | Website ID +-----------+ | | Root Category ID | | +------+ Default Store ID | | | +----------------------+ | | | | +---------------+ | | | Store | | | | ID | | | | Code | | +--------> Group ID | | | Website ID +-------------+ | Is Active | +---------------+ http://asciiflow.com
Those three objects also represents the tables in the database.
Sub package Scope ¶
The subpackage scope depends on these structure except that the group has been removed and a default scope has been introduced.
Supported build tags: csall or proto or db
More explanation @todo Read more: http://magento-quickies.alanstorm.com/post/146610004255/taming-magento-2s-scope https://cyrillschumacher.com/2015/04/20/magento2-stores-and-scopes/
Code generated by codegen. DO NOT EDIT. Generated by sql/dmlgen. DO NOT EDIT.
Index ¶
- Constants
- func CodeIsValid(c string) error
- type CodeProcessor
- type Finder
- type Option
- type Service
- func (s *Service) AllowedStores(runMode scope.TypeID) (*Stores, error)
- func (s *Service) ClearCache()
- func (s *Service) Close() error
- func (s *Service) DefaultStoreID(runMode scope.TypeID) (websiteID, storeID uint32, _ error)
- func (s *Service) DefaultStoreView() (*Store, error)
- func (s *Service) Group(id uint32) (*StoreGroup, error)
- func (s *Service) Groups() StoreGroups
- func (s *Service) IsAllowedStoreID(runMode scope.TypeID, storeID uint32) (isAllowed bool, storeCode string, _ error)
- func (s *Service) IsCacheEmpty() bool
- func (s *Service) Options(opts ...Option) error
- func (s *Service) Store(id uint32) (*Store, error)
- func (s *Service) StoreIDbyCode(runMode scope.TypeID, storeCode string) (websiteID, storeID uint32, err error)
- func (s *Service) Stores() Stores
- func (s *Service) Website(id uint32) (*StoreWebsite, error)
- func (s *Service) Websites() StoreWebsites
- type Store
- type StoreGroup
- type StoreGroups
- func (cc *StoreGroups) Append(n ...*StoreGroup) *StoreGroups
- func (cc *StoreGroups) Codes(ret ...string) []string
- func (cc *StoreGroups) Cut(i, j int) *StoreGroups
- func (cc *StoreGroups) Delete(i int) *StoreGroups
- func (cc *StoreGroups) Each(f func(*StoreGroup)) *StoreGroups
- func (cc *StoreGroups) Filter(f func(*StoreGroup) bool) *StoreGroups
- func (cc *StoreGroups) GroupIDs(ret ...uint32) []uint32
- func (cc *StoreGroups) Insert(n *StoreGroup, i int) *StoreGroups
- func (cc *StoreGroups) Len() int
- func (cc *StoreGroups) Swap(i, j int)
- func (cc *StoreGroups) Validate() (err error)
- func (cc *StoreGroups) WriteTo(w io.Writer) (n int64, err error)
- type StoreWebsite
- func (e *StoreWebsite) Copy() *StoreWebsite
- func (e *StoreWebsite) DefaultGroup() (*StoreGroup, error)
- func (e *StoreWebsite) DefaultStore() (*Store, error)
- func (e *StoreWebsite) Empty() *StoreWebsite
- func (e *StoreWebsite) Validate() error
- func (e *StoreWebsite) WriteTo(w io.Writer) (n int64, err error)
- type StoreWebsites
- func (cc *StoreWebsites) Append(n ...*StoreWebsite) *StoreWebsites
- func (cc *StoreWebsites) Codes(ret ...string) []string
- func (cc *StoreWebsites) Cut(i, j int) *StoreWebsites
- func (cc *StoreWebsites) Default() (*StoreWebsite, error)
- func (cc *StoreWebsites) Delete(i int) *StoreWebsites
- func (cc *StoreWebsites) Each(f func(*StoreWebsite)) *StoreWebsites
- func (cc *StoreWebsites) Filter(f func(*StoreWebsite) bool) *StoreWebsites
- func (cc *StoreWebsites) Insert(n *StoreWebsite, i int) *StoreWebsites
- func (cc *StoreWebsites) Len() int
- func (cc *StoreWebsites) Swap(i, j int)
- func (cc *StoreWebsites) Validate() (err error)
- func (cc *StoreWebsites) WebsiteIDs(ret ...uint32) []uint32
- func (cc *StoreWebsites) WriteTo(w io.Writer) (n int64, err error)
- type Stores
- func (cc *Stores) Append(n ...*Store) *Stores
- func (cc *Stores) Codes(ret ...string) []string
- func (cc *Stores) Cut(i, j int) *Stores
- func (cc *Stores) Delete(i int) *Stores
- func (cc *Stores) Each(f func(*Store)) *Stores
- func (cc *Stores) Filter(f func(*Store) bool) *Stores
- func (cc *Stores) Insert(n *Store, i int) *Stores
- func (cc *Stores) Len() int
- func (cc *Stores) StoreIDs(ret ...uint32) []uint32
- func (cc *Stores) Swap(i, j int)
- func (cc *Stores) Validate() (err error)
- func (cc *Stores) WriteTo(w io.Writer) (n int64, err error)
Constants ¶
const CodeFieldName = `store`
CodeFieldName defines the filed name where store code has been saved. Used in Cookies and JSON Web Tokens (JWT) to identify an active store besides from the default loaded store.
const CodeMaxLen = 32
CodeMaxLen defines the overall maximum length a store code can have.
const CodeURLFieldName = `___store`
CodeURLFieldName name of the GET parameter to set a new store in a current website/group context/request.
const DefaultStoreID int64 = 0
DefaultStoreID is always 0.
Variables ¶
This section is empty.
Functions ¶
func CodeIsValid ¶
CodeIsValid checks if a store code is valid. Returns an ErrStoreCodeEmpty or an ErrStoreCodeInvalid if the first letter is not a-zA-Z and followed by a-zA-Z0-9_ or store code length is greater than 32 characters. Error behaviour: NotValid
Types ¶
type CodeProcessor ¶
type CodeProcessor interface { // FromRequest returns the valid non-empty store code. Returns an empty // store code on all other cases. FromRequest(runMode scope.TypeID, req *http.Request) (code string) // ProcessDenied gets called in the middleware WithRunMode whenever a store // ID isn't allowed to proceed. The variable newStoreID reflects the denied // store ID. The ResponseWriter and Request variables can be used for // additional information writing and extracting. The error Handler will // always be called. ProcessDenied(runMode scope.TypeID, oldStoreID, newStoreID int64, w http.ResponseWriter, r *http.Request) // ProcessAllowed enables to adjust the ResponseWriter based on the new // store ID. The variable newStoreID contains the new ID, which can also be // 0. The code is guaranteed to be not empty, a valid store code, and always // points to an existing active store. The ResponseWriter and Request // variables can be used for additional information writing and extracting. // The next Handler in the chain will after this function be called. ProcessAllowed(runMode scope.TypeID, oldStoreID, newStoreID int64, newStoreCode string, w http.ResponseWriter, r *http.Request) }
CodeProcessor gets used in the middleware WithRunMode() to extract a store code from a Request and modify the response; for example setting cookies to persists the selected store.
type Finder ¶
type Finder interface { // DefaultStoreID returns the default active store ID and its website ID // depending on the run mode. Error behaviour is mostly of type NotValid. DefaultStoreID(runMode scope.TypeID) (websiteID, storeID uint32, err error) // StoreIDbyCode returns, depending on the runMode, for a storeCode its // active store ID and its website ID. An empty runMode hash falls back to // select the default website, with its default group, and the slice of // default stores. A not-found error behaviour gets returned if the code // cannot be found. If the runMode equals to scope.DefaultTypeID, the // returned ID is always 0 and error is nil. StoreIDbyCode(runMode scope.TypeID, storeCode string) (websiteID, storeID uint32, err error) }
Finder depends on the runMode from package scope and finds the active store depending on the run mode. The Hash argument will be provided via scope.RunMode type or the scope.FromContextRunMode(ctx) function. runMode is named in Mage world: MAGE_RUN_CODE and MAGE_RUN_TYPE. The MAGE_RUN_TYPE can be either website or store scope and MAGE_RUN_CODE any defined website or store code from the database. In our case we must pass an ID and not a code string.
type Option ¶
type Option struct {
// contains filtered or unexported fields
}
Option type to pass options to the service type.
func WithGroups ¶
func WithGroups(groups ...*StoreGroup) Option
WithGroups upserts new groups to the Store service. It clears the internal cache group and sorts the groups by GroupID.
func WithStores ¶
WithStores upserts new stores to the Store service. It clears the internal cache store and sorts the stores by StoreID.
func WithWebsites ¶
func WithWebsites(websites ...*StoreWebsite) Option
WithWebsites upserts new websites to the Store service. It clears the internal cache website and sorts the websites by WebsiteID.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
func MustNewService ¶
MustNewService same as NewService, but panics on error.
func NewService ¶
NewService creates a new store Service which handles websites, groups and stores. You must either provide the functional options or call LoadFromDB() to setup the internal cache.
func (*Service) AllowedStores ¶
AllowedStores creates a new slice containing all active stores depending on the current runMode. The returned slice and its pointers are owned by the callee.
func (*Service) ClearCache ¶
func (s *Service) ClearCache()
ClearCache resets the internal caches which stores the pointers to Websites, Groups or Stores. The ReInit() also uses this method to clear caches before the Storage gets reloaded.
func (*Service) DefaultStoreID ¶
DefaultStoreID returns the default active store ID depending on the run mode. Error behaviour is mostly of type NotValid.
func (*Service) DefaultStoreView ¶
DefaultStoreView returns the overall default store view depending on the website, group and store settings. It traverses websites, looks for is_default, returns the website.default_group_id, then group with default_store_id and finally loads the active store. If not an active store can be found and NotFound error gets returned.
func (*Service) Group ¶
func (s *Service) Group(id uint32) (*StoreGroup, error)
Group returns a cached Group which contains all related stores and its website. The returned pointer is owned by the Service. You must copy it for further modifications.
func (*Service) Groups ¶
func (s *Service) Groups() StoreGroups
Groups returns a cached slice containing all Groups with its associated stores and websites. You shall not modify the returned slice.
func (*Service) IsAllowedStoreID ¶
func (s *Service) IsAllowedStoreID(runMode scope.TypeID, storeID uint32) (isAllowed bool, storeCode string, _ error)
IsAllowedStoreID checks if the store ID is allowed within the runMode. Returns true on success. An error may occur when the default website and store can't be selected. An empty scope.Hash checks the default website with its default group and its default stores.
func (*Service) IsCacheEmpty ¶
IsCacheEmpty returns true if the internal cache is empty.
func (*Service) Store ¶
Store returns the cached Store view containing its group and its website. The returned pointer is owned by the Service. You must copy it for further modifications.
func (*Service) StoreIDbyCode ¶
func (s *Service) StoreIDbyCode(runMode scope.TypeID, storeCode string) (websiteID, storeID uint32, err error)
StoreIDbyCode returns, depending on the runMode, for a storeCode its active store ID and its website ID. An empty runMode hash falls back to select the default website, with its default group, and the slice of default stores. A not-found error behaviour gets returned if the code cannot be found. If the runMode equals to scope.DefaultTypeID, the returned ID is always 0 and error is nil. Implements interface Finder.
func (*Service) Stores ¶
Stores returns a cached Store slice containing all related websites and groups. You shall not modify the returned slice.
func (*Service) Website ¶
func (s *Service) Website(id uint32) (*StoreWebsite, error)
Website returns the cached Website from an ID including all of its groups and all related stores. The returned pointer is owned by the Service. You must copy it for further modifications.
func (*Service) Websites ¶
func (s *Service) Websites() StoreWebsites
Websites returns a cached slice containing all Websites with its associated groups and stores. You shall not modify the returned slice.
type Store ¶
type Store struct { StoreID uint32 `max_len:"5"` // store_id smallint(5) unsigned NOT NULL PRI auto_increment "Store ID" Code string `max_len:"64"` // code varchar(64) NOT NULL UNI "Code" WebsiteID uint32 `max_len:"5"` // website_id smallint(5) unsigned NOT NULL MUL DEFAULT '0' "Website ID" GroupID uint32 `max_len:"5"` // group_id smallint(5) unsigned NOT NULL MUL DEFAULT '0' "Group ID" Name string `max_len:"255"` // name varchar(255) NOT NULL "Store Name" SortOrder uint32 `max_len:"5"` // sort_order smallint(5) unsigned NOT NULL DEFAULT '0' "Store Sort Order" IsActive bool `max_len:"5"` // is_active smallint(5) unsigned NOT NULL MUL DEFAULT '0' "Store Activity" StoreGroup *StoreGroup // 1:1 store.group_id => store_group.group_id StoreWebsite *StoreWebsite // 1:1 store.website_id => store_website.website_id }
Store represents a single row for DB table store. Auto generated.
type StoreGroup ¶
type StoreGroup struct { GroupID uint32 `max_len:"5"` // group_id smallint(5) unsigned NOT NULL PRI auto_increment "Group ID" WebsiteID uint32 `max_len:"5"` // website_id smallint(5) unsigned NOT NULL MUL DEFAULT '0' "Website ID" Name string `max_len:"255"` // name varchar(255) NOT NULL "Store Group Name" RootCategoryID uint32 `max_len:"10"` // root_category_id int(10) unsigned NOT NULL DEFAULT '0' "Root Category ID" DefaultStoreID uint32 `max_len:"5"` // default_store_id smallint(5) unsigned NOT NULL MUL DEFAULT '0' "Default Store ID" Code string `max_len:"64"` // code varchar(64) NOT NULL UNI "Store group unique code" StoreWebsite *StoreWebsite // 1:1 store_group.website_id => store_website.website_id }
StoreGroup represents a single row for DB table store_group. Auto generated.
func (*StoreGroup) Copy ¶
func (e *StoreGroup) Copy() *StoreGroup
Copy copies the struct and returns a new pointer
func (*StoreGroup) Empty ¶
func (e *StoreGroup) Empty() *StoreGroup
Empty empties all the fields of the current object. Also known as Reset.
func (*StoreGroup) Validate ¶
func (e *StoreGroup) Validate() error
Validate runs internal consistency tests.
type StoreGroups ¶
type StoreGroups struct {
Data []*StoreGroup `json:"data,omitempty"`
}
StoreGroups represents a collection type for DB table store_group Not thread safe. Auto generated.
func NewStoreGroups ¶
func NewStoreGroups() *StoreGroups
NewStoreGroups creates a new initialized collection. Auto generated.
func (*StoreGroups) Append ¶
func (cc *StoreGroups) Append(n ...*StoreGroup) *StoreGroups
Append will add a new item at the end of * StoreGroups . Auto generated via dmlgen.
func (*StoreGroups) Codes ¶
func (cc *StoreGroups) Codes(ret ...string) []string
Codes returns a slice with the data or appends it to a slice. Auto generated.
func (*StoreGroups) Cut ¶
func (cc *StoreGroups) Cut(i, j int) *StoreGroups
Cut will remove items i through j-1. Auto generated via dmlgen.
func (*StoreGroups) Delete ¶
func (cc *StoreGroups) Delete(i int) *StoreGroups
Delete will remove an item from the slice. Auto generated via dmlgen.
func (*StoreGroups) Each ¶
func (cc *StoreGroups) Each(f func(*StoreGroup)) *StoreGroups
Each will run function f on all items in []* StoreGroup . Auto generated via dmlgen.
func (*StoreGroups) Filter ¶
func (cc *StoreGroups) Filter(f func(*StoreGroup) bool) *StoreGroups
Filter filters the current slice by predicate f without memory allocation. Auto generated via dmlgen.
func (*StoreGroups) GroupIDs ¶
func (cc *StoreGroups) GroupIDs(ret ...uint32) []uint32
GroupIDs returns a slice with the data or appends it to a slice. Auto generated.
func (*StoreGroups) Insert ¶
func (cc *StoreGroups) Insert(n *StoreGroup, i int) *StoreGroups
Insert will place a new item at position i. Auto generated via dmlgen.
func (*StoreGroups) Len ¶
func (cc *StoreGroups) Len() int
Len will satisfy the sort.Interface. Auto generated via dmlgen.
func (*StoreGroups) Swap ¶
func (cc *StoreGroups) Swap(i, j int)
Swap will satisfy the sort.Interface. Auto generated via dmlgen.
func (*StoreGroups) Validate ¶
func (cc *StoreGroups) Validate() (err error)
Validate runs internal consistency tests on all items.
type StoreWebsite ¶
type StoreWebsite struct { WebsiteID uint32 `max_len:"5"` // website_id smallint(5) unsigned NOT NULL PRI auto_increment "Website ID" Code string `max_len:"64"` // code varchar(64) NOT NULL UNI "Code" Name null.String `max_len:"128"` // name varchar(128) NULL DEFAULT 'NULL' "Website Name" SortOrder uint32 `max_len:"5"` // sort_order smallint(5) unsigned NOT NULL MUL DEFAULT '0' "Sort Order" DefaultGroupID uint32 `max_len:"5"` // default_group_id smallint(5) unsigned NOT NULL MUL DEFAULT '0' "Default Group ID" IsDefault bool `max_len:"5"` // is_default smallint(5) unsigned NOT NULL DEFAULT '0' "Defines Is Website Default" Stores *Stores // Reversed 1:M store_website.website_id => store.website_id StoreGroups *StoreGroups // Reversed 1:M store_website.website_id => store_group.website_id }
StoreWebsite represents a single row for DB table store_website. Auto generated.
func (*StoreWebsite) Copy ¶
func (e *StoreWebsite) Copy() *StoreWebsite
Copy copies the struct and returns a new pointer
func (*StoreWebsite) DefaultGroup ¶
func (e *StoreWebsite) DefaultGroup() (*StoreGroup, error)
func (*StoreWebsite) DefaultStore ¶
func (e *StoreWebsite) DefaultStore() (*Store, error)
DefaultStore returns the first default active store.
func (*StoreWebsite) Empty ¶
func (e *StoreWebsite) Empty() *StoreWebsite
Empty empties all the fields of the current object. Also known as Reset.
func (*StoreWebsite) Validate ¶
func (e *StoreWebsite) Validate() error
Validate runs internal consistency tests.
type StoreWebsites ¶
type StoreWebsites struct {
Data []*StoreWebsite `json:"data,omitempty"`
}
StoreWebsites represents a collection type for DB table store_website Not thread safe. Auto generated.
func NewStoreWebsites ¶
func NewStoreWebsites() *StoreWebsites
NewStoreWebsites creates a new initialized collection. Auto generated.
func (*StoreWebsites) Append ¶
func (cc *StoreWebsites) Append(n ...*StoreWebsite) *StoreWebsites
Append will add a new item at the end of * StoreWebsites . Auto generated via dmlgen.
func (*StoreWebsites) Codes ¶
func (cc *StoreWebsites) Codes(ret ...string) []string
Codes returns a slice with the data or appends it to a slice. Auto generated.
func (*StoreWebsites) Cut ¶
func (cc *StoreWebsites) Cut(i, j int) *StoreWebsites
Cut will remove items i through j-1. Auto generated via dmlgen.
func (*StoreWebsites) Default ¶
func (cc *StoreWebsites) Default() (*StoreWebsite, error)
Default returns the default website. The returned pointer is owned by StoreWebsites.
func (*StoreWebsites) Delete ¶
func (cc *StoreWebsites) Delete(i int) *StoreWebsites
Delete will remove an item from the slice. Auto generated via dmlgen.
func (*StoreWebsites) Each ¶
func (cc *StoreWebsites) Each(f func(*StoreWebsite)) *StoreWebsites
Each will run function f on all items in []* StoreWebsite . Auto generated via dmlgen.
func (*StoreWebsites) Filter ¶
func (cc *StoreWebsites) Filter(f func(*StoreWebsite) bool) *StoreWebsites
Filter filters the current slice by predicate f without memory allocation. Auto generated via dmlgen.
func (*StoreWebsites) Insert ¶
func (cc *StoreWebsites) Insert(n *StoreWebsite, i int) *StoreWebsites
Insert will place a new item at position i. Auto generated via dmlgen.
func (*StoreWebsites) Len ¶
func (cc *StoreWebsites) Len() int
Len will satisfy the sort.Interface. Auto generated via dmlgen.
func (*StoreWebsites) Swap ¶
func (cc *StoreWebsites) Swap(i, j int)
Swap will satisfy the sort.Interface. Auto generated via dmlgen.
func (*StoreWebsites) Validate ¶
func (cc *StoreWebsites) Validate() (err error)
Validate runs internal consistency tests on all items.
func (*StoreWebsites) WebsiteIDs ¶
func (cc *StoreWebsites) WebsiteIDs(ret ...uint32) []uint32
WebsiteIDs returns a slice with the data or appends it to a slice. Auto generated.
type Stores ¶
type Stores struct {
Data []*Store `json:"data,omitempty"`
}
Stores represents a collection type for DB table store Not thread safe. Auto generated.
func NewStores ¶
func NewStores() *Stores
NewStores creates a new initialized collection. Auto generated.
func (*Stores) Append ¶
Append will add a new item at the end of * Stores . Auto generated via dmlgen.
func (*Stores) Codes ¶
Codes returns a slice with the data or appends it to a slice. Auto generated.
func (*Stores) Each ¶
Each will run function f on all items in []* Store . Auto generated via dmlgen.
func (*Stores) Filter ¶
Filter filters the current slice by predicate f without memory allocation. Auto generated via dmlgen.
func (*Stores) StoreIDs ¶
StoreIDs returns a slice with the data or appends it to a slice. Auto generated.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package mock implements mocking of the store.Service for tests.
|
Package mock implements mocking of the store.Service for tests. |
Package scope defines the configuration of scopes default, website, group and store.
|
Package scope defines the configuration of scopes default, website, group and store. |