Web-Components

 view release on metacpan or  search on metacpan

lib/Web/Components/Util.pm  view on Meta::CPAN


sub fqdn (;$) {
   my $x = shift // hostname; return (gethostbyname($x))[0];
}

=item C<is_arrayref>

   $bool = is_arrayref $scalar_variable;

Tests to see if the scalar variable is an array ref

=cut

sub is_arrayref (;$) {
   return $_[ 0 ] && ref $_[ 0 ] eq 'ARRAY' ? 1 : 0;
}

=item C<load_components>

   $hash_ref_of_objects = load_components $search_path, @options_list;

Load and instantiates MVC components. The search path is appended to the
applications classname to define the package namespace that is searched for
components

The options list is a list of keys and values. Either C<application> or,
C<config> and C<log> must be specified. If C<application> is specified it
must define C<config> and C<log> attributes

The configuration object or hash reference must define the C<appclass> and
C<web_components> attributes

The C<web_components> attribute (one of the collection references held by
L<Web::Components::Loader>) is passed to the component constructor method and
is used by a component to discover it's dependencies

An adaptor pattern is possible using the C<web_components_adaptors> attribute

=cut

sub load_components ($;@) {
   my $base = shift;
   my $opts = (is_hashref $_[0]) ? $_[0] : {@_};

   throw(Unspecified, ['component base']) unless $base;

   my $app = $opts->{application};
   # If the app object is defined it must have a config attribute
   # uncoverable condition false
   my $config = $opts->{config} // $app->config;
   my $appclass = deref $config, 'appclass';
   # The config object/hash ref is required. It must have an appclass attribute
   # uncoverable branch true
   throw(Unspecified, ['config appclass']) unless $appclass;

   my $search_path;

   if (first_char $base eq '+') { $search_path = $base = substr $base, 1 }
   else { $search_path = "${appclass}::${base}" }

   my $depth = () = split m{ :: }mx, $search_path, -1;

   $depth += 1;

   my $finder = Module::Pluggable::Object->new(
      max_depth   => $depth,
      min_depth   => $depth,
      search_path => [$search_path],
      require     => TRUE,
   );
   my $compos = $opts->{components} //= {}; # Dependency injection
   my $comp_cfg = (deref $config, 'web_components') // {};

   for my $class ($finder->plugins) {
      _setup_component($compos, $comp_cfg, $opts, $appclass, $class);
   }

   my $cfgcomps = deref $config, 'web_components_adaptors';

   if ($cfgcomps) { $cfgcomps = $cfgcomps->{$base} }
   else { return $compos }

   for my $moniker (keys %{$cfgcomps}) {
      my $class = "${base}::".(ucfirst $moniker);
      my @roles = @{ _qualify($appclass, $cfgcomps->{$moniker}) };
      my $cwr   = Moo::Role->create_class_with_roles($search_path, @roles);

      _setup_component($compos, $comp_cfg, $opts, $appclass, $class, $cwr);
   }

   return $compos;
}

sub _app_prefix ($) {
   (my $v = lc ($_[0] // q())) =~ s{ :: }{_}gmx;

   return $v;
}

sub _env_prefix ($) {
   return uc _app_prefix $_[0];
}

=item C<ns_environment>

   $value = ns_environment $class, $key, [$value];

An accessor/mutator for the environment variables prefixed by the supplied
class name. Providing a value is optional, always returns the current value

=cut

sub ns_environment ($$;$) {
   my ($class, $k, $v) = @_;

   $k = (_env_prefix $class) . '_' . (uc $k);

   return defined $v ? $ENV{$k} = $v : $ENV{$k};
}

=item C<throw>



( run in 0.819 second using v1.01-cache-2.11-cpan-71847e10f99 )