1 package DBIx::Class::MethodAttributes;
6 use DBIx::Class::_Util qw( uniq refdesc visit_namespaces );
7 use Scalar::Util qw( weaken refaddr );
12 my ( $attr_cref_registry, $attr_cache_active );
13 sub DBIx::Class::__Attr_iThreads_handler__::CLONE {
15 # This is disgusting, but the best we can do without even more surgery
16 # Note the if() at the end - we do not run this crap if we can help it
17 visit_namespaces( action => sub {
20 # skip dangerous namespaces
21 return 1 if $pkg =~ /^ (?:
22 DB | next | B | .+? ::::ISA (?: ::CACHE ) | Class::C3
28 exists ${"${pkg}::"}{__cag___attr_cache}
30 ref( my $attr_stash = ${"${pkg}::__cag___attr_cache"} ) eq 'HASH'
32 $attr_stash->{ $attr_cref_registry->{$_}{weakref} } = delete $attr_stash->{$_}
33 for keys %$attr_stash;
37 }) if $attr_cache_active;
39 # renumber the cref registry itself
40 %$attr_cref_registry = map {
41 ( defined $_->{weakref} )
43 # because of how __attr_cache works, ugh
44 "$_->{weakref}" => $_,
47 } values %$attr_cref_registry;
50 sub MODIFY_CODE_ATTRIBUTES {
56 $_ =~ /^[a-z]+$/ ? 'builtin'
57 : $_ =~ /^DBIC_/ ? 'dbic'
63 defined $attr_cref_registry->{$_}{weakref} or delete $attr_cref_registry->{$_}
64 for keys %$attr_cref_registry;
66 # The original misc-attr API used stringification instead of refaddr - can't change that now
67 if( $attr_cref_registry->{$code} ) {
68 Carp::confess( sprintf
69 "Coderefs '%s' and '%s' stringify to the same value '%s': nothing will work",
71 refdesc($attr_cref_registry->{$code}{weakref}),
73 ) if refaddr($attr_cref_registry->{$code}{weakref}) != refaddr($code);
76 weaken( $attr_cref_registry->{$code}{weakref} = $code )
80 if( $attrs->{misc} ) {
82 # if the user never tickles this - we won't have to do a gross
83 # symtable scan in the ithread handler above, so:
85 # User - please don't tickle this
86 $attr_cache_active = 1;
88 $class->mk_classaccessor('__attr_cache' => {})
89 unless $class->can('__attr_cache');
91 $class->__attr_cache->{$code} = [ sort( uniq(
92 @{ $class->__attr_cache->{$code} || [] },
93 keys %{ $attrs->{misc} },
98 if( $attrs->{dbic} ) {
99 my $slot = $attr_cref_registry->{$code};
101 $slot->{attrs} = [ uniq
102 @{ $slot->{attrs} || [] },
104 $class->VALID_DBIC_CODE_ATTRIBUTE($_)
106 Carp::confess( "DBIC-specific attribute '$_' did not pass validation by $class->VALID_DBIC_CODE_ATTRIBUTE() as described in DBIx::Class::MethodAttributes" )
107 } keys %{$attrs->{dbic}},
111 # FIXME - DBIC essentially gobbles up any attribute it can lay its hands on:
114 # There should be some sort of warning on unrecognized attributes or
115 # somesuch... OTOH people do use things in the wild hence the plan of action
116 # is anything but clear :/
118 # https://metacpan.org/source/ZIGOROU/DBIx-Class-Service-0.02/lib/DBIx/Class/Service.pm#L93-110
119 # https://metacpan.org/source/ZIGOROU/DBIx-Class-Service-0.02/t/lib/DBIC/Test/Service/User.pm#L29
120 # https://metacpan.org/source/ZIGOROU/DBIx-Class-Service-0.02/t/lib/DBIC/Test/Service/User.pm#L36
122 # For the time being reuse the old logic for any attribute we do not have
123 # explicit plans for (i.e. stuff that is neither reserved, nor DBIC-internal)
125 # Pass the "builtin attrs" onwards, as the DBIC internals can't possibly handle them
126 return sort keys %{ $attrs->{builtin} || {} };
129 # Address the above FIXME halfway - if something (e.g. DBIC::Helpers) wants to
130 # add extra attributes - it needs to override this in its base class to allow
131 # for 'return 1' on the newly defined attributes
132 sub VALID_DBIC_CODE_ATTRIBUTE {
133 #my ($class, $attr) = @_;
135 # initially no valid attributes
139 sub FETCH_CODE_ATTRIBUTES {
140 #my ($class,$code) = @_;
143 @{ $_[0]->_attr_cache->{$_[1]} || [] },
144 ( defined( $attr_cref_registry->{$_[1]}{ weakref } )
145 ? @{ $attr_cref_registry->{$_[1]}{attrs} || [] }
154 %{ $self->can('__attr_cache') ? $self->__attr_cache : {} },
155 %{ $self->maybe::next::method || {} },
165 DBIx::Class::MethodAttributes - DBIC-specific handling of CODE attributes
169 my @attrlist = attributes::get( \&My::App::Schema::Result::some_method )
173 This class provides the L<DBIx::Class> inheritance chain with the bits
174 necessary for L<attribute|attributes> support on methods.
176 Historically DBIC has accepted any string as a C<CODE> attribute and made
177 such strings available via the semi-private L</_attr_cache> method. This
178 was used for e.g. the long-deprecated L<DBIx::Class::ResultSetManager>,
179 but also has evidence of use on both C<CPAN> and C<DarkPAN>.
181 Starting mid-2016 DBIC treats any method attribute starting with C<DBIC_>
182 as an I<internal boolean decorator> for various DBIC-related methods.
183 Unlike the general attribute naming policy, strict whitelisting is imposed
184 on attribute names starting with C<DBIC_> as described in
185 L</VALID_DBIC_CODE_ATTRIBUTE> below.
187 =head2 DBIC-specific method attributes
189 The following method attributes are currently recognized under the C<DBIC_*>
200 =head2 MODIFY_CODE_ATTRIBUTES
202 See L<attributes/MODIFY_type_ATTRIBUTES>.
204 =head2 FETCH_CODE_ATTRIBUTES
206 See L<attributes/FETCH_type_ATTRIBUTES>. Always returns the combination of
207 all attributes: both the free-form strings registered via the
208 L<legacy system|/_attr_cache> and the DBIC-specific ones.
210 =head2 VALID_DBIC_CODE_ATTRIBUTE
214 =item Arguments: $attribute_string
216 =item Return Value: ( true| false )
220 This method is invoked when processing each DBIC-specific attribute (the ones
221 starting with C<DBIC_>). An attribute is considered invalid and an exception
222 is thrown unless this method returns a C<truthy> value.
228 =item Arguments: none
230 =item Return Value: B<purposefully undocumented>
234 The legacy method of retrieving attributes declared on DBIC methods
235 (L</FETCH_CODE_ATTRIBUTES> was not defined until mid-2016). This method
236 B<does not return any DBIC-specific attributes>, and is kept for backwards
239 In order to query the attributes of a particular method use
240 L<attributes::get()|attributes/get> as shown in the L</SYNOPSIS>.
242 =head1 FURTHER QUESTIONS?
244 Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
246 =head1 COPYRIGHT AND LICENSE
248 This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
249 by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
250 redistribute it and/or modify it under the same terms as the
251 L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.